Periodic functions are represented as a sum of sine and cosine terms using the Fourier Series, a basic idea in mathematics and engineering. This detailed tutorial will assist you in using and comprehending the Fourier Series in MATLAB. After completing this lesson, you will be capable of:
- Dissect a periodic function into its constituent frequency elements.
- Determine the coefficients of the Fourier series.
- Use MATLAB to reconstruct and display the original function.
What You Will Learn
- Mathematical Foundation: Recognize the elements of the Fourier Series expression.
- MATLAB Implementation: To calculate the coefficients of the Fourier Series and reconstruct the function, write MATLAB code.
- Visualization: Create a plot and contrast the original function with its approximation using the Fourier Series.
- Practical Exercise: Use the ideas to analyze typical waveforms, such as a pulse wave.
Step-by-Step Guide
Step 1: Understand the Fourier Series Formula
The Fourier Series represents a periodic function f(t) with period T as:
\begin{equation} f(t) = a_0 + \sum_{n=1}^{\infty}\left(a_n cos\left(\frac{2\pi nt}{T}\right) + b_n sin\left(\frac{2\pi nt}{T}\right)\right) \end{equation}
Where:
- \( a_0 \) is the average value of the function.
- \( a_n \) and \( b_n \) are the Fourier coefficients for the cosine and sine terms, respectively.
- \( T\) is the period of the function.
The coefficients are calculated using the following integrals:
\begin{equation} a_0 = \frac{1}{T}\int_{0}^{T} f(t) \,dt \end{equation}
\begin{equation} a_n = \frac{2}{T}\int_{0}^{T} f(t)cos\left(\frac{2\pi nt}{T}\right) \,dt \end{equation}
\begin{equation} a_n = \frac{2}{T}\int_{0}^{T} f(t)sin\left(\frac{2\pi nt}{T}\right) \,dt \end{equation}
Step 2: Compute the Fourier Series Coefficients Mathematically

For a periodic pulse waveform shown in Figure 1, the Fourier series coefficients can be calculated as:
\begin{equation} T = 10\pi, \omega_0 = \frac{2\pi}{T} = \frac{2\pi}{10\pi} = 1/5 \end{equation}
\begin{equation} a_0 = \frac{1}{T}\int_{0}^{T} f(t) \,dt = \frac{1}{10\pi}\int_{-\pi}^{\pi} 1 \,dt \\ = \frac{1}{10\pi}|t|_{-\pi}^{\pi} = \frac{1}{10\pi}\times 2\pi = \frac{1}{5}\end{equation}
\begin{equation} a_n = \frac{2}{T}\int_{0}^{T} f(t)cos\left(\frac{2\pi nt}{T}\right) \,dt = \frac{2}{10\pi}\int_{-\pi}^{\pi}cos\left(\frac{nt}{5}\right) \,dt \\ = \frac{1}{5\pi}\left|\frac{sin\left(\frac{nt}{5}\right)}{n/5}\right|_{-\pi}^{\pi} =\frac{1}{5\pi}\left(\frac{\sin{\left(\frac{n\pi}{5}\right)}+\sin{\left(\frac{n\pi}{5}\right)}}{\frac{n\pi}{5}}\right)=\frac{1}{5\pi}\times\frac{10}{n\pi}\sin{\left(\frac{n\pi}{5}\right)} \\ =\frac{2}{n\pi^2}\sin{\left(\frac{n\pi}{5}\right)} \end{equation}
\begin{equation} b_n=\frac{2}{T}\int_{0}^{T}{f\left(t\right)\sin{\left(\frac{2n\pi t}{T}\right)}\ dt}=\frac{2}{10\pi}\int_{-\pi}^{\pi}{\sin{\left(\frac{nt}{5}\right)}\ dt} \end{equation}
\begin{equation} =\frac{1}{5\pi}\left|-\frac{\cos{\left(\frac{nt}{5}\right)}}{\frac{n\pi}{5}}\right|_{-\pi}^\pi =\frac{1}{5\pi}\left(\frac{-\cos{\left(\frac{n\pi}{5}\right)}+\cos{\left(\frac{n\pi}{5}\right)}}{\frac{n\pi}{5}}\right) = 0 \end{equation}
Step 3: Reconstruct Fourier Series using MATLAB
close all;clear all;clc; t = -20*pi:0.001:20*pi; % Time Vector a0 = 1/5; % a0 value w0 = 1/5; % omega_0 value n = 1; % Initial value of n % Add dc component (a0) to the xN vector initially xN = a0*ones(1, length(t)); while(n <= 1000); xN = xN + (2*sin(n*pi/5)/(n*pi^2))*cos(w0*n*t); % bn is zero plot(t,xN, 'LineWidth',2) % plot the reconstructed signal title(['Reconstructed Signal for ', num2str(n)]) grid on xlabel('t (seconds)'), ylabel('x_N(t)') n = n + 1; pause end

A visual demonstration of the reconstruction of the given function for each addition of the Fourier series coefficient in the reconstructed function is shown below:
In this tutorial, we explored the fascinating world of the Fourier Series and learned how to implement it in MATLAB from scratch. By breaking down the mathematical foundation and walking through the step-by-step implementation, we successfully approximated periodic functions like pulse wave.