Phys 238
Fourier Series Representation of Various Functions

Clear["Global`*"]

Fourier Expansion

Given f[x] periodic in [0, λ], the Fourier series representation (up to nmax terms) is given by

k[n_, λ_] := n 2 π / λ
fs[nmax_, x_, λ_] := a0/2 + Sum[a[[n]]Cos[k[n, λ] x] + b[[n]] Sin[k[n, λ] x], {n, 1, nmax}]

Define functions to compute the a and b coefficients.  We’ll calculate them all at once, and then build the series later.

Consider a series of pulses separated by a wavelength λ

pulse[x_, λ_] := UnitStep[x] UnitStep[4-x] + UnitStep[x-λ]UnitStep[(λ+4) -x]

af[n_, λ_] := (2/λ) NIntegrate[Cos[k[n, λ]x]pulse[x, λ], {x, 0, λ}]
bf[n_,λ_] := (2/λ) NIntegrate[Sin[k[n, λ] x] pulse[x, λ], {x, 0, λ}]

Plot[pulse[x, 10], {x, 0, 20}]

Compute the Fourier Coefficients

Off[NIntegrate::ncvb];Off[NIntegrate::slwcon]

showFT[λ_] := Module[{nmax, a0, a, b, c, w},
w = 4 (* Pulse width; fixed above *);
nmax = Ceiling[λ];
a0 = af[0, λ];
a = Table[af[n, λ], {n, 1, nmax}];
b = Table[bf[n, λ], {n, 1, nmax}];
c = Table[Sqrt[a[[n]]^2 + b[[n]]^2], {n, 1, nmax}];
fplot = Plot[pulse[x, λ], {x, 0, 80}, PlotStyle -> Thick, AxesLabel->{"x", "f(x)"}, LabelStyle->Larger];cplot = ListPlot[Table[{ n/λ, c[[n]]}, {n, 1, Length[c]}],Filling -> Axis, FillingStyle -> Black, PlotRange->All, AxesLabel->{"k/2π", "Amplitude"},  LabelStyle->Larger];
GraphicsRow[{fplot, cplot}, ImageSize->800]
]

Manipulate[showFT[ L], {{L, 10}, 4, 80, 2, Appearance-> "Open"}]