Signals and Systems: Laboratory Manual
Signals and Systems: Laboratory Manual
Signals and Systems: Laboratory Manual
(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.
The sampling theorem states that a band-limited continuous-time signal, with highest frequency (or
bandwidth) equal to B Hz, can be recovered from its samples provided that the sampling frequency,
denoted by Fs, is greater than or equal to 2B Hz (or samples per second). The minimum sampling rate
is often called the Nyquist rate. For example, the minimum sampling rate for a telephone speech signal
(assumed low-pass filtered at 4 kHz) should be 8 KHz (or 8000 samples per second), while the
minimum sampling rate for an audio CD signal with frequencies up to 22 KHz should be 44KHz.
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
h = stem(t,x,'filled','r','LineWidth',2);
xlabel('t (sec)')
title('Sampled Signal')
grid
Xk = fft(x); % Computing DFT of x
X = fftshift(abs(Xk))/length(x);
% fx = linspace(-Fs/2,(Fs/2)-1,N); % setting frequency axis
between –Fs/2 to Fs/2
fx = -Fs/2:Fs/length(X):Fs/2-(Fs/length(X)); % setting frequency
axis between –Fs/2 to Fs/2
subplot(212)
h = stem(fx,X,'filled','LineWidth',2);
grid
xlabel('f (Hz)')
title('DFT Magnitude Sepectrum |X[k]|')
set(gca,'Fontweight','bold');
xlim([-5 5])
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');