You are on page 1of 6

%BUTERWORTH lpf FILTER DESIGN

clear all;
close all;
fp=input(' passpand egde frequency in Hz:');
fs=input(' stopband edge frequency in Hz:');
Fs=input('sampling frequency in Hz:');
Ap=input('passband attenuation in dB:');
As=input('stopband attenuation in dB:');
wp=2*pi*fp/Fs;
ws=2*pi*fs/Fs;
T=1;
ohm_p=2*tan(wp/2)/1;
ohm_s=2*tan(ws/2)/1;
order=log10((10^(0.1*As)-1)/(10^(0.1*Ap)-1))/(2*log10(ohm_s/ohm_p));
N=ceil(order);
disp('order is :');
disp(N);
ohm_c=ohm_p/(10.^(0.1*Ap)-1).^(1/(2*N));%meet passband specifications
exactly
for k=0:1:N-1
q=(pi/2)+((2*k+1)*pi/(2*N));
p(k+1)=exp(j*q);
end
disp('poles are');
disp(p);%pole
z=[];%zero
k=1;%gain=1
[num,den]=zp2tf(z,p,k);%Zero-pole to transfer function conversion,with
numerator and denominator coefficients in descending powers of s& k=gain
Hnorm=tf(num,den)% creates a continuous-time transfer function with
numerator(s) NUM and denominator(s) DEN
numerator=ohm_c^N.*num;
for k=0:1:N
denominator(k+1)=den(k+1).*ohm_c^k;%ie,for N=2 multiply co-efficient
of s^2 with omh_c^0,co-efficient of s with ohm_c^1 and so on..
end;
Hs=tf(numerator,denominator);%Actual transfer function
[numz,denz]=bilinear(numerator,denominator,1);%conversion of H(s) to H(z)
using bilinear transformation
Hz=tf(numz,denz,1,'variable','z^-1')%for discrete time systems,set
variable as z^-1
[H,omega]=freqz(numz,denz,256);
gain=20*log10(abs(H));
subplot(2,1,1);
plot(omega/pi,gain);
axis([0 1 -150 1]);
title('MAGNITUDE PLOT');
xlabel('\omega/\pi');
ylabel('gain in dB');
subplot(2,1,2);
plot(omega/pi,angle(H));

title('PHASE PLOT');
xlabel('\omega/\pi');
ylabel('phase in radian');

%BUTERWORTH HIGH PASS FILTER DESIGN

clear all;
close all;
fp=input(' passpand egde frequency in Hz:');
fs=input(' stopband edge frequency in Hz:');
Fs=input('sampling frequency in Hz:');
Ap=input('passband attenuation in dB:');
As=input('stopband attenuation in dB:');
wp=2*pi*fp/Fs;
ws=2*pi*fs/Fs;
T=1;
ohm_p=2*tan(wp/2)/1;
ohm_s=2*tan(ws/2)/1;
ohm_pl=1;
ohm_sl=ohm_p/ohm_s;
order=log10((10^(0.1*As)-1)/(10^(0.1*Ap)-1))/(2*log10(ohm_sl/ohm_pl));
N=ceil(order);
disp('order is :');
disp(N);
ohm_c=ohm_sl/(10.^(0.1*As)-1).^(1/(2*N));%meet stopband specifications
exactly
for k=0:1:N-1
q=(pi/2)+((2*k+1)*pi/(2*N));
p(k+1)=exp(j*q);
end
disp('poles are');
disp(p);%pole
z=[];%zero
k=1;%gain=1
[num,den]=zp2tf(z,p,k);%Zero-pole to transfer function conversion,with
numerator and denominator coefficients in descending powers of s& k=gain
Hnorm=tf(num,den)% creates a continuous-time transfer function with
numerator(s) NUM and denominator(s) DEN
numerator=ohm_c^N.*num;
for k=0:1:N
denominator(k+1)=den(k+1).*ohm_c^k;%ie,for N=2 multiply co-efficient
of s^2 with omh_c^0,co-efficient of s with ohm_c^1 and so on..
end;
new_num=fliplr(numerator);
mirror_deno=fliplr(denominator);
for k=0:1:N
new_deno(k+1)=mirror_deno(k+1).*ohm_p^k;%ie,for N=2 multiply coefficient of s^2 with omh_c^0,co-efficient of s with ohm_c^1 and so on..
end;
Hs=tf(new_num,new_deno)
[numz,denz]=bilinear(new_num,new_deno,1);%conversion of H(s) to H(z)
using bilinear transformation
Hz=tf(numz,denz,1,'variable','z^-1')%for discrete time systems,set
variable as z^-1
[H,omega]=freqz(numz,denz,256);
gain=20*log10(abs(H));

subplot(2,1,1);
plot(omega/pi,gain);
axis([0 1 -150 1]);
legend('BUTTERWORTH HPF for
fp=2khz,fs=500hz,Fs=8khz,Ap=2dB,As=15dB','location','northoutside');
title('MAGNITUDE PLOT ');
xlabel('\omega/\pi');
ylabel('gain in dB');
subplot(2,1,2);
plot(omega/pi,angle(H));
title('PHASE PLOT ');
xlabel('\omega/\pi');
ylabel('phase in radian');

%CHEBSHEV LOWPASS FILTER


clear all;
close all;

clc;
fp=input(' passpand egde frequency in Hz:');
fs=input(' stopband edge frequency in Hz:');
Fs=input('sampling frequency in Hz:');
Ap=input('passband attenuation in dB:');
As= input('stopband attenuation in dB:');
wp= 2*pi*fp/Fs;
ws= 2*pi*fs/Fs;
T=1;
ohm_p=2*tan(wp/2)/1;
ohm_s=2*tan(ws/2)/1;
order=acosh(sqrt((10^(0.1*As)-1)/(10^(0.1*Ap)-1)))/(acosh(ohm_s/ohm_p));
N=ceil(order);
disp('order is :');
disp(N);
epsi=sqrt(10^(0.1*Ap)-1);
beta=sqrt((sqrt(1+epsi^2)+1)/epsi);
r1=((beta^2)+1)/(2*beta);
r2=((beta^2)-1)/(2*beta);
numer=1;
for k=0:1:N-1
q=(pi/2)+((2*k+1)*pi/(2*N));
p(k+1)=r2*cos(q)+j*r1*sin(q);
if rem(N,2)==0
numer=numer*p(k+1);
else
numer=-numer*p(k+1);
end;
end;
disp('poles are');
disp(p);%pole
if rem(N,2)==0
zero=numer/(sqrt(1+epsi^2));
else
zero=numer;
end;
z=[];%zero
k=1;%gain=1
[num,den]=zp2tf(z,p,k);%Zero-pole to transfer function conversion,with
numerator and denominator coefficients in descending powers of s& k=gain
Hnorm=tf(num,den)% creates a continuous-time transfer function with
numerator(s) NUM and denominator(s) DEN
numerator=ohm_p^N.*num*abs(zero);
for k=0:1:N
denominator(k+1)=den(k+1).*ohm_p^k;%ie,for N=2 multiply co-efficient
of s^2 with omh_c^0,co-efficient of s with ohm_c^1 and so on..
end;
Hs=tf(numerator,denominator)%Actual transfer function
[numz,denz]=bilinear(numerator,denominator,1);%conversion of H(s) to H(z)
using bilinear transformation

Hz=tf(numz,denz,1,'variable','z^-1')

[H,omega]=freqz(numz,denz,256);
gain=20*log10(abs(H));
subplot(2,1,1);
plot(omega/pi,gain);
axis([0 1 -150 10]);
title('MAGNITUDE PLOT ');
xlabel('\omega/\pi');
ylabel('gain in dB');
legend('CHEBYSHEV LPF for
fp=4k,fs=8k,Fs=40k,Ap=1dB,As=10dB','location','NorthOutside');
subplot(2,1,2);
plot(omega/pi,angle(H));
title('PHASE PLOT');
xlabel('\omega/\pi');
ylabel('phase in radian');
%axis([0 1 -8 0]);

You might also like