Sunday, 21 October 2012

DFT in MATLAB



Just create a new file with extension .m and paste the code below in that .m file. Then Run it.


clear all;
clc;
x=input('Enter the sequence x(n) : ');
N=input('\nEnter the value of no. of points N : ');
L=length(x);
if(N>=L)
    x=[x zeros(1,N-L)];
elseif(N<L)
 error('Length of sequence must be less than no. of points.\n');
end

for k=0:1:N-1
    SUM(k+1)=0;
    for n=0:1:N-1
        SUM(k+1)=SUM(k+1)+x(n+1)*exp(-2*pi*j*k*n/N);     
    end    
end  
fprintf('DFT of x(n) is X(k)= ')
SUM 

%Graphics
figure(1);
subplot(2,1,1);
stem(abs(SUM));
subplot(2,1,2);
stem(angle(SUM));

No comments:

Post a Comment

Tech N Science © 2013