You are on page 1of 3

Spectral analysis

clf;
N=input('signal length=');
R=input('DFT length=');
fr=input('Type in the sinusoidal frequencies=');
n=0:N-1;
x=0.5*sin(2*pi*n*fr(1))+sin(2*pi*n*fr(2));
Fx=fft(x,R);
k=0:R-1;
stem(k,abs(Fx));grid;
xlabel('k');ylabel('magnitude');
title(['N=',num2str(N),'R=',num2str(R)]);

PROGRAM FOR CIRCULAR CONVOLUTION


clc;clear all;close all;
x=input('enter the first sequence x(n)=');
h=input('enter the second sequence h(n)=');
h=h';x=x';
z=[h,circshift(h,1),circshift(h,2),circshift(h,3)];
y=z*x;figure;
subplot(3,1,1);stem(x);
ylabel('amplitude------->');xlabel('(a)n------>');
subplot(3,1,2),stem(h);
ylabel('amplitude------->');xlabel('(b)n------>');
subplot(3,1,3),stem(h);
ylabel('amplitude------->');xlabel('(c)n------>');
disp('the resultant signal is y(n),');y

PROGRA FOR LINEAR CONVOLUTION


clc;clear all;close all;
x=input('enter the first sequence x(n)=');
h=input('enter the second sequence h(n)=');
y=conv(x,h);
subplot(3,1,1);stem(x);
ylabel('amplitude------->');xlabel('(a)n------>');
subplot(3,1,2),stem(x);
ylabel('amplitude------->');xlabel('(b)n------>');
subplot(3,1,3),stem(h);
ylabel('amplitude------->');xlabel('(c)n------>');
disp('the resultant signal is y(n),');y

DFT COMPUTATION USING FFT


clc;clear all;close all;
x=input('enter the first sequence x(n)=');
n=input('enter the no.of points n=');
z=length('x');
if(n>z)
y=fft(x,n);
subplot(2,2,1);abs(y);stem(ans);
title('DFT magnitude');
xlabel('freq index');
ylabel('magnitude');
subplot(2,2,2);
angle(y);
stem(ans);
ylabel('phase');
xlabel('freq index');
title('DFT phase');
disp('the resultant signal is');y

IDFT COMPUTATION USING FFT


x=ifft(y,n);
subplot(2,2,3);
abs(x);stem(ans);
title('IDFT magnitude');
xlabel('freq index');
ylabel('magnitude');subplot(2,2,4);
angle(x);stem(ans);
ylabel('phase');
xlabel('freq index');
title('IDFT phase');
disp('the resultant signal is');x
else
disp('DFT and IDFT cannot be computed');
end;

Program To Generate The Followin Sequence

% 1)Unit Step Sequence


N=21;
X=ones(1,N);
n=0:1:N-1;
subplot(3,2,1),stem(n,X);
Xlabel('n'),ylabel('X(n)');
title('Unit Step Sequence');

%2) Addition Of Two Sinusoidal Sequence


X1=sin(.1*pi*n)+sin(.2*pi*n);
subplot(3,2,2),stem(n,X1);
Xlabel('n'),ylabel('X1(n)');
title('Addition Of Two Sinusoidal Sequence');

%3)Exponential Sequence
X2=.8.^(n);
subplot(3,2,3),stem(n,X2);
Xlabel('n'),Ylabel('X2(n)');
title('Exponential Sequence');

%4) Sinusoidal Sequence


X3=cos(.1*pi*n);
subplot(3,2,4),stem(n,X3);
Xlabel('n'),Ylabel('X3(n)');
title('Sinusoidal Sequence');

%5) Ramp Sequence


n2=input('enter the length of the ramped sequence');
n=0:n2;
subplot(3,2,5);
stem(n,n);
ylabel('amplitude r(n)---->');
xlabel('(c)n----->');
title('ramp sequence');
n=0:.1:pi;

%6) Cosine Sequence


y4=cos(2*pi*n);
subplot(3,2,6);stem(n,y4);
ylabel('amplitude---->');
xlabel('(f)n----->');
title('cosine sequence');
PROGRAM FOR BUTTERWORTH LOW PASS FILTER
close all;clc;clear all;
alphap=04;
alphas=30;
fp=400;
fs=800;
F=2000;
omp=2*fp/F;oms=2*fs/F;
[n,wn]=buttord(omp,oms,alphap,alphas);
[b,a]=butter(n,wn);
w=0:.01:pi;
[h,om]=freqz(b,a,w,'whole');
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);plot(om/pi,m);grid;
ylabel('gain in db');xlabel('normalised frequency');
subplot(2,1,2);plot(om/pi,an);grid;
ylabel('phase in radians');
xlabel('normalised frequency');

PROGRAM FOR BUTTERWORTH HIGH PASS FILTER


close all;clc;clear all;
alphap=04;
alphas=30;
fp=400;
fs=800;
F=2000;
omp=2*fp/F;oms=2*fs/F;
[n,wn]=buttord(omp,oms,alphap,alphas);
[b,a]=butter(n,wn,'HIGH');
w=0:.01:pi;
[h,om]=freqz(b,a,w,'whole');
m=20*log10(abs(h));
an=angle(h);
subplot(2,2,1);plot(om/pi,m);grid;
ylabel('gain in db');xlabel('normalised frequency');
subplot(2,1,2);plot(om/pi,an);grid;
ylabel('phase in radians');
xlabel('normalised frequency');

PROGRAM FOR BUTTERWORTH BAND STOP FILTER


close all;clc;clear all;
ap=2;
as=20;
ws=[.2*pi,.4*pi];
wp=[.1*pi,.5*pi];
[n,wn]=buttord(wp/pi,ws/pi,ap,as);
[b,a]=butter(n,wn,'stop');
wi=0:.01:pi;
[h,w]=freqz(b,a,wi,'whole');
m=20*log10(abs(h));
phase=angle(h);
subplot(2,1,1);plot(w/pi,m);grid;
ylabel('magnitude in db');xlabel('normalised frequency');
subplot(2,1,2);plot(w/pi,phase);grid;
ylabel('phase in radians');
xlabel('normalised frequency');

PROGRAM FOR BUTTERWORTH BAND PASS FILTER


close all;clc;clear all;
alphap=12;
alphas=40;
wp=[.2*pi,.4*pi];
ws=[.1*pi,.5*pi];
[n,wn]=buttord(wp/pi,ws/pi,alphap,alphas);
[b,a]=butter(n,wn,'bandpass');
w=0:.01:pi;
[ph,m]=freqz(b,a,w);
g=20*log10(abs(ph));
an=angle(ph);
subplot(2,1,1);plot(m/pi,g);grid;
ylabel('gain in db');xlabel('normalised frequency');
subplot(2,1,2);plot(m/pi,an);grid;
ylabel('phase in radians');
xlabel('normalised frequency');

Program for cheby1 low pass filter


clear all;
alp=20;
als=50;
wp=0.2*pi;
ws=0.3*pi;
[n,wn]=cheb1ord(wp/pi,ws/pi,alp,als);
[b,a]=cheby1(n,alp,wn);
w=0:.01:pi;
[h,ph]=freqz(b,a,w);
m=20*log10(abs(h));
a=angle(h);
subplot(2,1,1);plot(ph/pi,m);grid;
ylabel('gain in db');xlabel('normalised frequency');
subplot(2,1,2);plot(ph/pi,a);grid;
ylabel('phase in radians');
xlabel('normalised frequency');

Program for cheby1 high pass filter


clear all;
alp=20;
als=50;
cop=0.2*pi;
cos=0.3*pi;
[n,wn]=cheb1ord(cop/pi,cos/pi,alp,als);
[b,a]=cheby1(n,alp,wn,'high');
w=0:.01:pi;
[h,ph]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);plot(ph/pi,m);grid;
ylabel('gain in db');xlabel('normalised frequency');
subplot(2,1,2);plot(ph/pi,an);grid;
ylabel('phase in radians');
xlabel('normalised frequency');

Program for cheby1 bandstop filter


clear all;
alp=20;
als=60;
wp=[.2*pi,.4*pi];
ws=[.1*pi,.5*pi];
[n,wn]=cheb1ord(wp/pi,ws/pi,alp,als);
[b,a]=cheby1(n,alp,wn,'stop');
w=0:.01:pi;
[h,ph]=freqz(b,a,w);
m=20*log10(abs(h));
a=angle(h);
subplot(2,1,1);plot(ph/pi,m);grid;
ylabel('gain in db');xlabel('normalised frequency');
subplot(2,1,2);plot(ph/pi,a);grid;
ylabel('phase in radians');
xlabel('normalised frequency');
Program for cheby1 bandstop filter
clear all;
alp=20;
als=40;
wp=[.2*pi,.4*pi];
ws=[.1*pi,.5*pi];
[n,wn]=cheb1ord(wp/pi,ws/pi,alp,als);
[b,a]=cheby1(n,alp,wn,'bandpass');
w=0:.01:pi;
[ph,m]=freqz(b,a,w);
g=20*log10(abs(ph));
an=angle(ph);
subplot(2,1,1);plot(m/pi,g);grid;
ylabel('gain in db');xlabel('normalised frequency');
subplot(2,1,2);plot(m/pi,an);grid;
ylabel('phase in radians');
xlabel('normalised frequency');

Hamming And Blackman Window


clear all;
wc = 0.5 *pi;
N=25;
b=fir1(N,wc/pi,hamming(N+1));
w=0:.01:pi;
h=freqz(b,1,w);
plot(w/pi, abs(h));
hold on
b=fir1(N,wc/pi,blackman(N+1));
w=0:.01:pi;
h=freqz(b,1,w);
plot(w/pi,abs(h),'-');
ylabel('Magnitude');
xlabel('Normalised Frequency');
hold off

Hamming And Rectangular Window


clear all;
wc=.5*pi;
N=25;
alpha=(N-1)/2;
eps=0.001;
n=0:1:N-1;
hd=sin(wc*(n-alpha + eps))/(pi *(n-alpha + eps));
wr = boxcar(N);
hn = hd*wr;
w=0:0.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h));
hold on
wh=hamming(N);
hn=hd*wh;
w=0:0.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'-'); grid;
ylabel('Magnitude'); xlabel('Normalised Frequency');
hold off

Kaiser Window
format long;
kw=kaiser(61,4.5333514);
b=fir1(60,.3,kw);
[h,omega]=freqz(b,1,512);
mag=20*log10(abs(h));
plot(omega/pi,mag);grid;
xlabel('Normalised Frequency');
ylabel('gain in db');

You might also like