Pages

Tuesday, 26 February 2013

DFT & IDFT Without Using Function (Matlab 2012a)

Program:

clc;
clear all;
N = input('Number of DFT points = ');
xn = input('Enter the sequence xn = ');  %Get the sequence from user
ln = length(xn);       %find the length of the sequence
xn = [xn zeros(1,N-ln)];
xk = zeros(1,N);  %initialize an array of same size as that of input sequence
ixk = zeros(1,N); %initialize an array of same size as that of input sequence
%code block to find the DFT of the sequence
for k = 0:N-1
    for n = 0:N-1
        xk(k+1) = xk(k+1)+(xn(n+1)*exp((-1i)*2*pi*k*n/N));
    end
end

%code block to plot the input sequence
t = 0:N-1;
subplot(2,2,1);
stem(t,xn);
ylabel('Amplitude ---->');
xlabel('n ---->');
title('Input Sequence ---->');
grid on;
magnitude = abs(xk); %Find the magnitudes of individual DFT points
disp('DFT Sequence = ');
disp(magnitude);

%code block to plot the DFT sequence
t = 0:N-1;
subplot(2,2,2);
stem(t,magnitude);
ylabel('Amplitude ---->');
xlabel('K ---->');
title('DFT Sequence ---->');
grid on;
phase = angle(xk); %Find the phases of individual DFT points
disp('Phase = ');
disp(phase);
 %code block to plot the Phase Response
t = 0:N-1;
subplot(2,2,3);
stem(t,phase);
ylabel('Phase ---->');
xlabel('K ---->');
title('Phase Response');
grid on;
% Code block to find the IDFT of the sequence
for n = 0:N-1
    for k = 0:N-1
        ixk(n+1) = ixk(n+1)+(xk(k+1)*exp(1i*2*pi*k*n/N));
    end
end
ixk = ixk./N;
%code block to plot the IDFT sequence
t = 0:N-1;
subplot(2,2,4);
stem(t,ixk);
disp('IDFT Sequence = ');
disp(ixk);
ylabel('Amplitude ---->');
xlabel('n ---->');
title('IDFT sequence ---->');
grid on;



Output:

Number of DFT points = 4
Enter the sequence xn = [1 1 0 0]
DFT Sequence =
    2.0000    1.4142    0.0000    1.4142

Phase =
         0   -0.7854   -1.5708    0.7854

IDFT Sequence =
   1.0000 - 0.0000i   1.0000            -0.0000 + 0.0000i   0.0000 + 0.0000i



Waveform:

No comments:

Post a Comment