Download MATLab Tutorial #6

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Tensor operator wikipedia , lookup

Cross product wikipedia , lookup

Dual space wikipedia , lookup

Non-negative matrix factorization wikipedia , lookup

Singular-value decomposition wikipedia , lookup

Laplace–Runge–Lenz vector wikipedia , lookup

Euclidean vector wikipedia , lookup

Cayley–Hamilton theorem wikipedia , lookup

Gaussian elimination wikipedia , lookup

Vector space wikipedia , lookup

Covariance and contravariance of vectors wikipedia , lookup

Linear algebra wikipedia , lookup

Basis (linear algebra) wikipedia , lookup

Cartesian tensor wikipedia , lookup

Matrix multiplication wikipedia , lookup

Bra–ket notation wikipedia , lookup

Matrix calculus wikipedia , lookup

Four-vector wikipedia , lookup

Transcript
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
-2 3
-1
6
10
t
The signal, f(t) has a period, T = 7. Thus, wo = 2π/7, and the Fourier series expansion is:
∞
∞
n=1
n=1
f (t) = a0 + ∑ an cos nω 0 t + ∑ bn sin nω 0 t
where:
a0 =
an =
bn =
ω0
10
f (t)dt =
∫
2π T
7
ω0
π
T
ω0
π
∫
f (t)cos nω 0 t dt =
2 ⎡
⎛ 6π n ⎞
⎛ 2π n ⎞
⎛ 12π n ⎞ ⎤
3sin
+
2
sin
−
sin
⎜
⎟
⎜
⎟
⎜⎝
⎟
⎝ 7 ⎠
⎝ 7 ⎠
π n ⎢⎣
7 ⎠ ⎥⎦
∫
f (t)sin nω 0 t dt =
2 ⎡
⎛ 6π n ⎞
⎛ 2π n ⎞
⎛ 12π n ⎞ ⎤
−3cos
+
2
cos
+
cos
⎜
⎟
⎜
⎟
⎜⎝
⎟
⎝ 7 ⎠
⎝ 7 ⎠
π n ⎢⎣
7 ⎠ ⎥⎦
T
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
5.0000
6.0000
8.0000
9.0000 10.0000
4.0000
7.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
Columns 9 through 11
2.2494
2.4254
2.8008 3.0051 2.7159
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
n=1
n=1
f (t) ≈ a0 + ∑ an cos nω 0 t + ∑ bn sin nω 0 t
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.