Vous êtes sur la page 1sur 8

EE 341 Matlab Fourier Examples Fall 2007

A touch of Fourier with Matlab


Lets recall how to get the frequency spectrum (DTFT) of a sequence, say x = [2 3 2 3]
for T = 0.5
X
d
(w) =

n=
x(nT)e
jnwT
= 2 + 3e
jwT
+ 2e
2jwT
3e
j3wT
In MATLAB:
w = -20.0:.01:20; T = 0.3;
Xd = 2 + 3
*
exp(-j
*
T.
*
w) + 2
*
exp(-j
*
2
*
T.
*
w) - 3
*
exp(-j
*
3
*
T.
*
w);
subplot(121),plot(w,abs(Xd));
ylabel(magnitude),xlabel(frequency),grid
subplot(122),plot(w,angle(Xd));
ylabel(phase),xlabel(frequency),grid
20 10 0 10 20
2.5
3
3.5
4
4.5
5
5.5
6
6.5
7
7.5
m
a
g
n
i
t
u
d
e
frequency
20 10 0 10 20
4
3
2
1
0
1
2
3
4
p
h
a
s
e
frequency
1
EE 341 Matlab Fourier Examples Fall 2007
Lets get the DFT using the fft function from MATLAB:
% Program 5.1
x = [2 3 2 -3 ]; N = length(x); T = 0.3;
X = fft(x,N);
m = 0:N-1; D = 2
*
pi/(N
*
T);
subplot(121)
stem(m
*
D,abs(X),fill), title((a));
ylabel(magnitude),xlabel(frequency),grid
axis([0 20 0 8])
subplot(122)
stem(m
*
D,angle(X),fill),title((aa))
ylabel(phase),xlabel(frequency),grid
axis([0 20 -4 4])
0 5 10 15 20
0
1
2
3
4
5
6
7
8
(a)
m
a
g
n
i
t
u
d
e
frequency
0 5 10 15 20
4
3
2
1
0
1
2
3
4
(aa)
p
h
a
s
e
frequency
2
EE 341 Matlab Fourier Examples Fall 2007
% Program 5.1bis
x = [2 3 2 -3 ]; N = length(x); T = 0.3;
X = fft(x,N);
m = 0:N-1; D = 2
*
pi/(N
*
T);
subplot(121)
stem(m
*
D,abs(X),fill), title((a));
ylabel(magnitude),xlabel(frequency),grid
hold on
w = -20.0:.01:20;
Xd = 2 + 3
*
exp(-j
*
T.
*
w) + 2
*
exp(-j
*
2
*
T.
*
w) - 3
*
exp(-j
*
3
*
T.
*
w);
subplot(121),plot(w,abs(Xd),:);
axis([0 20 0 8])
subplot(122)
stem(m
*
D,angle(X),fill),title((aa))
ylabel(phase),xlabel(frequency),grid
hold on
subplot(122),plot(w,angle(Xd),:);
axis([0 20 -4 4])
0 5 10 15 20
0
1
2
3
4
5
6
7
8
(a)
m
a
g
n
i
t
u
d
e
frequency
0 5 10 15 20
4
3
2
1
0
1
2
3
4
(aa)
p
h
a
s
e
frequency
3
EE 341 Matlab Fourier Examples Fall 2007
If a DT sequence is real valued, the magnitude spectrum is even and its phase spectrum is odd.
Thus plot only the positive frequency range.
Note: I am expanding the sequence by doing zero padding. The effect is to display more points of
the frequency spectra.
x = [2 3 2 -3 ]; N = length(x); T = 0.3;
X = fft(x,N);
mp = 0:N/2; D = 2
*
pi/(N
*
T); %if N is odd mp = 0:(N-1)/2
subplot(221)
stem(mp
*
D,abs(X(mp+1)),fill), title((b));
axis([0 20 0 8]);
subplot(222)
stem(mp
*
D,angle(X(mp+1)),fill),title((bb))
axis([0 20 -4 4]);
xpad = [x zeros(1,50)]; Npad = length(xpad); T = 0.3;
Xpad = fft(xpad,Npad);
mpad = 0:Npad/2; D = 2
*
pi/(Npad
*
T);
subplot(223)
stem(mpad
*
D,abs(Xpad(mpad+1)),fill),
title((b) with zero padding);
subplot(224)
stem(mpad
*
D,angle(Xpad(mpad+1)),fill),
title((bb)with zero padding )
0 5 10 15 20
0
2
4
6
8
(b)
0 5 10 15 20
4
2
0
2
4
(bb)
0 5 10 15
0
2
4
6
8
(b) with zero padding
0 5 10 15
4
2
0
2
4
(bb)with zero padding
4
EE 341 Matlab Fourier Examples Fall 2007
For comparison purposes, I include the two previous graphs in the same page:
0 5 10 15 20
0
1
2
3
4
5
6
7
8
(a)
m
a
g
n
i
t
u
d
e
frequency
0 5 10 15 20
4
3
2
1
0
1
2
3
4
(aa)
p
h
a
s
e
frequency
0 5 10 15 20
0
2
4
6
8
(b)
0 5 10 15 20
4
2
0
2
4
(bb)
0 5 10 15
0
2
4
6
8
(b) with zero padding
0 5 10 15
4
2
0
2
4
(bb)with zero padding
5
EE 341 Matlab Fourier Examples Fall 2007
Interpolation and Frequency Resolution
In MATLAB interpolation can be done replacing the function stem with plot. Its called linear
interpolation. Lets look at the spectrum of same sequence x = [2 3 2 3] for different
values of N = 4, 12, 1024; and different support: [0, 2/T) = [0, 20.94) and [0, /T) = [0, 10.47)
x = [2 3 2 -3 ]; N = length(x); T = 0.3;
X = fft(x,N); m = 0:N-1; D = 2
*
pi/(N
*
T);
subplot(321),plot(m
*
D,abs(X),m
*
D,angle(X),:r);
axis([0 20 -5 10]); mp = 0:N/2;
subplot(322),plot(mp
*
D,abs(X(mp+1)),mp
*
D,angle(X(mp+1)),:)
axis([0 10.47 -5 10]);
x12 = [2 3 2 -3 zeros(1,8)]; N = length(x12);
X = fft(x12,N); m = 0:N-1; D = 2
*
pi/(N
*
T);
subplot(323),plot(m
*
D,abs(X),m
*
D,angle(X),:r);
axis([0 20 -5 10]); mp = 0:N/2;
subplot(324),plot(mp
*
D,abs(X(mp+1)),mp
*
D,angle(X(mp+1)),:)
axis([0 10.47 -5 10]);
x1024 = [2 3 2 -3 zeros(1,1020)]; N = length(x1024);
X = fft(x12,N); m = 0:N-1; D = 2
*
pi/(N
*
T);
subplot(325),plot(m
*
D,abs(X),m
*
D,angle(X),:r);
axis([0 20 -5 10]); mp = 0:N/2;
subplot(326),plot(mp
*
D,abs(X(mp+1)),mp
*
D,angle(X(mp+1)),:)
axis([0 10.47 -5 10]);
0 5 10 15 20
5
0
5
10
0 5 10
5
0
5
10
0 5 10 15 20
5
0
5
10
0 5 10
5
0
5
10
0 5 10 15 20
5
0
5
10
0 5 10
5
0
5
10
6
EE 341 Matlab Fourier Examples Fall 2007
Plotting spectra in [/T, /T)
The function fft generates the spectrum of a DT signal in [0, 2/T). If we want to plot the spectrum
of the sequence x = [2 3 2 3] in the frequency range [/T, /T) = [10.47, 10.47) for
T = 0.3 and N = 4, D = 2/NT = 5.235.
If N is even select m = N/2 : N/2 1, then mD lie inside the Nyquist Frequency Range.
Since X
d
[m] is periodic with period N you can shift the second half of X
d
[m] to the rst half. The
function tshift does this
x = [2 3 2 -3 ]; N = length(x); T = 0.3;
X = fft(x,N);
Xs = fftshift(X);
ms = -N/2:N/2 - 1;
D = 2
*
pi/(N
*
T);
subplot(121)
plot(ms
*
D,abs(Xs),ms
*
D,angle(Xs),:)
title((a)); xlabel(Frequency (rad/s));
ylabel(magnitude and phase);
axis([-10 10 -5 10]);grid
%using 1024 point fft and linear interpolation
x1024 = [2 3 2 -3 zeros(1,1020)]; N = length(x1024);
X = fft(x1024,N);
Xs = fftshift(X);
ms = -N/2:N/2 - 1; D = 2
*
pi/(N
*
T);
subplot(122),plot(ms
*
D,abs(Xs),ms
*
D,angle(Xs),:r);
title((b)); xlabel(Frequency (rad/s));
ylabel(magnitude and phase);
axis([-10 10 -5 10]);grid
10 5 0 5 10
5
0
5
10
(a)
Frequency (rad/s)
m
a
g
n
i
t
u
d
e

a
n
d

p
h
a
s
e
10 5 0 5 10
5
0
5
10
(b)
Frequency (rad/s)
7
EE 341 Matlab Fourier Examples Fall 2007
FFT computation of CT signals
Example 5.6.3 modied
Compute the spectra of x
1
(t) = 2e
0.3t
sin 5t and x
2
(t) = x
1
(t) cos 20t, for t 0.
% Program 5.8 modified
T = 0.1; N = 8192; D = 2
*
pi/(N
*
T);
n = 0:N-1; t = n
*
T;
x1 = 2
*
exp(-0.3
*
t).
*
sin(5
*
t);
X = T
*
fft(x1);
mp = 0:N/2; w = mp
*
D;
subplot(211), plot(w,abs(X(mp+1)))
x2 = x1.
*
cos(20
*
t);
X = T
*
fft(x2);
subplot(212), plot(w,abs(X(mp+1)))
0 5 10 15 20 25 30 35
0
1
2
3
4
0 5 10 15 20 25 30 35
0
0.5
1
1.5
2
Other things we glimpsed at:
How to build periodic functions with Fourier Series
wintool from MATLAB
fdatool from MATLAB
Note: This presentation follows very closely the text used in class: Signals and Systems, by Chi-
Tsong Chen, Third edition, 2004 Oxfor University Press
8

Vous aimerez peut-être aussi