DFT Computation
a) MATLAB program to compute N-point DFT of a given sequence x[n],
magnitude and phase of X(k) and to recover time domain signal
% DFT and IDFT computation using equations
clc;clear all;close all;
x = input('enter the input sequence x[n]');
L = length(x)
N = input('enter the N value');
% zero padding
if N > L
x = [x,zeros(1,N-L)];
end
% computation of DFT
for k = 0:1:N-1;
X(k+1) = 0;
for n = 0:1:N-1;
X(k+1) = X(k+1)+(x(n+1)*exp(-1i*2*pi*n*k/N));
end
end
disp('The DFT of x[n] using direct method of DFT computation is')
disp(X);
% computation of magnitude and phase
Xm = abs(X);
Xp = phase(X);
% IDFT computation using equation
for n = 0:1:N-1;
xr(n+1) = 0;
for k = 0:1:N-1;
xr(n+1) = xr(n+1)+(X(k+1)*exp(1i*2*pi*n*k/N));
end
end
disp('The IDFT of X(k) using equation is')
xr = xr/N
1
% plotting x[n],Xm(k),Xp(k) and xr[n]
n = 0:1:N-1;
k = 0:1:N-1;
subplot(411),stem(n,x),xlabel('n'),ylabel('x(n)'),title('input sequence');grid on;
subplot(412),stem(k,Xm),xlabel('k'),ylabel('|X(k)|'),title('magnitude response');grid on;
subplot(413),stem(k,Xp),xlabel('k'),ylabel('phase of X(k)'),title('phase response');grid on;
subplot(414),stem(n,xr),xlabel('n'),ylabel('xr[n]'),title('recovered signal');grid on;
2
b) MATLAB program to compute N-point DFT of a given sequence x[n]
using Direct method of computation and FFT algorithm
% Compuatational performance comparision of DFT using Equation and FFT
algorithm
clc;clear all;close all;
x = input('enter the input sequence x[n]');
L = length(x);
N = input('enter the N value');
% zero padding
if N > L
x = [x,zeros(1,N-L)];
end
% DFT computation using equation
disp('The time taken for the computation of DFT by direct method is')
tic;
for k = 0:1:N-1;
X(k+1) = 0;
for n = 0:1:N-1;
X(k+1) = X(k+1)+(x(n+1)*exp(-1i*2*pi*n*k/N));
end
end
toc;
% DFT computation using FFT algorithm
disp('The time taken for computation of DFT by FFT algorithm is')
tic;
X = fft(x,N); % % choose N>=L for getting better display
toc;
disp('The DFT X(k) for the input x[n] is')
disp(X);
% computation of IDFT using FFT algorithm
xr = ifft(X,N);
% plot of x[n] and xr[n]
n = 0:1:N-1;
figure(1);
subplot(211),stem(n,x),xlabel('n'),ylabel('x(n)'),title('input sequence');grid on;
subplot(212),stem(n,xr),xlabel('n'),ylabel('xr(n)'),title('recovered sequence');grid on;
3
Applications of DFT for linear filtering and spectral analysis
a) MATLAB program to find spectral components of a signal
clc;clear all;close all;
% To generate the signal
A = 0.8;
f = 500;
fs = 8000;
ts = 1/fs;
cycles = 10;
t=0:ts:cycles/f;
x = A*sin(2*pi*f*t) ;
L = length(x);
sound(x);
% To find the frequency component of the signal (choose N > L)
N = 512;
X = fft(x,N);
Xm = abs(X);
f = 0:fs/N:(N-1)*fs/N;
k = 0:1:N-1;
% To plot the signal in time domain and frequency domain
figure(1);
subplot(311);plot(t,x);xlabel('t');ylabel('x(t)');title('sinewave');grid on;
subplot(312);plot(f(1:N/2),Xm(1:N/2)),xlabel('f in Hz');ylabel('X(f)');
title('spectrum of sinewave');grid on;
subplot(313);stem(k(1:N/2),Xm(1:N/2));xlabel('k');ylabel('X(k)'); grid on;
4
b) MATLAB program to compute linear convolution of two given
sequences using circular convolution
clc;clear all;close all;
% read the input sequences
x1=input(‘Enter the first sequence x1[n]’)
x2=input(‘Enter the second sequence x2[n]’)
L1=length(x1);
L2=length(x2);
% linear convlution length
L= L1+L2-1;
% circular convlution length made equal to linear convolution length
N= L;
% zero-padding
x1=[x1, zeros(1, N-L1)];
x2=[x2, zeros(1, N-L2)];
% compuataion of circular convolution by DFT-IDFT method
X1=fft(x1, N);
X2=fft(x2, N);
Y=X1.*X2;
y =ifft(Y);
% sketching the input and output sequences
Subplot(311), stem(x1), xlabel(‘n’), ylabel(‘x1[n]’), title(‘sequence x1[n]’);
Subplot(312), stem(x2), xlabel(‘n’), ylabel(‘x2[n]’), title(‘sequence x2[n]’);
Subplot(313), stem(y), xlabel(‘n’), ylabel(‘x1[n] * x2[n]’), title(‘linear convolution’);