Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Matlab Code:: % Spectrum Analysis by DFT

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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