Académique Documents
Professionnel Documents
Culture Documents
MATLAB Tutorial #6
MATLAB can be used to find and plot the trigonometric Fourier series of a periodic function.
t
-1 -2 3 6 10
The signal, f(t) has a period, T = 7. Thus, wo = 2π/7, and the Fourier series expansion is:
∞ ∞
f (t) = a0 + ∑ an cos nω 0 t + ∑ bn sin nω 0 t
n=1 n=1
where:
ω0 10
a0 = ∫
2π T
f (t)dt =
7
ω0 2 ⎡ ⎛ 6π n ⎞ ⎛ 2π n ⎞ ⎛ 12π n ⎞ ⎤
an =
π ∫
T
f (t)cos nω 0 t dt =
π n ⎢⎣
3sin ⎜
⎝ 7 ⎠ ⎟ + 2 sin ⎜
⎝ 7 ⎠ ⎟ − sin ⎜⎝ ⎟
7 ⎠ ⎥⎦
ω0 2 ⎡ ⎛ 6π n ⎞ ⎛ 2π n ⎞ ⎛ 12π n ⎞ ⎤
bn =
π ∫
T
f (t)sin nω 0 t dt =
π n ⎢⎣
−3cos ⎜
⎝ 7 ⎠ ⎟ + 2 cos ⎜
⎝ 7 ⎠ ⎟ + cos ⎜⎝ ⎟
7 ⎠ ⎥⎦
resulting in:
Note that we use the “stem” function to plot these coefficients since they are discrete values and
not a continuous function.
Matrices and vectors can be multiplied in two ways, using MATLAB. For vectors x and y as
shown below:
>> x = [ 1 2 3 4];
>> y = [ 5 6 7 8];
We can perform a term – by – term multiplication:
>> x.*y
ans =
5 12 21 32
When using the term-by-term multiplication, the vectors/matrices being multiplied must be the
same size, since corresponding terms are multiplied. This is not what we typically think of as
matrix multiplication. If we were to do a regular multiplication of these two vectors:
>> x * y
??? Error using ==> mtimes
Inner matrix dimensions must agree.
Similarly, we can transpose x converting it to a 4 x 1 vector. Now the product xTy is defined
since x has one column and y has one row. The product will be 4 x 4 as shown below:
>> x'*y
ans =
5 6 7 8
10 12 14 16
15 18 21 24
20 24 28 32
Say we want to approximate a signal with its Fourier series representation. We need to evaluate
a sum of products at various values of t similar to the term:
10
∑a n cos 4nt
n=1
If we want to plot this summation as a function of t for 0 ≤ t ≤ 1, we define the values of n and t
using:
>> n = 1:10;
>> t = 0: .1: 1;
The resulting n vector is 1 x 10 and t is 1 x 11. In order to get all of the possible products of n*t,
we can perform a vector multiplication:
>> n' * t
ans =
0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 1.0000
0 0.2000 0.4000 0.6000 0.8000 1.0000 1.2000 1.4000 1.6000 1.8000 2.0000
0 0.3000 0.6000 0.9000 1.2000 1.5000 1.8000 2.1000 2.4000 2.7000 3.0000
0 0.4000 0.8000 1.2000 1.6000 2.0000 2.4000 2.8000 3.2000 3.6000 4.0000
0 0.5000 1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000
0 0.6000 1.2000 1.8000 2.4000 3.0000 3.6000 4.2000 4.8000 5.4000 6.0000
0 0.7000 1.4000 2.1000 2.8000 3.5000 4.2000 4.9000 5.6000 6.3000 7.0000
0 0.8000 1.6000 2.4000 3.2000 4.0000 4.8000 5.6000 6.4000 7.2000 8.0000
0 0.9000 1.8000 2.7000 3.6000 4.5000 5.4000 6.3000 7.2000 8.1000 9.0000
0 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000
where the resulting 1 x 11 vector corresponds to the summation evaluated at each value of t. We
will use this technique for evaluating the Fourier series approximations below.
A simple approximation to the waveform f(t) can be found if we include the terms from 0≤n≤2.
In this case:
2 2
f (t) ≈ a0 + ∑ an cos nω 0 t + ∑ bn sin nω 0 t
n=1 n=1
>> n=1:20;
>> an=(2./(pi*n)).*(3*sin(6*pi*n/7)+2*sin(2*pi*n/7)- …
sin(12*pi*n/7));
>> bn=(2./(pi*n)).*(-3*cos(6*pi*n/7)+2*cos(2*pi*n/7) …
+cos(12*pi*n/7));
>> a=[a0, an];
>> b=[0, bn];
>> n=[0,n];
>> fapprox=a*cos(2*pi*n'*t/7)+b*sin(2*pi*n'*t/7);
>> fexact=4*(t>=-1)-6*(t>=3)+2*(t>=6);
>> plot(t,fexact,t,fapprox)