DSP Lab Manual, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
LAB # 5
Discrete Time Fourier Transform
Objective:
The basic objective of this lab is to perform discrete time Fourier transform and
its properties in Matlab.
Discrete Time Fourier Transform:
Discrete Time Fourier Transform (DTFT) is a frequency analysis tool for aperiodic discrete-
time signals The DTFT of x[n] , X(ejw) is given by following equation :
Fourier transform, X(ejw) is also called spectrum and is a continuous function of the frequency
parameter.
To convert X(ejw) to x[n] , we use inverse DTFT equation which is given by:
DTFT Properties:
1
DSP Lab Manual, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
a. DTFT Using exact transformation (Analytic form)
Example 1:
Exact transformation code:
w = linspace(-pi,pi,2^8);
X = exp(1j*w)./(exp(1j*w) - 0.9);
plot(w/pi,abs(X))
Figure 1: DTFT graph using exact transformation
b. DTFT using definition/formulae (Numerical computation form)
Here first we have to generate time domain input signal x[n]
Time domain input signal x[n] code:
N = 30;
n = 0:N-1;
x = zeros(1,N);
for i = 1:N
x(i) = 0.5^(i-1);
end
2
DSP Lab Manual, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
stem(n,x)
Figure 2: Time domain input signal x[n]
DTFT code using definition/formulae:
w = linspace(-1,1,2^8)*pi;
X = exp(-1j*(w'*n))*x';
plot(w/pi,abs(X))
c. DTFT using function:
Here DTFT function is defined and DTFT of any signal is found using this defined function.
This function uses DTFT formulae to find DTFT.
Note: This DTFT function is defined at end of lab.
DTFT code using function:
X = dtft(x,n,w);
plot(w/pi,abs(X))
3
DSP Lab Manual, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
Figure 3: DTFT graph using formulae
Figure 4: DTFT graph using DTFT function
4
DSP Lab Manual, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
Note: DTFT graphs using all three methods are same.
Real/Imaginary and Magnitude/Angle graphs:
As DTFT is a complex exponential therefore we can find its real/imaginary part graph and
magnitude/angle graph
Real/Imaginary and Magnitude/Angle graphs code:
N = 500;
w2 = [-1:1/N:1]*pi;
w = w2(1:end-1);
% closed form of DTFT
X = exp(1j*w)./(exp(1j*w) - 0.5*ones(size(w)));
magX = abs(X); angX = angle(X);
realX = real(X); imagX = imag(X);
subplot(2,2,1); plot(w/pi,magX); grid
xlabel('frequency in \pi'); title('Magnitude Part','fontsize',8); ylabel('
Magnitude')
subplot(2,2,3); plot(w/pi,angX); grid
xlabel('frequency in \pi'); title('Angle Part','fontsize',8); ylabel('Radi
ans')
subplot(2,2,2); plot(w/pi,realX); grid
xlabel('frequency in \pi'); title('Real Part','fontsize',8); ylabel('Real'
)
subplot(2,2,4); plot(w/pi,imagX); grid
xlabel('frequency in \pi'); title('Imaginary Part','fontsize',8); ylabel('
Imaginary')
5
DSP Lab Manual, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
Figure 5: Real/Imaginary and Magnitude/Angle graphs
Example 2:
x[n] = [1 2 3 4 5] ; n = [ -1 0 1 2 3]
Time domain input signal x[n] code:
% in MATLAB, we represent sequences and indices as row vectors
n = -1:3;
x = 1:5; % sequence x(n)
% just for a nice plot
m = 10;
stem([n(1)-m:n(end)+m],[zeros(1,m),x,zeros(1,m)],'filled'), axis tight
6
DSP Lab Manual, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
Figure 6: Time domain input signal x[n]
DTFT code using definition/formulae:
%% DTFT code
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%
N = 500;
k = -N:N-1; w = (pi/N)*k;
X = x * (exp(-1j*pi/500)).^(n'*k);% DTFT by formulae/matrix-vector product
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%
% plots
magX = abs(X); angX = angle(X);
realX = real(X); imagX = imag(X);
subplot(2,2,1); plot(w/pi,magX); grid
xlabel('frequency in \pi'); title('Magnitude Part','fontsize',8); ylabel('
Magnitude')
subplot(2,2,3); plot(w/pi,angX); grid
xlabel('frequency in \pi'); title('Angle Part','fontsize',8); ylabel('Radi
ans')
subplot(2,2,2); plot(w/pi,realX); grid
xlabel('frequency in \pi'); title('Real Part','fontsize',8); ylabel('Real'
)
subplot(2,2,4); plot(w/pi,imagX); grid
xlabel('frequency in \pi'); title('Imaginary Part','fontsize',8); ylabel('
Imaginary')
7
DSP Lab Manual, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
Figure 7: Real/Imaginary and Magnitude/Angle graphs
Example 3
Time domain input signal x[n] code:
n = 0:10;
x = (0.5).^n;
subplot(2,1,1), stem(n,real(x),'filled')
subplot(2,1,2), stem(n,imag(x),'filled')
8
DSP Lab Manual, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
Figure 8: Time domain input signal x[n]
DTFT code using definition/formulae:
N = 100;
k = -N:N-1; w = (pi/N)*k;
X = x*(exp(-1j*pi/N)).^(n'*k); % DTFT
magX = abs(X); angX = angle(X);
subplot(2,1,1); plot(w/pi,magX);grid
xlabel('frequency in \pi'); ylabel('|X|')
title('Magnitude Part')
subplot(2,1,2); plot(w/pi,angX);grid
xlabel('frequency in \pi'); ylabel('radians')
title('Angle Part')
9
DSP Lab Manual, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
Figure 9: Magnitude/Angle graphs
Example 4
DTFT of the unit pulse
DTFT code using DTFT function:
x = [1 1 1 1 1 1 1];
n = -3:3;
w = linspace(-2,2,2^10)*pi;
X = dtft(x,n,w);
xd = zeros(1,31);
10
DSP Lab Manual, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
nd = -15:15;
[y,ny] = sigadd(xd,nd,x,n);
subplot(2,1,1), stem(ny,y,'filled')
subplot(2,1,2), plot(w/pi,abs(X)), xlabel('Freq. in \pi units')
Figure 10: Time domain and DTFT graph of input signal x[n]
DTFT Properties implementation:
a. DTFT Linearity Property:
DTFT code using definition/formulae:
x1=rand(1,11);
x2=rand(1,11);
n=0:10
a=2;
b=3;
k=0:500
w=(pi/500)*k;
11
DSP Lab Manual, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
X1=x1*(exp(-j*pi/500)).^(n'*k);
X2=x2*(exp(-j*pi/500)).^(n'*k);
x=a*x1+b*x2;
X=x*(exp(-j*pi/500)).^(n'*k);
X_check=a*X1+b*X2;
error=max(abs(X-X_check))
b. DTFT Time Shift Property:
Example:
same amplitude
phase changed (linearly −∠ωm)
DTFT code using definition/formulae:
x=rand(1,11);
n=0:10;
k=0:500;
w=(pi/500)*k;
X=x*(exp(-j*pi/500)).^(n'*k);
y=x;m=n+2;
Y=y*(exp(-j*pi/500)).^(m'*k);
Y_check=(exp(-j*2).^w).*X;
error=max(abs(Y-Y_check))
error=5.7737e-015
Functions Used:
1. Sigdd:
This function is used to 2 signals:
Code:
function [y,n] = sigadd(x1,n1,x2,n2)
% implements y(n) = x1(n)+x2(n)
% [y,n] = sigadd(x1,n1,x2,n2)
% y = sum sequence over n, which includes n1 and n2
12
DSP Lab Manual, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
% x1 = first sequence over n1
% x2 = second sequence over n2 (n2 can be different from n1)
n = min(min(n1),min(n2)):max(max(n1),max(n2)); % duration of y(n)
y1 = zeros(1,length(n)); y2 = y1; % initialization
y1(find((n >= min(n1)) & (n <= max(n1)) == 1)) = x1;%x1 with duration of y
y2(find((n >= min(n2)) & (n <= max(n2)) == 1)) = x2;%x2 with duration of y
y = y1 + y2; % sequence addition
2. DTFT Function:
This function is used to find DTFT of signal using DTFT formulae:
Code:
function X = dtft(x,n,w)
% X = dtft(x, n, w)
%% X = DTFT values computed at w frequencies
% x = finite duration sequence over n
% n = sample position vector
% w = frequency location vector
X = exp(-1j*(w'*n))*x';
end
13
DSP Lab Manual, Electrical Engineering Department, COMSATS Institute of IT, Islamabad
Lab Tasks
In-Lab Task 01: Compute DTFT of a one-sided exponential:
In-Lab Task 02: Compute DTFT of a following signal:
Post-Lab Task 01: Compute DTFT of a following triangle:
Post-Lab Task 02: Prove frequency shift property of DTFT.
14