Signals and Systems: Laboratory Manual

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

Signals and Systems

(EL-223)

LABORATORY MANUAL

Dr.Shahzad Saleem
Engr. Fakhar Abbas
Implementatin of DFT and IDFT using MATLAB
(LAB # 13)

____________________________________________________________________________________________________________________________________________________________

NATIONAL UNIVERSITY OF COMPUTER AND EMERGING SCIENCES, ISLAMABAD

Prepared by: Engr. Fakhar Abbas Version: 1.00


Last Edited by:
Verified by: Dr. Waqas bin Abbas, Dr. Shahzad Updated: Spring 2020
Saleem
Implementatin of DFT and IDFT using MATLAB Lab 13

Lab # 13: Implementatin of DFT and IDFT using MATLAB


Objective: In this lab, you will learn:
 How to find DFT and inverse DFT:
 Using fft and ifft command
 Using Formula implementation
 Sampling and Aliasing using MATLAB
 Analysis of fft command over different points
 Filtering applied to Audio Signals
Tools Used: MATLAB

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.

Sampling and Aliasing:

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 2 of 6


Implementatin of DFT and IDFT using MATLAB Lab 13

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

Sampling and Aliasing Phenomenon using MATLAB:

Example: Compting Magnitude Spectrum of DFT.


clc; close all; clear
Fs = 50; %sampling Frequency
f = 2; % Signal’s Frequency
N = Fs; % 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 = n*T = n/Fs
x = cos(2*f*pi*t); % Time Domain Signal
subplot(211)
plot(t,x,'linewidth',2)
set(gca,'Fontweight','bold');
hold on

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 3 of 6


Implementatin of DFT and IDFT using MATLAB Lab 13

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])

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 4 of 6


Implementatin of DFT and IDFT using MATLAB Lab 13

Example: Understanding fftshift () command:


Let x(t) = cos(2π2t), where t = nTs , Fs = 10, and N = 10 samples.
Write a Matlab code to compute dft using fft command . The objective of this task is to become
familiar with fftshift command. Your graphs should contain 3 subplots like this:

Matlab Code of above Example:

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');

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 5 of 6


Implementatin of DFT and IDFT using MATLAB Lab 13

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');

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 6 of 6

You might also like