MATLAB

Discrete Fourier Transform (DFT) of the sequence


%file name : dft.m
%Discrete Fourier Transform (DFT) of the sequence. MatLab Version 5.2

% This Matlab program calculates the DFT of the sequence and
% displays its magnitude and phase.
% Inputs: Causal sequences x(n)
% Ouputs: Display of X(k)
% Magnitude and Phase of X(k)
% Assumptions: The DFT is computed for length of input sequence.
% Zero padding is absent.
%-------------------------------------------------------------------------
%------ next part accepts sequence x(n) ----------------------------------

xn = input('Enter the sequence x(n)= '); % accept sequence x(n)

%------ Next part calculates DFT X(k) ------------------------------------

[N,M] = size(xn); % These statements determine
if M~=1, % the size of the vector xn
   xn = xn'; % suitable for evaluating
   N = M; % the DFT. Here 'N' is the length
end % of the DFT.
Xk = zeros(N,1); % The DFT values are initialized to zeros
n = 0:N-1; % Index n varies form n to N-1
for k = 0:N-1 % This 'for' loop implements
   Xk(k+1) = exp(-j*2*pi*k*n/N)*xn; % the DFT equation and calculates
end %   X(k) for k = 0 to N-1.
disp('Xk = ');
disp(Xk); % Display the DFT X(k)

%------- next part calculates and sketches magnitude of X(k) -------------

subplot(2,1,1); % select first figure of the two in subplot
k = 0:N-1; % range of k form 0 to N-1
stem(k,abs(Xk),'filled'); % sketch |X(k)| in the figure
grid on; % grid in the figure
ylabel('Magnitude |X(k)|','color','m'); % label of y - axis
title('Magnitude and Phase of the DFT','color','r');
% title of the figure
                                         
%------- next part calculates and sketches phase of X(k) -----------------
                                 
subplot(2,1,2); % select second figure of the two in subplot
k = 0:N-1; % range of k form 0 to N-1
stem(k,angle(Xk),'filled');% sketch phase of X(k) in the figure
grid on; % grid in the figure
ylabel('Phase of X(k)','color','m'); % label of y - axis
xlabel('Frequency index k','color','m'); % label of x - axis

%--------------- End of the program -------------------------------------

No comments:

Comment

Comment Box is loading comments...