0% found this document useful (0 votes)
125 views7 pages

Trigonometric Fourier Series: ECE 350 - Linear Systems I MATLAB Tutorial #6

The document discusses using MATLAB to find and plot the trigonometric Fourier series of a periodic function. It provides an example of a periodic waveform and calculates its Fourier series expansion. MATLAB code is shown to calculate the Fourier coefficients and plot the original function alongside approximations using increasing numbers of Fourier terms. Including more terms improves the approximation.

Uploaded by

Cat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views7 pages

Trigonometric Fourier Series: ECE 350 - Linear Systems I MATLAB Tutorial #6

The document discusses using MATLAB to find and plot the trigonometric Fourier series of a periodic function. It provides an example of a periodic waveform and calculates its Fourier series expansion. MATLAB code is shown to calculate the Fourier coefficients and plot the original function alongside approximations using increasing numbers of Fourier terms. Including more terms improves the approximation.

Uploaded by

Cat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

ECE 350 – Linear Systems I

MATLAB Tutorial #6

Trigonometric Fourier Series

MATLAB can be used to find and plot the trigonometric Fourier series of a periodic function.

Trigonometric Fourier Series


For the periodic waveform shown below:
f(t)
4

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 ⎠ ⎥⎦

Using MATLAB we can find and plot these coefficients as follows:


>> clear
>> n=1:20;
>> a0=10/7;
>> 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];
>> subplot(2,1,1)
>> stem(0:20, a)
>> subplot(2,1,2)
>> stem(0:20, b)

resulting in:

Note that we use the “stem” function to plot these coefficients since they are discrete values and
not a continuous function.

Review of Matrix/Vector Multiplication

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.

We get an error message. Recall that if a matrix/vector, A, is m (rows) x k (columns), and a


matrix/vector, B, is n x p, the product, A*B, is only defined if k = n (number of columns in the
first matrix equals the number of rows in the second), and the resulting product will be m x p. In
our example above, both x and y are 1 x 4, thus the multiplication is not defined and an error
message is obtained. If we were to transpose the vector y and convert it to a 4 x 1 column vector
the product, xyT, is now defined, since x has four columns and y has four rows. The product will
be 1 x 1 as illustrated in MATLAB:
>> x*y'
ans =
70

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

Each row corresponds to a row in n’ and each column corresponds to a column of t. If we


compute the coefficients, a*n, and place them in a 1 x 10 row vector, we can perform the
summation above using:
>> an*cos(n'*t)
ans =
Columns 1 through 8
2.8700 2.7172 2.4072 2.2494 2.4254 2.8008 3.0051 2.7159
Columns 9 through 11
1.9108 0.8851 0.0317

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.

Approximating a Periodic Function

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

This approximation is found using the following MATLAB commands:


>> n = 1:2;
>> a0=10/7;
>> 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];
>> t=-1:.01:6;
>> n=[0,n];
>> fapprox=a*cos(2*pi*n'*t/7)+b*sin(2*pi*n'*t/7);

and then plotted along with the original function using:


>> fexact=4*(t>=-1)-6*(t>=3)+2*(t>=6);
>> plot(t,fexact,t,fapprox)

This results in the following plot:


We can see that the Fourier series expansion does approximate the function, however, the
approximation is not very good. We can improve the approximation by including a greater
number of terms. If we repeat the MATLAB commands given above for 0≤n≤20:

>> 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)

which results in:


a much better approximation.

You might also like