Lab 8
Lab 8
Lab 8
=
=
,where ,......... 2 , 1 , 0 = n and ,......... 2 , 1 , 0 ), ( = k k X are
called the discrete Fourier transform co-efficients, which is given by
) .( .......... .......... .......... ) ( ) (
1
0
2
B e n x k X
N
n
kn
N
j
=
, where ,......... 2 , 1 , 0 = k . The discrete Fourier series co-efficients X(k) is
itself a 9complex-valued) periodic sequence with fundamental period of N. Using
N
j
N
e W
2
=
, we can express Equations (A) and (B) as
) .( .......... .......... .......... ) ( ) (
1
0
C W n x k X
N
n
nk
N
=
=
) ........( .......... .......... ) (
1
) (
1
0
D W k X
N
n x
N
k
nk
N
=
Now, we will define a Matlab function that implement Discrete Fourier Transform of a
periodic sequence.
function [Xk]=dfs(xn,N)
% Compute Discrete Fourier Series Co-efficients
% [Xk]=DFS co-efficients array over 0<=k<=N-1
% xn= one of period of the periodic sequence
% N= fundamental period
n=[0:1:N-1]
k=[0:1:N-1]
WN=exp(-j*2*pi/N);
nk=n'*k
WNnk=WN.^nk
Xk=xn*WNnk
Example 1: Find the DFT of a periodic discrete signal defined by
x(n)={ . 0,1,2,3,0,1,2,3,0,1,2,3.}
Solution: For the given signal, the fundamental period is N=4. The signal can be defined
one period as xn={0,1,2,3}.
By using the above function, we can compute the DFS by using the following code
>> xn=[0 1 2 3]
xn =
0 1 2 3
>> N=4
N =
4
>> xk=dfs(xn,N)
xk = 6.0000 -2.0000 + 2.0000i -2.0000 - 0.0000i -2.0000 - 2.0000i
Now, we will define another function that will implement the Inverse Discrete Fourier
Transform (IDFT).
function [Xk]=idfs(Xk,N)
% Compute Inverse Discrete Fourier Series Co-efficients
% [xn]=one of period of the periodic sequence
% [Xk]=DFS co-efficients error
% N= fundamental period
n=[0:1:N-1]
k=[0:1:N-1]
WN=exp(-j*2*pi/N);
nk=n'*k;
WNnk=WN.^(-nk);
Xk=(Xk*WNnk)/N
We can use this function to compute one period of signal is the DFS co-efficients are
given as follows:
>> N=4
N =
4
>> Xk=[6.0000 -2.0000+2.0000i -2.0000-0.0000i -2.0000-2.0000i]
>> xn=idfs(xk,N)
The output of the code will be
xn = 0.0000 - 0.0000i 1.0000 - 0.0000i 2.0000 - 0.0000i 3.0000 + 0.0000i
The xn shows one period of the discrete signal.
Example 2: A periodic signal is given by
1 ) ( = n x
mNnmN+L-1
0 = mN+Ln(m+1)N-1
where m=0,1, 2, , N is the fundamental period, L/N is the duty cycle
Find the DFS of the signal and plot the magnitude spectrum for different L and N.
The following Matlab Code will find the DFS and plot the magnitude spectrum
L=5;
N=20;
n=[-N:N+1];
x1=[ones(1,L), zeros(1,N-L+1)];
x=[x1 x1]
subplot(221)
stem(n,x)
title('Signal')
% find the DFS with period N=20 and duty cycle =5
k=[-N/2:N/2]
xn=[ones(1,L), zeros(1,N-L)]
Xk=dfs(xn,N);
magXk=abs([Xk(N/2+1:N) Xk(1:N/2+1)])
subplot(222)
stem(k,magXk)
title('DFS with N=20, L=5')
% find the DFS with period N=50 and duty cycle=5
N=50
L=5
k=[-N/2:N/2]
xn=[ones(1,L), zeros(1,N-L)]
Xk=dfs(xn,N);
magXk=abs([Xk(N/2+1:N) Xk(1:N/2+1)])
subplot(223)
stem(k,magXk)
title('DFS with N=50, L=5')
% find the DFS with period N=100 and duty cycle=5
N=200
L=5
k=[-N/2:N/2]
xn=[ones(1,L), zeros(1,N-L)]
Xk=dfs(xn,N);
magXk=abs([Xk(N/2+1:N) Xk(1:N/2+1)])
subplot(224)
stem(k,magXk)
title('DFS with N=200, L=5')
20 0 20 40
0
0.2
0.4
0.6
0.8
1
Signal
10 5 0 5 10
0
1
2
3
4
5
DFS with N=20, L=5
40 20 0 20 40
0
1
2
3
4
5
DFS with N=50, L=5
100 50 0 50 100
0
1
2
3
4
5
DFS with N=200, L=5
Several interesting observations can be made from the plots. The envelopes of the DFS
co-efficients of square wave is looks like a sinc functions. The amplitude at k=0 is equal
to L. The students should now find the spectrum of the signal by changing the duty cycle
L=5 to L=10
Discrete Fourier Transform(DFT)
To compute the Fourier transform numerically on a computer, discretization plus
numerical integration are required. This is an approximation of the true (i.e.,
mathematical), analytically-defined FT in a synthetic (digital) environment, and is called
Discrete Fourier Transformation (DFT). The DFT of a continuous time signal sampled
over the period of T, with a sampling rate of t can be given as
=
=
1
0
2
) ( ) (
N
n
t n f m j
e t n s
N
T
f m S
(4)
where f=1/T, and, is valid at frequencies up to f
max
= 1/(2t).
The Discrete Fourier Series (DFS) is practically equivalent to the Discrete Fourier
Transform (DFT) when 0nN-1. [The students are asked to check the text book for
proof] Therefore, the implementation of DFT should be similar to that of DFS. Hence
the earlier dfs and idfs functions can be renamed as dft and idft as follows:
function [Xk]=dft(xn,N)
% Compute Discrete Fourier Series Co-efficients
% [Xk]=DFS co-efficients array over 0<=k<=N-1
% xn= one of period of the periodic sequence
% N= fundamental period
n=[0:1:N-1]
k=[0:1:N-1]
WN=exp(-j*2*pi/N);
nk=n'*k
WNnk=WN.^nk
Xk=xn*WNnk
function [Xk]=idft(Xk,N)
% Compute Inverse Discrete Fourier Series Co-efficients
% [xn]=one of period of the periodic sequence
% [Xk]=DFS co-efficients error
% N= fundamental period
n=[0:1:N-1]
k=[0:1:N-1]
WN=exp(-j*2*pi/N);
nk=n'*k;
WNnk=WN.^(-nk);
Xk=(Xk*WNnk)/N
Example 3: Let x(n) be a 4-point sequence define by
X(n) = 1 0n3
= 0, otherwise
a. Compute the Discrete Time Fourier Transform (DTFT) of x(n)
b. Compute the 4-point DFT
c. Compute the 8-point, 16-point and 64 point DFT of x(n)
d. Compare the results of all DFTs
The DTFT of x(n) is defined by
=
3
0
) ( ) (
n
jn j
e n x e X
3 2
1
+ + + = e e e
j j
The following piece of MatLab code will compute and plot the magnitude of DTFT. In
addition 4-point, 8-point and 16-point DFTs are also calculated and plotted for
comparison.
w=0:pi/200:2*pi
H=1+exp(-j*w)+exp(-j*2*w)+exp(-j*3*w)
subplot(221)
plot(w,abs(H))
title('Magnitude of DTFT of x(n)')
axis([0 6 0 4])
% computer the 4-point DFT
x=[1 1 1 1]
N=4
n=0:1:(N-1)
X4=dft(x,4)
subplot(222)
stem(n,abs(X4))
title('Magnitude of 4-point DFT')
axis([0 3 0 4])
% compute the 16-point DFT
N=16
x=[ones(1,4) zeros(1,12)] % Note that x has been zero padded
n=0:1:(N-1)
X8=dft(x,N)
subplot(223)
stem(n,abs(X8))
hold on
plot(n,abs(X8),'-.k')
title('Magnitude of 16-point DFT')
axis([0 16 0 4])
% compute 32 point DFT
N=32
x=[ones(1,4) zeros(1,28)] % Note that x has been zero padded
n=0:1:(N-1)
X8=dft(x,N)
subplot(224)
stem(n,abs(X8))
hold on
plot(n,abs(X8),'-.k')
title('Magnitude of 32-point DFT')
axis([0 32 0 4])
The Magnitude and the phase of DTFT of x(n) are shown in the following figures
0 2 4 6
0
1
2
3
4
Magnitude of DTFT of x(n)
0 1 2 3
0
1
2
3
4
Magnitude of 4point DFT
0 5 10 15
0
1
2
3
4
Magnitude of 16point DFT
0 10 20 30
0
1
2
3
4
Magnitude of 32point DFT
Now, we should compute and plot 4-point DFT, 8-point DFT and 16-point DFT by using
the following MatLab code
Note: Base on the plots we can conclude that zero-padding is an operation in which
more zeros are appended with the original sequence. The resulting longer DFT
provides closely resemblances to the DTFT of the original sequence.