Lab 6 DFT and FFT
Lab 6 DFT and FFT
LAB # 6
Discrete Fourier Transform and Fast Fourier Transform
Objective:
The basic objective of this lab is to perform discrete time transform (DFT) and
fast fourier transform (FFT) in Matlab.
DFT is the computable version of discrete time Fourier transform (DTFT). Despite being the
Fourier transform of a discrete signal x[n], DTFT X(ejw) is function of a continuous variable w.
In order to compute DTFT, infinite summations are required, which is not possible, hence DFT is
computed for the knowledge of frequency response.
DFT and Inverse discrete Fourier transform (IDFT) is given by:
Figure 1 below presents DFT as the discretized version of discrete time Fourier transform
(DTFT) in frequency domain.
1
DSP Lab Manual, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
DFT can become DTFT if we consider N→∞. For visualization consider the following figure:
2
DSP Lab Manual, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
Note: In above figure we can observer that as N is increasing DFT is becoming DTFT.
Example 1:
x[n] = [2 3 -1 4] ; n = [0 1 2 3]
for k = 0:N-1
for n = 0:N-1
X(k+1) = X(k+1) + x(n+1)*exp(-1j*2*pi/N*n*k);
end
end
3
DSP Lab Manual, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
4
DSP Lab Manual, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
xn = 0:N-1;
k = 0:N-1;
% finding DFT
X= dft(x,4)
5
DSP Lab Manual, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
x = [2 3 -1 4];
N = length(x);
X = zeros(1,N);
% finding DFT
X= fft(x)
6
DSP Lab Manual, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
Example 2:
x[n]= e (j2π/N*k*n)
with N=8 (sampling period), k=4 (frequency index)
N = 8;
n = 0:N-1; % sampling period
X = fft(x,N);
%Note: both command gives same result(dft function & fft built-in command)
subplot(2,2,[1,3]); plot(real(x),imag(x),'o','MarkerFaceColor','b')
axis equal; ylim([-3 3])
xlabel('Real {x}','Fontsize',8)
ylabel('Im {x}','Fontsize',8)
title(['e^{(j2\pi/N kn)}, k = ',num2str(k)],'Fontsize',8)
7
DSP Lab Manual, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
Example:
Let's look at a simple rectangular pulse, x[n]=1 for 0≤n<M. The DTFT of x[n] is:
M = 8;
w = linspace(-2*pi, 2*pi, 800);
X_dtft = (sin(w*M/2) ./ sin(w/2)) .* exp(-1j * w * (M-1) / 2);
8
DSP Lab Manual, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
plot(w, abs(X_dtft))
title('|X(\omega)|')
The MATLAB function fft computes the DFT. Here's the 8-point DFT of our 8-point rectangular
pulse:
x = ones(1, M);
X = fft(x)
X =
8 0 0 0 0 0 0 0
9
DSP Lab Manual, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
One 8 and a bunch of zeros?? That doesn't seem anything like the DTFT plot above. But when
you superimpose the output of fft in the right places on the DTFT plot, it all becomes clear.
x = ones(1, M);
X = fft(x)
P = 8;
w_k = (0:P-1) * (2*pi/P);
X = fft(x);
plot(w, abs(X_dtft))
hold on
plot(w_k, abs(X), 'o')
hold off
10
DSP Lab Manual, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
x16 =
Columns 1 through 13
1 1 1 1 1 1 1 1 0 0 0 0 0
Columns 14 through 16
0 0 0
P = 16;
X16 = fft(x16);
w_k = (0:P-1) * (2*pi/P);
X = fft(x);
plot(w, abs(X_dtft))
hold on
plot(w_k, abs(X16), 'o')
hold off
11
DSP Lab Manual, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
Another way to increase P is to use the fft(x,P) syntax of the fft function. This syntax computes
the P-point DFT of x by using zero-padding. Let's try a 50-point DFT.
P = 50;
Xp = fft(x, P);
w_k = (0:P-1) * (2*pi/P);
X = fft(x);
plot(w, abs(X_dtft))
hold on
plot(w_k, abs(Xp), 'o')
hold off
12
DSP Lab Manual, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
Functions Used:
1. dft function:
Code:
13
DSP Lab Manual, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
Lab Tasks
In-Lab Task 01: Compute DFT (using FFT command) of following signal:
In-Lab Task 02: Compute DFT (using DFT function) of a following signal:
x[n]= sin(2*pi/N*k*n)
14
DSP Lab Manual, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
Post-Lab Task 02: Find DTFT and DFT of following signal and verify that DFT is just equally-
spaced samples of the DTFT.
Execute above code for 5 and 30 DFT samples. Your output graph with 30 DFT samples should
look like as given in figure below:
15
DSP Lab Manual, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
16