Phys 238
Fourier Series Representation of a Gaussian Function

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 function later.

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

Pick a particular function

Clear[f]

λ = 10; (* For plotting *)
μ = λ/2;
σ = λ/10;
f[x_] := 1.0 Exp[-(((x-μ))^2)/(2 (σ^2))]
Plot[f[x], {x, 0, λ}, PlotRange->{0,1}]

Compute the Fourier Coefficients

nmax = 20;

a0 = af[0];
a = Table[af[n], {n, 1, nmax}];
b = Table[bf[n], {n, 1, nmax}];

ListPlot[{a, b}, Joined->True, Mesh->All, PlotRange->All]

Reconstructing the original waveform

Manipulate[Plot[Evaluate[{f[x], fs[n, x], a[[n]] Cos[k[n] x]}], {x, 0, λ}, PlotRange->{-0.25, 1}], {n, 0, nmax, 1, Appearance->"Open"}]