11MAI -- Fourier series demo from Lecture 2

Problem 2.3

Our task is to find Fourier series representation of half-wawe rectified sinusoid, $$ f(t) = \begin{cases} \sin\left(\dfrac{2\pi t}{T}\right) & \text{if} \quad 0 \le t < \dfrac{T}{2} \\ 0 & \text{if} \quad \dfrac{T}{2} \le t < T \end{cases} $$ i.e. we have to find coefficients $a_0, a_1, a_2, \dots$ and $b_1, b_2, \dots$ of the formula $$ f(t) = a_0 + \sum_{k=1}^{\infty} a_k \cos k\omega_0 t + \sum_{k=1}^{\infty} b_k \sin k\omega_0 t $$ as functions of $k$,and plot the first 5 components of the series $$ f(t) = \sum_{j=0}^{\infty} \alpha_j \phi_j(t), $$ that is the functions \begin{align*} f_0(t) &= \alpha_0 \phi_0(t), \\ f_1(t) &= \alpha_1 \phi_1(t), \\ f_2(t) &= \alpha_2 \phi_2(t), \\ f_3(t) &= \alpha_3 \phi_3(t), \\ f_4(t) &= \alpha_4 \phi_4(t). \\ \end{align*}

Contrary to the lecture, I will demonstrate here the use of Python, Jupyter notebooks, SymPy and NumPy + Matplotlib. In order to do so, we need to prepare our working environment:

The Fourier series is defined w.r.t. fundamental frequency $$ \omega_0 = \frac{2\pi}{T} $$ so let us defined the corresponding symbols for future use in SymPy:

In fact, the sinusoid we work with is $$ \sin\left(\dfrac{2\pi t}{T}\right) = \sin(\omega_0 t). $$

Scaling constants

Constant $a_0$

The constant coefficient $a_0$ is defined as \begin{align*} a_0 &= \frac{1}{T} \int_{0}^{T} f(t) \,\text{d}t = \frac{1}{T} \int_{0}^{T/2} \sin(\omega_0 t) \,\text{d}t =\\ &= \frac{1}{T} \left[ -\frac{1}{\omega_0} \cos \omega_0 t \right]_0^{T/2} = \frac{1}{T} \frac{1}{\omega_0} \left( - \cos \omega_0\frac{T}{2} + 1 \right) = \frac{1}{T} \frac{T}{2\pi} \left( - \cos \frac{2\pi}{T}\frac{T}{2} + 1 \right) = \frac{1}{2\pi} \left( -\cos \pi + 1 \right) = \frac{1}{2\pi} \left( 1 + 1 \right) \\ &= \frac{1}{\pi} \end{align*}

Using SymPy we easily verify

Coefficients $a_1, a_2, \dots$

For $k\ge1$ the situation becomes more complicated. We have $$ a_k = \frac{2}{T} \int_0^{T} f(t) \cdot \cos(k \omega_0 t) \, \text{d}t = \frac{2}{T} \int_0^{T/2} \sin(\omega_0 t) \cdot (\cos k \omega_0 t) \, \text{d}t $$ which can be solved e.g. using the identity $$ 2 \cos \theta \sin \varphi = \sin (\theta+\varphi) - \sin (\theta-\varphi) $$ for $\theta=k\omega_0$ and $\varphi=\omega_0$.

This substitution results in an integral that is not difficult to compute, it's only time cosuming to write down: \begin{align*} a_k &= \frac{1}{T} \int_{0}^{T/2} 2 \sin(\omega_0 t) \cdot \cos (k \omega_0 t) \,\text{d}t \\ &= \frac{1}{T} \int_{0}^{T/2} \sin\left((k+1)\omega_0 t\right) \,\text{d}t - \frac{1}{T} \int_{0}^{T/2} \sin\left((k-1)\omega_0 t\right) \,\text{d}t \\ \frac{1}{T} \int_{0}^{T/2} \sin\bigl((k+1)\omega_0 t\bigr) \,\text{d}t &= \frac{1}{T} \left[ -\frac{1}{(k+1)\omega_0} \cos\bigl((k+1)\omega_0 t\bigr) \right]_0^{T/2} \\ &= \frac{1}{T} \frac{1}{(k+1)\omega_0} \left( - \cos (k+1)\omega_0\frac{T}{2} + 1 \right) = \frac{1}{T} \frac{T}{2(k+1)\pi} \left( - \cos \frac{2(k+1)\pi}{T}\frac{T}{2} + 1 \right) \\ &= \frac{-\cos (k+1)\pi + 1}{2(k+1)\pi} = \frac{(-1)^{k} + 1}{2(k+1)\pi} \\ \frac{1}{T} \int_{0}^{T/2} \sin\bigl((k-1)\omega_0 t\bigr) \,\text{d}t &= \frac{1}{T} \left[ -\frac{1}{(k-1)\omega_0} \cos\bigl((k-1)\omega_0 t\bigr) \right]_0^{T/2} \\ &= \frac{1}{T} \frac{1}{(k-1)\omega_0} \left( - \cos (k-1)\omega_0\frac{T}{2} + 1 \right) = \frac{1}{T} \frac{T}{2(k-1)\pi} \left( - \cos \frac{2(k-1)\pi}{T}\frac{T}{2} + 1 \right) \\ &= \frac{-\cos (k-1)\pi + 1}{2(k-1)\pi} = \frac{(-1)^{k} + 1}{2(k-1)\pi} \end{align*} Note that the second integral vanishes for $k=1$ because $\sin(0\omega_0t) = 0$, so at the end we have $$ a_1 = \frac{(-1)^{1} + 1}{2(1+1)\pi} - 0 = \frac{0}{4\pi} = 0 $$ and for $k\ge2$ $$ a_k = \frac{(-1)^{k} + 1}{2(k+1)\pi} - \frac{(-1)^{k} + 1}{2(k-1)\pi} = \frac{\left((-1)^{k} + 1\right)(k - 1 - k - 1)}{2\pi(k^2-1)} = -\frac{(-1)^{k} + 1}{\pi(k^2-1)} = \frac{(-1)^{k+1} - 1}{\pi(k^2-1)} $$ Hence, $$ a_k = \begin{cases} 0 & k=1, \\ \dfrac{(-1)^{k+1} - 1}{\pi(k^2-1)} & k\ge2. \end{cases} $$

Let's verify with SymPy:

Start with the first integral

and continue with the second one

(my version of SymPy has a slight problem with the condition for the first term, instead of $k\ne1$ the conditioning system is probably confused with both $T$ and $k$ being symbols and produces $\dfrac{2\pi(k-1)}{T}\ne0$ instead...)

Putting it together we get

And just to be sure, let's compute the complete integral for $a_k$ as well:

Coefficients $b_1, b_2, \dots$

Finally we need to compute also the scaling coefficients $b_k$: $$ b_k = \frac{2}{T} \int_0^{T} f(t) \cdot \sin(k \omega_0 t) \, \text{d}t = \frac{2}{T} \int_0^{T/2} \sin(\omega_0 t) \cdot \sin(k \omega_0 t) \, \text{d}t. $$ This can be again solved e.g. using identity $$ 2 \sin \theta \sin \varphi = \cos (\theta-\varphi) - \cos (\theta-\varphi) $$ for $\theta=k\omega_0$ and $\varphi=\omega_0$.

This substitution again results in an integral that is not difficult to compute, it's only complicated to do so. \begin{align*} b_k &= \frac{1}{T} \int_{0}^{T/2} 2 \sin\left(\omega_0 t\right) \cdot \sin\left(k \omega_0 t\right) \,\text{d}t \\ &= \frac{1}{T} \int_{0}^{T/2} \cos\left((k-1)\omega_0 t\right) \,\text{d}t - \frac{1}{T} \int_{0}^{T/2} \cos\left((k+1)\omega_0 t\right) \,\text{d}t \\ \end{align*} The first integral degenerates to unity for $k=1$ and therefore \begin{align*} \frac{1}{T} \int_{0}^{T/2} \cos(0\omega_0 t) \,\text{d}t &= \frac{1}{T} \int_{0}^{T/2} \,\text{d}t = \frac{1}{T} \Bigl[ t \Bigr]_0^{T/2} = \frac{1}{T} \frac{T}{2} \\ &= \frac{1}{2} \end{align*} For $k>1$ we have \begin{align*} \frac{2}{T} \int_{0}^{T/2} \cos\bigl((k-1)\omega_0 t\bigr) \,\text{d}t &= \frac{2}{T} \left[ \frac{1}{(k-1)\omega_0} \sin \bigl((k-1)\omega_0 t\bigr) \right]_0^{T/2} \\ &= \frac{2}{T} \frac{1}{(k-1)\omega_0} \left( \sin (k-1)\omega_0\frac{T}{2} - 0 \right) = \frac{2}{T} \frac{T}{2(k-1)\pi} \sin \frac{2(k-1)\pi}{T}\frac{T}{2} \\ &= \frac{\sin (k-1)\pi}{(k-1)\pi} = 0 \\ \frac{2}{T} \int_{0}^{T/2} \cos\bigl((k+1)\omega_0 t\bigr) \,\text{d}t &= \frac{2}{T} \left[ \frac{1}{(k+1)\omega_0} \sin \bigl((k+1)\omega_0 t\bigr) \right]_0^{T/2} \\ &= \frac{2}{T} \frac{1}{(k+1)\omega_0} \sin (k+1)\omega_0\frac{T}{2} = \frac{2}{T} \frac{T}{2(k+1)\pi} \sin \frac{2(k+1)\pi}{T}\frac{T}{2} \\ &= \frac{\sin (k+1)\pi}{(k+1)\pi} = 0 \end{align*} Hence, $$ b_k = \begin{cases} \dfrac{1}{2} & k=1, \\ 0 & k>1. \end{cases} $$

Plots

Let's first have a look at first five components of the Fourier series, i.e. at the first five scaled basis functions $\phi_0, \phi_1, \phi_2, \phi_3$, and $\phi_4$.

Looking back at the lecture notes we find \begin{align*} \alpha_0 &= \sqrt{T} a_0 = \frac{1}{\pi}\,\sqrt{T} \\ \alpha_1 &= \sqrt{\frac{T}{2}} a_1 = \frac{1}{2} \sqrt{\frac{T}{2}} \\ \alpha_2 &= \sqrt{\frac{T}{2}} b_1 = 0 \\ \alpha_3 &= \sqrt{\frac{T}{2}} a_2 = 0 \\ \alpha_4 &= \sqrt{\frac{T}{2}} b_2 = -\frac{2}{3\pi} \sqrt{\frac{T}{2}} \end{align*}

Let us compare it with the scaled harmonic components for $k=0,1,2,3,4$ from the separated formula, i.e. with the components \begin{align*} f_0(t) &= a_0, \\ f_1(t) &= a_1 \cos(\omega_0 t) + b_1 \sin(\omega_0 t), \\ f_2(t) &= a_2 \cos(2\omega_0 t) + b_2 \sin(2\omega_0 t), \\ f_3(t) &= a_3 \cos(3\omega_0 t) + b_3 \sin(3\omega_0 t), \\ f_4(t) &= a_4 \cos(4\omega_0 t) + b_4 \sin(4\omega_0 t). \\ \end{align*}

Create a vector holding function values of the original $f(t)$:

Now we have everything that is needed to plot some graphs that compare the original function with its approximation by partial sums of Fourier series.

Problem 2.4

Find the Fourier series representation for the sawtooth $$ f(t) = f(t+T) = t $$ for $-T/2 \leq t \leq T/2$.

As the function $f(t)$ is odd, the coefficients $a_{k} = 0 $. We need to calculate only coefficients $b_{k}$. Finally, we shall plot the first 5 components of the Fourier series.

We have $$ b_k = \frac{2}{T} \int_{-T/2}^{T/2} f(t) \cdot \sin(k \omega_0 t) \, \text{d}t = \frac{2}{T} \int_{-T/2}^{T/2} t \cdot \sin(k \omega_0 t) \, \text{d}t. $$ The integral cen be solved by per partes integration, i.e. using the formula $$ \int uv^\prime = uv - \int u^\prime v $$ and in our case \begin{align*} u = t \quad & \quad v^\prime = \sin(k \omega_0 t), \\ u^\prime = 1 \quad & \quad v = -\frac{1}{k \omega_0}\cos(k \omega_0 t). \end{align*} Therefore \begin{align*} b_k &= \frac{2}{T} \int_{-T/2}^{T/2} t \cdot \sin(k \omega_0 t) \, \text{d}t = \\ &= -\frac{2}{Tk \omega_0} \Bigl[ t \cos(k \omega_0 t)\Bigr]_{-T/2}^{T/2} -\frac{2}{Tk\omega_0} \int_{-T/2}^{T/2} \cos(k \omega_0 t) \, \text{d}t = \\ &= -\frac{2}{Tk \omega_0} \Bigl[ t \cos(k \omega_0 t)\Bigr]_{-T/2}^{T/2} -\frac{2}{Tk^2\omega_0^2} \Bigl[ \sin(k \omega_0 t)\Bigr]_{-T/2}^{T/2} = \\ &= -\frac{2}{Tk \omega_0} \Bigl[ \frac{T}{2}\cos\left( k\frac{2\pi}{T}\frac{T}{2}\right) +\frac{T}{2}\cos\left(-k\frac{2\pi}{T}\frac{T}{2}\right) \Bigr] -\frac{2}{Tk^2\omega_0^2} \Bigl[ \sin\left( k\frac{2\pi}{T}\frac{T}{2}\right) -\sin\left(-k\frac{2\pi}{T}\frac{T}{2}\right) \Bigr] = \\ &= -\frac{1}{k \omega_0} \Bigl[ \cos\left( k\pi\right) +\cos\left(-k\pi\right) \Bigr] -\frac{2}{Tk^2\omega_0^2} \Bigl[ \sin\left( k\pi\right) -\sin\left(k\pi\right) \Bigr] = \\ &= -\frac{2\cos\left(k\pi\right)}{k\omega_0} - 0 = \\ &= -\frac{2(-1)^k}{k\omega_0} = -\frac{2(-1)^k}{k}\frac{T}{2\pi} = -\frac{T(-1)^k}{k\pi} \end{align*} In fact, the integral $$ \int_{-T/2}^{T/2} \cos(k \omega_0 t) \, \text{d}t $$ is an integral of a hramonic function over its period so we could have immediately concluded that it is equal to zero.

Before checking that we have computed it correctly, some symbolic variables have to be reset:

And now we can verify

Plots

Let's first have a look at first five components of the Fourier series, i.e. at the first five scaled basis functions $\phi_0, \phi_1, \phi_2, \phi_3$, and $\phi_4$.

Looking back at the lecture notes we find \begin{align*} \alpha_0 &= \sqrt{T} a_0 = 0 \\ \alpha_1 &= \sqrt{\frac{T}{2}} a_1 = 0 \\ \alpha_2 &= \sqrt{\frac{T}{2}} b_1 = \sqrt{\frac{T}{2}}\frac{T}{\pi} \\ \alpha_3 &= \sqrt{\frac{T}{2}} a_2 = 0 \\ \alpha_4 &= \sqrt{\frac{T}{2}} b_2 = -\sqrt{\frac{T}{2}}\frac{T}{2\pi} \end{align*}

Let us compare it with the scaled harmonic components for $k=0,1,2,3,4$ from the separated formula, i.e. with the components \begin{align*} f_0(t) &= a_0, \\ f_1(t) &= a_1 \cos(\omega_0 t) + b_1 \sin(\omega_0 t), \\ f_2(t) &= a_2 \cos(2\omega_0 t) + b_2 \sin(2\omega_0 t), \\ f_3(t) &= a_3 \cos(3\omega_0 t) + b_3 \sin(3\omega_0 t), \\ f_4(t) &= a_4 \cos(4\omega_0 t) + b_4 \sin(4\omega_0 t). \\ \end{align*}

Create a vector holding function values of the original $f(t)$:

Now we have everything that is needed to plot some graphs that compare the original function with its approximation by partial sums of Fourier series.