kfli

60 Reputation

4 Badges

4 years, 201 days

MaplePrimes Activity


These are questions asked by kfli

Hello,

I have tried to get a simple matlab example of a fourier transform code to work in Maple. This is just to understand a simple fourier transform and eventually try some more difficult 2D transforms.

restart:
with(LinearAlgebra):
with(RandomTools):
with(orthopoly):
with(plots):
with(ArrayTools):
with(DiscreteTransforms):
Digits:=15:

# 1D Fourier Transform

Fs := 1000;                       # Sampling frequency                    
T := 1/Fs;                        # Sampling period       
L := 1500;                        # Length of signal
t := Vector(L, i -> i-1)*T:       # Time vector

f := Fs*Vector(floor(L/2)+1, i-> i-1)/L:   # Frequency                 


S:= Vector(L, i -> 0.7*sin(2.0*Pi*50*t[i]) + sin(2.0*Pi*120*t[i])):    # Signal


Z1 := FourierTransform(Vector(L, j->S[j])):              # DFT 
Z2 := Vector(L, i-> sqrt(Re(Z1[i])**2 + Im(Z1[i])**2)):  # Amplitude
                

FP1 := pointplot({seq([f[n],Z2[n]],n=1..floor(L/2)+1)}, labels=["Frequency","Amplitude"], connect=true, color=green):

display(FP1, axes=boxed);

The right answer should be a plot with frequencies at 50Hz and 120Hz, with amplitudes at 0.7 and 1.0, respectively. However my amplitude axes is off somehow and I don't understand why. 

Hello,

I'm compiling a maple procedure to a C code with the Compiler command. I was wondering if it is possible to change the optimization flag, for example -03  for gcc? Is there a way to maybe change a file in the Maple directory that specifies the compiler options?

Hello,

I am trying to generate a compiled c function

dc_test := proc( a :: Array(datatype=float[8]), b :: Array(datatype=float[8]), c :: Array(datatype=float[8]) ) 

c[..,..] := a . b;

return 0

end:

cdc_test:=Compiler:-Compile(dc_test, optimize):

a := Array(1..2, 1..2, fill=1.0, datatype=float[8]);
b := Array(1..2, 1..2, fill=1.0, datatype=float[8]);
c := Array(1..2, 1..2, datatype=float[8]);
                           
dc_test(a, b, c);

cdc_test(a, b, c);

This doesn't work because it doesn't recognize the "." symbol. Also if I were to take the transpose, for example Transpose(a), then it doesn't recognize that either. Are there commands for this that I'm unware of or do you have to program them from scratch?

Hello,

is it possible to send compiled Maple functions (C function) to the nodes with the Grid toolbox? I can run the non-compiled function on the nodes but when I try to send a compiled version of the same function I get segmentation error. I have run the compiled function just fine in other nonparallel codes, and also with the Threads package. So I was wondering if there is something else you need to do to get it to work? Maybe compile on each node?

 

Hi! I have been trying to calculate the value of theClenshaw derivative. I can get the regular clenshaw to work but not the derivative. I'm going off of the thread (https://scicomp.stackexchange.com/questions/27865/clenshaw-type-recurrence-for-derivative-of-chebyshev-series). (P.s notice I have halfed the A[z+1] term. I have tried both ways but the overall result is wrong so I am guessing something more important is wrong).

This is my code so far

Clenshaw_Dx_1D:=proc(z,C,Nm,s)
local i,k,A,B: global Clen1D_Dx: 

A := Vector(z..Nm+1+z);
B := Vector(z..Nm+1+z);

for k from Nm-1+z by -1 to 1+z do
A[k] := C[k] + 2*s*A[k+1] - A[k+2]:
od:

for k from Nm-1+z by -1 to 1+z do
B[k] := 2*A[k+1] + 2*s*B[k+1] - B[k+2]:
od:
Clen1D_Dx :=  A[z+1]/2 + s*B[1+z] - B[2+z]:

end:

Where z could be 0 or 1 depending on what index your C array starts at. C is the array of chebyshev coefficients of a Chebyshev series appromating u(x) for example. The Chebycoeff1D procedure calculates the Chebyshev coefficients  and the code below calls the procedure for a specific function. Try it with some values of xM, for example 4-10.
 

Chebycoeff1D:=proc(express,Nn,C2,A,B)
local Cfac,fac1x,fac2x,k,K;

fac1x:=Pi/Nn;
  for k from 1 to Nn do 
  Cfac(k):=eval(subs(x=evalf(cos(fac1x*(k-0.5)))*A+B,evalf(express))); 
  od; unassign('k'):                    
  for K from 1 to Nn do
    fac2x:=Pi*(K-1)/Nn;
    C2[K-1]:=(2/Nn)*add(Cfac(k)*evalf(cos(fac2x*(k-0.5))),k=1..Nn);
  od:
end:
nn := 1.0:
Lc := 0.0: Rc := 1.0:
func:=nn*sin(2*Pi*x)+0.5*nn*sin(Pi*x):
Chebycoeff1D(func,xM+1,C,0.5*(Rc-Lc),0.5*(Rc+Lc)):

Once you have the C array call Clenshaw_Dx_1D. For example
 

Clenshaw_Dx_1D(0,C,xM+1,0.0);  # Evalutes f'(x) in the middle of the domain.

Check with

subs(x=0.5, diff(func,x));

The overall "pattern/shape" of the derivative values is correct, just not the amplitude/values. Any help here? Where is the Clenshaw derivative procedure going wrong.

1 2 3 Page 1 of 3