0% found this document useful (0 votes)
61 views3 pages

Matlab Code:: % Spectrum Analysis by DFT

This MATLAB code document contains examples of using the discrete Fourier transform (DFT) and inverse discrete Fourier transform (IDFT) to analyze sequences and signals. It includes code to: 1) Calculate the DFT of a user-input sequence using a for-loop implementation and plot the magnitude and phase responses. 2) Calculate the IDFT of a user-input sequence (the DFT of the original sequence) and verify the result. 3) Take the DFT of example impulse and step sequences to observe their magnitude and phase responses.

Uploaded by

sen_rrs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views3 pages

Matlab Code:: % Spectrum Analysis by DFT

This MATLAB code document contains examples of using the discrete Fourier transform (DFT) and inverse discrete Fourier transform (IDFT) to analyze sequences and signals. It includes code to: 1) Calculate the DFT of a user-input sequence using a for-loop implementation and plot the magnitude and phase responses. 2) Calculate the IDFT of a user-input sequence (the DFT of the original sequence) and verify the result. 3) Take the DFT of example impulse and step sequences to observe their magnitude and phase responses.

Uploaded by

sen_rrs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

% Spectrum analysis by DFT

MATLAB CODE :
clc;
clear all;
close all;
a=input('Enter the sequence :');
N=length(a);
disp('The length of the sequence is:');N
for k=1:N
y(k)=0;
for i=1:N
y(k)=y(k)+a(i)*exp((-2*pi*j/N)*((i-1)*(k-1)));
end;
end;
k=1:N
disp('The result is:');y
figure(1);
subplot(211);
stem(k,abs(y(k)));
grid;
xlabel('sample values n-->');
ylabel('Amplitudes-->');
title('Mangnitude response of the DFT of given sequence');
subplot(212);
stem(angle(y(k))*180/pi);
grid;
xlabel('sample values n-->');
ylabel('phase-->');
title('Phase response of the DFT of given sequence');

IDFT:
clc;
clear all;
close all;
a=input('Enter the sequence :');
N=length(a);
disp('The length of the sequence is:');N
for n=1:N
y(n)=0;
for k=1:N
y(n)=y(n)+a(k)*exp((2*pi*j*(k-1)*(n-1))/N);
end;
end;
n=1:N
y1=1/N*y(n);
disp('The result is:');y1
figure(1);
stem(n,y1);
grid;
xlabel('sample values n-->');
ylabel('Amplitudes-->');
title('Magnitude response of the IDFT of given DFT');

% FFT of the impulse sequence : magnitude and phase response


clc;
clear all;
close all;
%impulse sequence
t=-2:1:2;
y=[zeros(1,2) 1 zeros(1,2)];
subplot (3,1,1);
stem(t,y);
grid;
input('y=');
disp(y);
title ('Impulse Response');
xlabel ('time -->');
ylabel ('--> Amplitude');
xn=y;
N=input('enter the length of the FFT sequence: ');
xk=fft(xn,N);
magxk=abs(xk);
angxk=angle(xk);
k=0:N-1;
subplot(3,1,2);
stem(k,magxk);
grid;
xlabel('k');
ylabel('|x(k)|');
subplot(3,1,3);
stem(k,angxk);
disp(xk);
grid;
xlabel('k');
ylabel('arg(x(k))');

% FFT of the step sequence : magnitude and phase response


clc;
clear all;
close all;
%Step Sequence
s=input ('enter the length of step sequence');
t=-s:1:s;
y=[zeros(1,s) ones(1,1) ones(1,s)];
subplot(3,1,1);
stem(t,y);
grid
input('y=');
disp(y);
title ('Step Sequence');
xlabel ('time -->');
ylabel ('--> Amplitude');
xn=y;
N=input('enter the length of the FFT sequence: ');
xk=fft(xn,N);
magxk=abs(xk);
angxk=angle(xk);
k=0:N-1;
subplot(3,1,2);
stem(k,magxk);
grid
xlabel('k');
ylabel('|x(k)|');
subplot(3,1,3);
stem(k,angxk);
disp(xk);
grid
xlabel('k');
ylabel('arg(x(k))');

You might also like