> | restart; |
> | #F:=x->piecewise(x=0,1/2,x>0,x);
#F:=x->piecewise(x<1/2,x,x>=1/2,1-x); #F:=x->piecewise(x<1/2,-1,x>1/2,1); #F:=x->piecewise(x<1/2,-1,x>=1/2,1); F:=x->piecewise(x>0 and x<1/2,-1,x>1/2,1); plot(F(x),x=0..1); |
> | KK:=2;
N:=2^KK;L:=1-0; |
Direct Integrations for Fourier Series
> | for n from 0 to N do
a[n]:=2/L*int(F(x)*cos(2*Pi*n/L*x),x=0..L); end do; |
> | for n from 0 to N do
b[n]:=2/L*int(F(x)*sin(2*Pi*n/L*x),x=0..L); end do; |
> | for n from 0 to N do
c[n]:=1/L*int(F(x)*exp(-I*2*Pi*n/L*x),x=0..L); end do; for n from 1 to N do c[-n]:=1/L*int(F(x)*exp(I*2*Pi*n/L*x),x=0..L); end do; |
Note that:
a[n]=c]n]+c[-n], b[n]=I (c[n]-c[-n])
c[-n]= 1/2 (a[n] + b[n]), c[n]=1/2 (a[n] - I b[n])
> | F1:=unapply(sum(evalf(c[i]*exp(I*2*Pi*i/L*x)),i=-(N-1)..(N-1)),x): |
> | plot({Re(F1(x)),F(x)},x=0..1); |
Check zero of imaginary part due to the cancellation
> | NN:=20;
for k from 0 to NN do F1(k/NN); end do: |
Orthogonality for the integration
> | KK:=3;
N:=2^KK;L:=1-0; |
> | for k from 0 to N-1 do
c[k]:=evalf(sum(F(i*L/N)*exp(-I*2*Pi*k*i/N),i=0..N-1)); end do; |
> | F1:=unapply(sum(evalf(c[i]*exp(I*2*Pi*i/L*x)/N),i=0..(N/2-1))+
sum(evalf(c[N-i]*exp(-I*2*Pi*i/L*x)/N),i=1..(N/2-1)),x): |
> | plot({Re(F1(x)),F(x)},x=0..1); |
> | NN:=20;
for k from 0 to NN do F1(k/NN); end do: |
> |
FFT
> | x1:=array([evalf(seq(F(i/N),i=0..N-1))]);
y1:=array([evalf(seq(0,i=0..N-1))]); |
> | FFT(KK,x1,y1); |
> | print(x1);print(y1); |
> | F2:=unapply(sum(evalf((x1[i]+I*y1[i])*exp(I*2*Pi*(i-1)/L*x)/N),i=1..N/2)+
sum(evalf((x1[N-i+2]+I*y1[N-i+2])*exp(-I*2*Pi*(i-1)/L*x)/N),i=2..N/2),x): |
> | plot({Re(F2(x)),F(x)},x=0..1); |
> |