Sei sulla pagina 1di 1

EXPERIMENT NO.

Object: Evaluate 4 point DFT of and IDFT of x(n)= 1.0<n<3; 0 elsewhere.


PROGRAMME FOR DFT:

% this matlab programme calculates the DFT of the sequence


% inputs: causal sequence x(n)
% output: display of X(k)
%------ 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 statement determine
if M~=1, %the size of xn
xn=xn'; %suitable for evaluating
N=M; %the DFT. here 'N' is the length
end
Xk = zeros(N,1); %the DFT values are initialized to zeros
n = 0:N-1; %index n varies from nn 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
disp('X(k) =');
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 from 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');
%--------next part6 calculate the sketch phase of X(k)---------
subplot(2,1,2); %select second figure of vthe two in subplot
k = 0:N-1;
stem(k,angle(Xk),'filled');
grid on;
ylabel('phase of X(k)','color','m');
xlabel('frequency index k','color','m');
%------- end of the programme--------------

PROGRAMME FOR IDFT:

% IDFT of the sequence


% input: DFT X(k)
% output: display the sequence x(n)
%-----next part of X(k)--------------------
Xk = input('Enter the DFT X(k)= '); %accept DFT X(k)
%----next part calculates IDFT x(n)-------
[N,M]=size(Xk);
if M~=1,
Xk = Xk.';
N=M;
end
xn=zeros(N,1);
k=0:N-1;
for n = 0:N-1
xn(n+1) = exp(j*2*pi*k*n/N)*Xk;
end
xn=xn/N;
disp ('x(n)= ');
disp(xn);

Potrebbero piacerti anche