S&S Lab 100
S&S Lab 100
(EL-223)
LABORATORY MANUAL
Dr.Shahzad Saleem
Engr. Fakhar Abbas
Implementatin of DFT and IDFT using MATLAB
(LAB # 13)
____________________________________________________________________________________________________________________________________________________________
Description:
DFT:
The discrete Fourier transform, or DFT, is the primary tool of digital signal processing. The
foundation of the product is the fast Fourier transform (FFT), a method for computing the DFT with
reduced execution time. Many of the toolbox functions (including Z-domain frequency response,
spectrum and cepstrum analysis, and some filter design and implementation functions) incorporate
the FFT. DFT and Inverse DFT are represented as:
DFT and IDFT computations using usign fft and ifft commands in MATLAB:
Example: x[n] = [ −3 4 1 2]
fft:
fft(x) computes the discrete Fourier transform (DFT) of X using a fast Fourier transform (FFT)
algorithm
ifft:
ifft(X) returns the inverse discrete Fourier transform (IDFT) of vector X, computed with a fast
Fourier transform (FFT) algorithm. If X is a matrix, ifftreturns the inverse DFT of each column of the
matrix.
Example:
x = [3 4 1 -2] % Defining x[n]
XK = fft(x)% Computing DFT
xn = ifft(XK)% Computing IDFT
Note: You can also compute DFT and IDFT using formula given above that would also give same
results as fft and ifft command.
In the figure below, you see the sampling effects on a sinusoidal signal of frequency B Hz that result
from the use of different sampling frequencies.
Sampling of a continuous-time signal results in repeating its spectrum in the frequency domain. The
spectrum is repeated every Fs Hz. Assuming that the bandwidth of the continuous-time signal is B
Hz, then the repeated bands in the frequency domain will not interfere with each other if the sampling
frequency is larger than 2B Hz. In this case, the spectrum of the original signal can be recovered from
the spectrum of the sampled signal by low-pass filtering with a cutoff frequency that is equal to Fs/2
Hz. This also means that the original signal can be recovered from the sampled signal via the low-
pass filtering operation. When the sampling rate is not large enough (not larger than 2B Hz), then
interference among adjacent bands will occur, and this results in the phenomenon of aliasing. In this
case, the original signal cannot be recovered from the sampled signal
clc;close;
Fs = 10; %sampling Frequency
f = 2; % Signal’s Frequency
N = 10; % Total number of samples
n = 0:N-1; % n represents sample number
T = 1/Fs; % T represents sampling interval i.e., time between 2
samples
t = n*T; % t = nTs = n/Fs
x2 = cos(2*f*pi*t);
Xk = fft(x2); % Computing fft
Y = abs(Xk);
X = fftshift(abs(Xk));
fx = 0:Fs/length(X):Fs-(Fs/length(X));
fy = -Fs/2:Fs/length(X):Fs/2-(Fs/length(X));
subplot(311)
h=stem(fx,Y,'filled');
set(gca,'Xtick',0:13,'fontweight','bold')
set(h,'linewidth',2);
grid on;
xlabel('0 to Fs - 1','fontweight','bold');
ylabel('|X(k)|','fontweight','bold')
title('Before fftshift')
subplot(312)
h=stem(fx,X,'filled');
set(gca,'Xtick',0:13,'fontweight','bold');
set(h,'linewidth',2);
grid on;
xlabel(' 0 to Fs - 1','fontweight','bold');
ylabel('|X(k)|','fontweight','bold');
title('After fftshift');
subplot(313)
h=stem(fy,X,'filled');
set(gca,'Xtick',-7:7,'fontweight','bold');
set(h,'linewidth',2);
grid on;
xlabel('-Fs/2 to Fs/2','fontweight','bold');
ylabel('|X(k)|','fontweight','bold');
title('After fftshift');