### 3.5 Obtain Fourier Series coeﬃcients for a periodic function

3.5.1 Example 1
3.5.2 Example 2
3.5.3 Example 3

#### 3.5.1 Example 1

3.5.1.1 Mathematica

Given a continuous time function $$x\left ( t\right ) =\sin \left (\frac {2 \pi }{T_0} t\right )$$, ﬁnd its Fourier coeﬃcients $$c_{n}$$, where (Using the default deﬁnition) $x\left ( t\right ) =\sum _{n=-\infty }^{\infty }c_{n}e^{j\omega _{0}nt}$ and $c_{n}=\frac {1}{T_{0}}\int _{-\frac {T_{0}}{2}}^{\frac {T_{0}}{2}}x(t)e^{-j\omega _{0}nt}dt$ Where $$T_{0}$$ is the fundamental period of $$x\left ( t\right )$$ and $$\omega _{0}=\frac {2\pi }{T_{0}}$$.

Notice that in Physics, sometimes the following deﬁntions are used instead of the above

$x\left ( t\right ) =\frac {1}{\sqrt {T_0}} \sum _{n=-\infty }^{\infty }c_{n}e^{j\omega _{0}nt}$ and $c_{n}= \frac {1}{\sqrt {T_0}} \int _{-\frac {T_{0}}{2}}^{\frac {T_{0}}{2}}x(t)e^{-j\omega _{0}nt}dt$

##### 3.5.1.1 Mathematica
Clear[T0, w0]
sol = FourierSeries[Sin[2 Pi/T0 t], t, 3, FourierParameters -> {1, 2 Pi/T0}]



$\frac {1}{2} i e^{-\frac {2 i \pi t}{\text {T0}}}-\frac {1}{2} i e^{\frac {2 i \pi t}{\text {T0}}}$

data = Table[{i,
FourierCoefficient[Sin[2 Pi/T0 t], t, i,
FourierParameters -> {1, 2 Pi/T0}]}, {i, -5, 5}];
Grid[Insert[data, head, 1], Frame -> All]



mag = data;
mag[[All,2]] = Map[Abs[#]&,data[[All,2]]];
ListPlot[mag,AxesOrigin->{0,0},
Filling->Axis,
FillingStyle->{{Thick,Red}},
AxesLabel->{"n","|Subscript[c, n]|"}]



phase = data;
phase[[All,2]]=Map[Arg[#]&,
data[[All,2]]]* 180/Pi;
ListPlot[phase,AxesOrigin->{0,0},
Filling->Axis,
FillingStyle->{{Thick,Red}},
AxesLabel->{"n","Phase Subscript[c, n] degrees"},
ImageSize->300]



#### 3.5.2 Example 2

3.5.2.1 Mathematica

Find Fourier Series for $$f(t) = \cos (\omega _0 t) + \sin ^2(\omega _0 t)$$

##### 3.5.2.1 Mathematica
T0 = 2 Pi;
w0 = (2 Pi)/T0;
f[t_] := Cos[w0 t] + Sin[w0 t]^2;
Plot[f[t], {t, -10 Pi, 10 Pi}, PlotRange -> All, ImageSize -> 300]



sol = FourierSeries[f[t],t,3, FourierParameters->{1,(2 Pi)/T0}]



$\frac {e^{-i t}}{2}+\frac {e^{i t}}{2}-\frac {1}{4} e^{-2 i t}-\frac {1}{4} e^{2 i t}+\frac {1}{2}$

data = Table[{i, FourierCoefficient[f[t],t,i, FourierParameters->{1,2 Pi/T0}]},{i,-5,5}];



mag = data;
mag[[All,2]] = Map[Abs[#]&,data[[All,2]]];
ListPlot[mag,AxesOrigin->{0,0},
Filling->Axis,
FillingStyle->{{Thick,Red}},
AxesLabel->{"n","|Subscript[c, n]|"},
ImageSize->300]



Plot the phase

phase = data;
phase[[All, 2]] = Map[Arg[#] &,
data[[All, 2]]]* 180/Pi;
ListPlot[phase, AxesOrigin -> {0, 0},
Filling -> Axis,
FillingStyle -> {{Thick, Red}},
AxesLabel -> {"n", "Phase cn degrees"}]



#### 3.5.3 Example 3

3.5.3.1 Mathematica

Find Fourier series for periodic square wave

##### 3.5.3.1 Mathematica
f[x_] := SquareWave[{0,1},(x+.5)/2] ;
Plot[f[x],{x,-6,6},Filling->Axis, ImageSize->300]



T0   = 2;
sol = Chop[FourierSeries[f[t],t,6,  FourierParameters->{1,(2 Pi)/T0}]]



$0.5 +0.31831 e^{-i \pi t} + 0.31831 e^{i \pi t} - 0.106103 e^{-3 i \pi t} - 0.106103 e^{3 i \pi t} + 0.063662 e^{-5 i \pi t}+0.063662 e^{5 i \pi t}$

data = Chop[Table[{i,FourierCoefficient[f[t],t,i, FourierParameters->{1,2 Pi/T}]},{i,-8,8}]];



data=Table[{i,FourierCoefficient[f[t],t,i,  FourierParameters->{1,2 Pi/T}]},{i,-20,20}];
mag = data;
mag[[All,2]]=Map[Abs[#]&,data[[All,2]]];
ListPlot[mag,AxesOrigin->{0,0},
Filling->Axis,
FillingStyle->{{Thick,Red}},
PlotRange->All,
AxesLabel->{"n","|Subscript[c, n]|"},
ImageSize->300]



Show phase

phase = data;
phase[[All,2]]=Map[Arg[#]&,data[[All,2]]]* 180/Pi;
ListPlot[phase,AxesOrigin->{0,0},
Filling->Axis,
FillingStyle->{{Thick,Red}},
AxesLabel->{"n","Phase cn degrees"},
ImageSize->300]