06-05-2011, 03:40 PM
Procedure:-
1) Enter the number of points, N
2) Enter the input sequence elements, x[n]
3) Create a vector for the sample index, ’n’
4) Initialize loop variable, ‘k’ for the DFT samples X(k)
5) Calculate the twiddle factor for each ‘k’
6) Multiply x[n] and the twiddle factors , elements-by-element
7) Sum all the products, assign to X(k)
8) Plot the magnitude and phase spectrum
9) Verify the results with built in function
MATLAB program for DFT
Code:
clear
clc
close all
% ENTER THE NUMBER OF POINTS
N = 6
% ENTER THE SEQUENCE IS x WITH TIME INDEX n
x = [1 1 2 2 3 3];
n = 0:N-1;
% COMPUTE DFT
for k = 0:N-1
W = exp(-j*2*pi*k*n/N)
dotprod = x.*W;
X(k+1) = sum(dotprod)
end
% PLOT THE MAGNITUDE SPECTRUM
subplot(2,1,1);
stem(n,abs(X));
xlabel('n-->');
ylabel('magnitude-->');
grid on;
% PLOT THE PHASE SPECTRUM
subplot(2,1,2);
stem(n,angle(X));
xlabel('n-->');
ylabel('angle-->');
grid on;
% VERIFICATION OF THE IMPLIMENTATION
verify = fft(x,N);
figure
% PLOT THE MAGNITUDE SPECTRUM
subplot(2,1,1);
stem(n,abs(verify));
xlabel('n-->');
ylabel('magnitude-->');
grid on;
% PLOT THE PHASE SPECTRUM
subplot(2,1,2);
stem(n,angle(verify));
xlabel('n-->');
ylabel('angle-->');
grid on;
RESULT:-COMPUTATION OF DFT