Thursday 17 October 2013

MATLAB Programme 01: Implement DFT Without using inbuilt function

DFT Implementation


DFT is used to convert a signal sample array x(n) of size 1 x N to signal spectral component array X(k) of size 1 x N.
 
 
 
%programme
 
clc;close all;

clear all;

xn = input('Enter the sequence');
 
 
ln = length(xn);
xk = zeros(1,ln);
ixk = zeros(1,ln);
 
%Calculating DFT
for k=0:ln-1
   for n=0:ln-1
xk(k+1) = xk(k+1)+(xn(n+1)*exp((-i)*2*pi*k*n/ln));
 
   end
end
%hear xk(k+1); because of in matlab array index start form 1

 
t = 0:ln-1;
subplot(3,2,1);
stem(t,xn);
title ('Input Sequence x(n)');
xlabel('Time Index');
ylabel('Amplitude');
 
t = 0:ln-1;
subplot(3,2,2);
stem(t,xk);
ylabel ('Amplitude');
title ('X(K)');
xlabel ('K');

 

 
t = 0:ln-1;
subplot(3,2,3);
stem(t,abs(xk));
 
 
ylabel ('Amplitude');
xlabel ('K');
title ('Magnitude Response');
 
 
t = 0:ln-1;
subplot(3,2,4);
stem(t,angle(xk));
 
ylabel('Phase');
xlabel('K');
title ('Phase Response');


%-------------------------------------------------------------------%

%Output
%Enter the sequence[ 1 2 3 3 2 1]



 

 

No comments:

Post a Comment