1. Definition of the Fourier series
The Fourier coefficients of a square wave signal are: Cn = -jnπ (1 – (- 1) n)
The Fourier series is written: u (t) = ∑n = 1∞2ReCnexpjn2πTt
The Fourier coefficients are defined by a function:
cnCreneau[n_]:=-I/(n*Pi)*(1-(-1)^n);
The following function displays the spectrum (modulus of cn) in decibel:
plotSpectre[cn_, nmax_] := Module[{spectre}, spectre = Table[Line[{{n, -80}, {n,20*Log[10,Abs[cn[n]]+10^(-4)]}}], {n, 1, nmax}]; Return[Graphics[{RGBColor[1, 0, 0],spectre},Frame->True,AspectRatio->0.7, PlotRange -> {{0, nmax},{-80,0}}, FrameLabel -> {"n", "20*log(|cn|)"}]]; ]
Show[plotSpectre[cnCreneau,50]]

Calculation function of the partial sum of the Fourier series (period 1):
serie[cn_,nmax_,t_]:=Sum[2*Re[cn[n]*Exp[I*2*Pi*n*t]],{n,1,nmax}];
Partial sum at N = 20:
Plot[serie[cnCreneau,20,t],{t,0,1},AxesLabel->{"t","u"}]

Partial sum at N = 100:
Plot[serie[cnCreneau,100,t],{t,0,1},AxesLabel->{"t","u"}]

2. First order low pass filter
The transfer function of a first order low pass filter is: H (f) = 11 + jffc
where fc is the cutoff frequency.
Consider the case where fc = 10. We define the transfer function then we calculate the new Fourier coefficients, noting that the index n corresponds to the frequency:
h[f_]:=1/(1+I*f/10); cnSortie[n_]:=cnCreneau[n]*h[n];
Here is the new spectrum
Show[plotSpectre[cnSortie,50]]

and the partial sum for N = 100
Plot[serie[cnSortie,100,t],{t,0,1},AxesLabel->{"t","u"}]

In the case fc = 0.1, the harmonics of the square wave are all in the integrating domain of the filter:
h[f_]:=1/(1+I*f/0.1); cnSortie[n_]:=cnCreneau[n]*h[n];
Plot[serie[cnSortie,100,t],{t,0,1},AxesLabel->{"t","u"}]
