Comparing Fourier Series and Fourier Transform

rect[x_,amp_,per_,off_]:=Piecewise[{{amp+off,0≤Mod[x,per]<(per/2)},{-amp+off,(per/2)≤Mod[x,per]<per}}]
tria[x_,amp_,per_,off_]:=Piecewise[{{(4amp*((per-Mod[x-(per/4),per])/per)-3amp+off),0≤Mod[x-(per/4),per]<(per/2)},{(4amp*((Mod[x-(per/4),per])/per)-3amp+off),(per/2)≤Mod[x-(per/4),per]<per}}]
sin[x_,amp_,per_,off_]:=Sin[(amp*x)*(per/(2π))]+off

ao[func_,per_]:=(1/(2π))(∫_0^per)func\[DifferentialD]x
an[func_,per_,n_]:=(1/π)(∫_0^per)func*Cos[n*x]\[DifferentialD]x
bn[func_,per_,n_]:=(1/π)(∫_0^per)func*Sin[n*x]\[DifferentialD]x

Ao[func_,per_]:=ao[func,per]
An[func_,per_,n_]:=(Sqrt[((an[func,per,n])^2)+((bn[func,per,n])^2)])

phA[func_,per_,n_]:=Arg[bn[func,per,n]+I*an[func,per,n]]

h[func_,per_,x_,lim_]:=ao[func,per]+(∑_(n=1)^lim)an[func,per,n]*Cos[n*x]+(∑_(n=1)^lim)bn[func,per,n]*Sin[n*x]

X[func_,per_,ω_]:=(∫_(-(per/2))^(per/2))func*(E^(-I*ω*x))\[DifferentialD]x

Manipulate[
Module[{four,abs,pha,fourtrans,phx},
four=h[func,2π,x,deg];
abs=Join[{{0,ao[func,2π]}},Table[{n,An[func,2π,n]},{n,1,deg}]];pha=Table[{n,((180*phA[func,2π,n])/(2π))},{n,1,deg}];
fourtrans=X[func,2π,ω];
phx=((180*Arg[fourtrans])/(2π));
Switch[ctrl,
1,
Column[{
Plot[four,{x,-2π,4π},ImageSize->{350,200}],
Plot[Piecewise[{{0,x<-π},{func,-π<x<π},{0,x>π}}],{x,-2π,4π},ImageSize->{350,200}]
}],
2,
Column[{
ListPlot[abs,PlotRange->{-0.1,1.5},Joined->False,Filling->Axis,Frame->True,Axes->True,PlotRange->All,FrameLabel->{{None,None},{"harmonic", "Fourier Series Spectrum"}},FrameTicks->{Automatic,Automatic},PlotRangeClipping->True,AspectRatio->0.4,ImageSize->{350,200}],
Plot[Abs[fourtrans],{ω,0,deg},Filling->Axis,Frame->True,Axes->True,PlotRange->All,FrameLabel->{{None,None},{"harmonic", "Fourier Transform Spectrum"}},FrameTicks->{Automatic,Automatic},PlotRangeClipping->True,AspectRatio->0.4,ImageSize->{350,200}]
}],
3,
Column[{
ListPlot[pha,PlotRange->{-180,180},Joined->False,Filling->Axis,Frame->True,Axes->True,PlotRange->All,FrameLabel->{{None,None},{"harmonic", "Fourier Series Phase"}},FrameTicks->{Automatic,Automatic},PlotRangeClipping->True,AspectRatio->0.4,ImageSize->{350,200}],
Plot[phx,{ω,1,deg},PlotRange->{-180,180},Frame->True,Axes->True,PlotRange->All,FrameLabel->{{None,None},{"harmonic", "Fourier Transform Phase"}},FrameTicks->{Automatic,Automatic},PlotRangeClipping->True,AspectRatio->0.4,ImageSize->{350,200}]
}]]],
{{ctrl,1,""},{1->"wave",2->"spectrum",3->"phase"},ControlPlacement->Top},
{{func,sin[x,1,2π,0],"function"},{sin[x,1,2π,0]->"sine",rect[x,1,2π,0]->"rectangular",tria[x,1,2π,0]->"triangular"},ControlType->PopupMenu,ControlPlacement->Left},
{{deg,5,"degree of Fourier series"},Range[20],ControlType->PopupMenu,ControlPlacement->Left},
TrackedSymbols:>{ctrl,func,deg},
SynchronousUpdating->False,SaveDefinitions->True]

CAPTION

This Demonstration shows the differences between the Fourier series and the Fourier transform. The Fourier series use the sine-cosine representation. The three functions used each have period 2 π.

THIS NOTEBOOK IS THE SOURCE CODE FROM

"Comparing Fourier Series and Fourier Transform" from the Wolfram Demonstrations Project
 http://demonstrations.wolfram.com/ComparingFourierSeriesAndFourierTransform/

Contributed by: Martin Jungwith

A full-function Wolfram Mathematica system (Version 6 or higher) is required to edit this notebook.
GET WOLFRAM MATHEMATICA »

© Wolfram Demonstrations Project & Contributors   |   Terms of Use   |   Make a new version of this Demonstration »