How to build a simple waveform using the Fourier series (MATLAB demo).

Ashan Serasinghe
4 min readFeb 18, 2021

--

Fourier series is one of the beautiful mathematical approaches in the mathematics world. It is heavily used in the field of signal processing. According to the Fourier series, we can build any periodic signal (a function) by adding sine (or cosine) waves together, which have different amplitudes and different frequencies. Here I am going to demonstrate how we can build a square wave and sawtooth wave using the Fourier series.

Here I am using MATLAB R2018a. But you can use any version you have. The codes will work regardless of the version.

First of all, let’s look at the definition of the Fourier series,

Any periodic signal x(t) can be written as,

Consider the square wave given below. I am going to find Xₖ (Fourier Series coefficient) for that square wave.

According to the Fourier series,

Therefore, we can write x(t) as:

If the Fourier series is correct, we should be able to make x(t) (the square wave) using the above equation. Let’s check it using some MATLAB codes. For simplicity, we will take k = [-20,20]

clear;syms X(t,k)   % define our common term X, that is a faunction
% of two variables
size_of_k = 20;K1 = -1*size_of_k:size_of_k; % range of k, theoritically that is
%(-)infinity to(+)infinity
K = nonzeros(K1); % remove zero from k, otherwise it may give
% "Division by zero" error with 'symengine'
K = K';x = 0.5; % manually compute x0, X for k = 0X(k) = (1/2)*sinc(k/2)*exp(1j*pi*k*t/2); % calculated Xk
for k=K
x = x + X(k); % make x(t) with adding terms by terms
end
fplot(x,[-10,10]); % plot the function x(t)xlabel('x');
ylabel('y');
title('x(k) = (sin(k\pi/2)*exp(1j\pi k(t-1)/2))/(\pi k)')
grid on;
ax = gca; % set the axis to the origin
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
wave for k = [-20 ,+20]

If we increase the range of k, we can get a much nicer waveform. Let’s make k = [-100,+100]

wave for k = [-100 , +100]

So you can see that when k reaches infinity the waveform takes the required shape.

Then we will consider a sawtooth waveform shown below.

As in the previous case, we can find the Xₖ for the above waveform. (if you want, you can differentiate the function ones to simplify the integration ). Then Xₖ,

Therefore,

Let’s check this with MATLAB. Here I am using the previous code and only the X(k) has changed.

clear;syms X(t,k)   % define our common term X, that is a faunction
% of two variables
size_of_k = 20;K1 = -1*size_of_k:size_of_k; % range of k, theoritically that is
%(-)infinity to(+)infinity
K = nonzeros(K1); % remove zero from k, otherwise it may give
% "Division by zero" error with 'symengine'
K = K';x = 0.5; % manually compute x0, X for k = 0X(k) = (exp(1j*k*pi*t))*1j/(2*pi*k); % calculated Xkfor k=K
x = x + X(k); % make x(t) with adding terms by terms
end
fplot(x,[-10,10]); % plot the function x(t)xlabel('x');
ylabel('y');
title('x(k) = (exp(1j*k*\pi*t))*1j/(2*\pi*k)')
grid on;
ax = gca; % set the axis to the origin
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';

Resulting waveform for k = [-20,20], as below:

wave for k = [-10,10]

For k = [-100 , 100]

wave for k = [-100 , 100]

As shown in those two examples we can build any periodic function using the Fourier series. What we want to do is finding the Fourier series coefficient Xₖ for the particular function (wave).

It is possible to find Xₖ using sampled data of x(t), even without the exact function of x(t). Then we have to use the numerical integration method to do the integration part.

--

--

Ashan Serasinghe

Undergraduate in Electrical and Electronic Engineering, University of Peradeniya, Sri Lanka