P2 Fourier Series
P2 Fourier Series
A periodic signal (with certain properties and restrictions – see the course notes)
f (t ) f (t T ), t , where T 2 / 0 is the period, can be expressed as a trigonometric Fourier
series as follows:
a0
f (t) ak cos(k0t ) bk sin(k0t ) (1.1)
2 k 1
2
T T
ak f (t ) cos(k0t )dt (1.3)
2
T T
bk f (t )sin(k0t )dt (1.4)
In this lesson our objective is to develop a Matlab script that determines these coefficients for
a given periodic signal, by making use of the function integral() (see Matlab’s help). As an example
we will use the switching function, f c (t ) , but the code can be adapted for any periodic signal, given
that it’s function is previously designed.
We start by defining the fundamental frequency (thus determining the corresponding period)
and we continue expressing the time base for the function that will be decomposed into a
trigonometric Fourier series (file fourierSeries.m):
f0 = 50;
step = T/100;
% We define the time base from -T to 2*T with the chosen step:
t=-T:step:2*T;
1
In order to determine the Fourier coefficients, first we must define the periodic signal as a
Matlab function. In our case we define f c (t ) as (file fc.m):
function result = fc(t,T)
% The function returns the amplitude values for the switching function
% defined as 1 from 0 la T/2 and -1 from T/2 la T)
% The input parameters are:
% t – the time base vector which contains the time values at which the
% function will be evaluated. The result will contain the amplitudes
% corresponding to the time base vector
% T – the period of the signal – should be a number, not a vector or matrix
result = zeros(1,length(t));
result = (-1)ones(size(t));
result( mod(t,T)<T/2 ) = 1;
end
Next, we determine the DC component using (1.2.) and Matlab’s integral() function :
a0over2 = 1/T * integral(@(t)fc(t,T),0,T);
where fun is the function, in our case it will be @(t)fc(t,T),which means that we will integrate
fc(t) with respect to t. In order to define the interval on which the integration will be determined, we
will specify xmin and xmax as 0 and T.
Next we will determine the first N coefficients for the trigonometric Fourier series:
% We determine the first N coefficients for the trigonometric Fourier
% series
N = 10;
a=zeros(1,N);
b=zeros(1,N);
for k=1:N
a(k) = 2/T * integral(@(t)(fc(t,T).*cos(k*omega0*t)),0,T);
b(k) = 2/T * integral(@(t)(fc(t,T).*sin(k*omega0*t)),0,T);
2
end
a
b
After we run the script we will see that the values for a k are very small (less than 10-15), even
though in theory they should be 0. The same is true for bk values. In order to force these values to 0
we will establish a threshold under which all the values will be considered zero (in our case the
threshold will be 1010 ). This operation is necessary because in the following steps, based on the ratio
between a k and bk , we will need to compute another parameter. Thus, if both a k and bk are very
close to zero but not zero, than their ratio will be different from 0 and furthermore, it will be random
value, which will alter the parameters computed based on this ratio.
thr = 10^-10;
if abs(a0over2)<thr
a0over2 = 0;
end
if abs(a(k))<thr
a(k) = 0;
end
if abs(b(k))<thr
b(k) = 0;
end
3
3. The harmonic Fourier series
For any periodic signal f (t ) f (t T ), t , with the period T , we can express the harmonic
Fourier series as follows:
f (t ) A0 Ak cos k0t k (1.5)
k 1
The coefficients of the harmonic Fourier series can be determined by using the trigonometric
Fourier series coefficients:
a0
A0 (1.6)
2
bk
arctan , ak 0
ak
k (1.8)
arctan bk , a 0
k
ak
We will modify the previous program in order to determine the harmonic Fourier series
coefficients:
%% The harmonic Fourier series
N = 100;
a=zeros(1,N);
b=zeros(1,N);
A=zeros(1,N+1);
for k=1:N
% We first determine the trigonometric series coefficients
a(k) = 2/T * integral(@(t)(fc(t,T).*cos(k*omega0*t)),0,T);
b(k) = 2/T * integral(@(t)(fc(t,T).*sin(k*omega0*t)),0,T);
if abs(a(k))<10^-10
a(k) = 0;
end
if abs(b(k))<10^-10
b(k) = 0;
end
4
% We determine the amplitude of the k-th harmonic
A(k+1) = sqrt(a(k)^2+b(k)^2);
% We determine the phase of the k-th harmonic
if a(k)==0 && b(k)==0
% If both ak and bk are 0 then the phase will be 0
phi(k+1) = 0;
else
% We determine the phase according to ak’s sign
% The function atan() is arctangent expressed in radians, if we
% want to determine the angle in degrees we should use atand()
if a(k)>=0
phi(k+1) = -atan(b(k)/a(k));
else
phi(k+1) = -atan(b(k)/a(k))-pi;
end
end
end
subplot(2,1,2);
stem((0:N)*omega0,phi);
xlabel('Frequency [rad/s]');
ylabel('Phase \phi_k');
title('Phase spectrum');
For any periodic signal f (t ) f (t T ), t , with the period, we can express the complex
Fourier series as follows:
f (t ) C n e
n
0
jn0t
(1.9)
We can also determine the complex Fourier series coefficients from the trigonometric or
harmonic series as follows:
an b
C n0 j n ,n 1 (1.11)
2 2
a0
C 0 (1.12)
2
5
An
C n0 ,n 1 (1.13)
2
Next we will determine the complex Fourier series by using the formula (1.10).
Useful Matlab functions and constants:
1j % the complex unit j = sqrt(-1)
real(x) % returns the real part of the complex number x
imag(x) % returns the imaginary part of the complex number x
abs(x) % returns the absolute value for the complex number x
angle(x) % returns the argument in radians for the complex number x
subplot(2,1,2);
stem((-N:N)*omega0,angle(C));
xlabel('Frequency \omega [rad/s]');
ylabel('Phases arg\{C(n\omega_0)\}');
title('Phase spectrum');
6
5. Power and bandwidth of periodic signals
The total power for a periodic signal can be determined with the formula:
1
f t dt
2
Pt
TT (1.16)
If the signal is real, we can also determine the total power by using the trigonometri, harmonic
or complex Fourier series coefficients:
a2 ak2 bk2 A 2
k
Pt 0 A02 C 2 0 2 C (n0 )
k 1 k 1 2
(1.17)
4 2 2 k 1
The bandwidth for a periodic signal is defined as the frequency range in which the coefficients
are different from zero. For most periodic signals the bandwidth is infinite, Bw 0, , however in
practice one cannot work with infinity. For this reasons, the energetic bandwidth was introduced as
the frequency interval where 99% of the total power is concentrated.
In order to determine the energetic bandwidth first we have to determine the total power and
then compute P99 0.99 Pt . The last part is achived by considering as many components as necessary
so that we reach P99 :
N
Ak2
PN A02 (1.18)
k 1 2
The Matlab code that determines the power and the energetic bandwidth for a periodic signal
is:
% Total power
Pt = 1/T*integral(@(t)fc(t,T).^2,0,T)
% 99% of the total power
P99 = 0.99*Pt
% We add the power of the DC component
PN = A(1)^2;
k = 1;
start = 0;
while PN<P99
% if in the beginning the power is 0 this means that the DC component is
% 0 so we must shift the starting point of the energetic bandwidth
% interval
if PN==0;
start = k;
end
PN = PN + A(k+1)^2/2;
k = k+1;
end
stop = k-1;
end
Next we will see what is the effect of different clip values on the harmonic Fourier series. The
code for displaying the clipped sine and the spectrum is as follows:
Clip = 0.5;
figure(1);
subplot(3,1,1);
plot(t,clipped_sine(t,T,clip));
axis([min(t), max(t), -1, 1]);
xlabel('Time [s]');
ylabel('Amplitude');
title('The clipped signal');
subplot(3,1,2);
% AdB = 20*log10(A/0.001);
% AdB(find(AdB<0))=0;
stem((0:N)*omega0,A);
% axis([0, N*omega0, 0, 60]);
8
xlabel('Frequency \omega [rad/s]');
ylabel('Amplitudes A_k');
title('Amplitude spectrum');
subplot(3,1,3);
stem((0:N)*omega0,phi);
xlabel('Frecquency \omega [rad/s]');
ylabel('Phase \phi_k');
title('Phase spectrum');
Figure 1. The harmonic Fourier series for a sine clipped at 0.5 of the amplitude
View the amplitude and phase spectrum for clip=1:-0.1:0.1. What happens when the signal is
further clipped?
7. Homework
A. Generate a periodic signal according to the index of the letters in the English alphabet of your
first name. As example, for the name ”VICTOR” we will have the following values for the
letter indexes:
Letter V I C T O R
Index 22 9 3 20 15 18
9
With the index values generate a periodic signal by dividing the period in equal intervals,
where the number of intervals corresponds to the number of letters in your name. Then for
each interval the amplitude will be equal to the index value of the corresponding letter. For
the previous example the signal described for one period is displayed in Figure 2.
a. Create a Matlab function which returns the periodic signal generated by your first name.
b. In another Matlab script display the periodic function on at least 3 periods and determine
the trigonometric, harmonic and complex Fourier series for at least 100 harmonics.
c. Display the amplitude and phase spectrum for the harmonic and complex Fourier series
d. Determine the total power and energetic bandwidth of the periodic signal
Note: point a. will consist of a Matlab function, points b., c. and d. can be solved in one Matlab
script. All matlab script files should be in the same folder.
B. Let us consider the periodic signal xt xt T , where T is the signal’s period. The signal
t
is described over a period as xt exp , t 0, T .
T
a. Design a Matlab function to generate x t
b. In a separate script plot at least 3 periods of the signal (e.g. t T , 2T ) and then
determine the trigonometric, harmonic and complex Fourier series for at least 100
harmonics.
c. Display the amplitude and phase spectrum for the complex Fourier series.
n 7
d. If we approximate x t with xa t Cn e o
jn0t
, determine the approximation
n 7
error defined as e(t ) x(t ) xa (t ) and compute the power for the error
T
1
Pe
2
e(t ) dt . (solve this task in the same script you designed for b. and c.)
T 0
e. Display the graphic of xa t
10