Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

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.

Discrete Fourier Transform:

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.

Type your text


Figure 1: DFT as discrete version of DTFT

DFT can become DTFT if we consider N→∞. For visualization consider the following figure:

Figure 2: DFT for N approaching infinity


Note: In above figure we can observer that as N is increasing DFT is becoming DTFT.

Modified DFT (Non-centered and Un-normalized):


We will be using modified form of DFT in we will be using interval of time and frequency from
0 →N-1
Modified DFT and IDFT is given by:

a. DFT Using loops:

Example 1:
x[n] = [2 3 -1 4] ; n = [0 1 2 3]

DFT Code using loops:


x = [2 3 -1 4];
N = length(x);
X = zeros(1,N);

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

% plot a discrete-time signal


xn = 0:N-1;
k = 0:N-1;

subplot(3,1,1), stem(xn,x,'filled','markersize',4), axis([-0.5,3.5,-1.5,4.


5]), ylabel('x[n]')
subplot(3,1,2), stem(k,real(X),'filled','markersize',4), xlim([-0.5,3.5]),
ylabel('Real')
subplot(3,1,3), stem(k,imag(X),'filled','markersize',4), xlim([-0.5,3.5]),
ylabel('Imaginary')
Figure 3: DFT graph using loops

b. DFT Using DFT function:


Here DFT function is defined and DFT of any signal is found using this defined function. This
function uses DFT formulae to find DFT.

Note: This DFT function is defined at end of lab.

DFT Code using DFT function:


x = [2 3 -1 4];
N = length(x);
X = zeros(1,N);

% plot a discrete-time signal


xn = 0:N-1;
k = 0:N-1;

% finding DFT
X= dft(x,4)

subplot(3,1,1), stem(xn,x,'filled','markersize',4), axis([-0.5,3.5,-1.5,4.


5]), ylabel('x[n]')
subplot(3,1,2), stem(k,real(X),'filled','markersize',4), xlim([-0.5,3.5]),
ylabel('Real')
subplot(3,1,3), stem(k,imag(X),'filled','markersize',4), xlim([-0.5,3.5]),
ylabel('Imaginary')

Figure 4: DFT graph using DFT function

c. DFT Using FFT command:


FFT algorithms are so commonly employed to compute DFTs that the term 'FFT' is often used to mean
'DFT': DFT refers to a mathematical transformation or function, whereas 'FFT' refers to a specific family
of algorithms for computing DFTs. FFT command is used to compute dft and fft is computationally
efficient.
DFT code using FFT command:

x = [2 3 -1 4];
N = length(x);
X = zeros(1,N);

% plot a discrete-time signal


xn = 0:N-1;
k = 0:N-1;

% finding DFT
X= fft(x)

subplot(3,1,1), stem(xn,x,'filled','markersize',4), axis([-0.5,3.5,-1.5,4.


5]), ylabel('x[n]')
subplot(3,1,2), stem(k,real(X),'filled','markersize',4), xlim([-0.5,3.5]),
ylabel('Real')
subplot(3,1,3), stem(k,imag(X),'filled','markersize',4), xlim([-0.5,3.5]),
ylabel('Imaginary')

Figure 5: DFT graph using FFT command


Example 2:
x[n]= e (j2π/N*k*n)
with N=8 (sampling period), k=4 (frequency index)

DFT code using dft function & FFT command:


k = 4; % index for freqeuncy

N = 8;
n = 0:N-1; % sampling period

x = exp(1j*2*pi/N*k*n); % harmonic complex exponential


X = dft(x,N);

% or use below command

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)

subplot(2,2,2); stem(n,abs(X),'filled'), axis tight


xlabel('k','Fontsize',8)
ylabel('Amplitude','Fontsize',8)
title('Result of FFT','fontsize',8)

subplot(2,2,4), stem(n,real(x),'filled'), axis tight


xlabel('n'), ylabel('x[n]')
title(['e^{(j2\pi/N kn)}, k = ',num2str(k)],'Fontsize',8)
Figure 6: DFT graph and time domain signal graph

Relationship between DFT and DTFT


It's finally time to start looking at the relationship between the discrete Fourier transform (DFT)
and the discrete-time Fourier transform (DTFT).

Example:
Let's look at a simple rectangular pulse, x[n]=1 for 0≤n<M. The DTFT of x[n] is:

Let's plot |X(ω)| for M=over a couple of periods:

DTFT code using exact transformation (analytic form):

M = 8;
w = linspace(-2*pi, 2*pi, 800);
X_dtft = (sin(w*M/2) ./ sin(w/2)) .* exp(-1j * w * (M-1) / 2);
plot(w, abs(X_dtft))
title('|X(\omega)|')

Figure 7: DTFT graph X(w)


It turns out that, under certain conditions, the DFT is just equally-spaced samples of the DTFT.
Suppose XP[k] is the P-point DFT of x[n]. If x[n] is nonzero only over the finite domain 0≤
n<M, then XP[k] equals X(ω) at equally spaced intervals of ω:

The MATLAB function fft computes the DFT. Here's the 8-point DFT of our 8-point rectangular
pulse:

DFT code using fft command:

x = ones(1, M);
X = fft(x)

X =

8 0 0 0 0 0 0 0
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.

Code for DTFT and DFT for 8-DFT samples:

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

Figure 8: DTFT and DFT graph for 8-DFT samples


Now you can see that the seven zeros in the output of fft correspond to the seven places (in each
period) where the DTFT equals zero.
You can get more samples of the DTFT simply by increasing P. One way to do that is to zero-
pad.
Code for zero padding of input signal x[n]:
x16 = [x, zeros(1, 8)]

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

Code for DTFT and DFT for 16-DFT samples:

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

Figure 9: DTFT and DFT graph for 16-DFT samples


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.

Code for DTFT and DFT for 50-DFT samples:

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

Figure 10: DTFT and DFT graph for 50-DFT samples


If you've ever wondered what that whole zero-padding business was all about with Fourier
transforms, now you know. When you tack on a bunch of zeros to a sequence and then compute
the DFT, you're just getting more and more samples of the DTFT of the original sequence.
Functions Used:
1. dft function:

This function is used to find dft of signal using dft formulae:

Code:

function [Xk] = dft(xn,N)

% Computes Discrete Fourier Transform


% [Xk] = dft(xn,N)
% Xk = DFT coeff. array over 0 <= k <= N-1
% xn = N-point finite-duration sequence
% N = Length of DFT
%

n = [0:1:N-1]; % row vector for n


k = [0:1:N-1]; % row vecor for k
WN = exp(-1j*2*pi/N); % Wn factor
nk = n'*k; % creates a N by N matrix of nk value
s
WNnk = WN.^nk; % DFT matrix
Xk = xn*WNnk; % row vector for DFT coefficients
Lab Tasks
In-Lab Task 01: Compute DFT (using FFT command) of following signal:

with N=8 (sampling period), n= 0:N-1

In-Lab Task 02: Compute DFT (using DFT function) of a following signal:

x[n]= sin(2*pi/N*k*n)

with N=8 (sampling period), k=2 (frequency index)

Your output graph should look like as shown in figure below:


Post-Lab Task 01: Compute DFT of a following triangle:
n
x[n]=(0.5*e(jπ/3))

Your output graph should look like as shown in figure below:

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:

You might also like