Mat Lab Program
Mat Lab Program
clc;
clear all;
close all;
rp=input('Enter the passband ripple(rp):');
rs=input('Enter the stopband ripple(rs):');
fp=input('Enter the passband frequency(fp):');
fs=input('Enter the stopband frequency(fs):');
f=input('Enter the sampling frequency(f):');
wp=2*fp/f;
ws=2*fs/f;
num=-20*log10(sqrt(rp*rs))-13;
dem=14.6*(fs-fp)/f;
n=ceil(num/dem);
n1=n+1;
if(rem(n,2)~=0)
n1=n;
n=n-1;
end
y=boxcar(n1);
%Low pass filter
b=fir1(n,wp,y);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,1);
plot(m);
ylabel('Gain(db)->');
xlabel('(a)Normalised frequency->');
%High pass filter
b=fir1(n,wp,'high',y);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,2);
plot(m);
ylabel('Gain(db)->');
xlabel('(b)Normalised frequency->');
%Band pass filter
wn=[wp*ws];
b=fir1(n,wn,y);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,3);
plot(m);
ylabel('Gain(db)->');
xlabel('(c)Normalised frequency->');
%Band stop filter
wn=[wp*ws];
b=fir1(n,wn,'stop',y);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,4);
plot(m);
ylabel('Gain(db)->');
xlabel('(d)Normalised frequency->');
fir filter graph
Fir filter in hanning window
clc;
clear all;
close all;
fp=input('enter the passband ripple(rp):');
fs=input('enter the stopband ripple(rs):');
rp=input('enter the passband freaquency(fp):');
rs=input('enter the stopband freaquency(fs):');
f=input('enter the sampling freaquency(f):');
num=-20*log10(sqrt(rp*rs))-13;
dem=14.6*(fs-fp)/f;
n=ceil(num/dem)
n=abs(n);
wp=2*fp/f;
ws=2*fs/f;
wn=(ws+wp)/2;
if(rem(n,2)==0)
m=n+1;
else
m=n;
n=n-1;
end
w=hann(m);
b=fir1(n,wn,'high',w);
freqz(b,1,500,3000);
title('magnitude and phase response')
graph
Butterworth filter program
clc;
clear all;
close all;
fp=input('enter the passband ripple(rp):');
fs=input('enter the stopband ripple(rs):');
rp=input('enter the passband freaquency(fp):');
rs=input('enter the stopband freaquency(fs):');
f=input('enter the sampling freaquency(f):');
wp=2*fp/f;
ws=2*fs/f;
[n,wn]=buttord(wp,ws,rp,rs);
[b,a]=butter(n,wn,'low');
freqz(b,a,500,f);
title('frequency response of butterworth filter');
graph