%Matlab file used for class demo conducted during Session 14
%Chebyshev Type I filter design example
clf
set(0,'defaultaxesfontsize',20);
Wp=2*pi*(4/24); Ws=2*pi*(6/24);
Wpa=tan(Wp/2);  Wsa=tan(Ws/2);
Rp=1;  Rs=40;
input('Minimum Chebyshev Type II Filter Order');
[N, Wn] = cheb2ord(Wpa, Wsa, Rp, Rs,'s')
[B,A] = cheby2(N,Rs,Wn,'s');
epsd=.01;
freqomega=linspace(-pi+epsd,pi-epsd,512);
zomega=exp(j*freqomega);
bilinear=(zomega-ones(size(freqomega)))./(zomega+ones(size(freqomega)));
%
Ma=length(B);
filtnum=zeros(size(freqomega));
filtden=zeros(size(freqomega));
for nz=1:Ma,
filtnum=filtnum+B(nz)*bilinear.^(Ma-nz);
end
Na=length(A);
for np=1:Na,
filtden=filtden+A(np)*bilinear.^(Na-np);
end
filtfreq=filtnum./filtden;
%
fmag=20*log10(abs(filtfreq));
farg=angle(filtfreq);
fmag=fmag-max(fmag);
clf
subplot(121)
plot(freqomega,fmag,'LineWidth',4);
axis([-pi pi -60 0]);
title('Chebyshev II Filter'),...
xlabel('Omega'), ylabel('Magnitude (dB)'), grid
subplot(122)
plot(freqomega,farg,'LineWidth',4);
axis([-pi pi -pi pi]);
hold on
plot([Wp Wp],[-60 0],'m--','LineWidth',4)
plot([-Wp -Wp],[-60 0],'m--','LineWidth',4)
hold off
title('Filter Response'),...
xlabel('Omega'), ylabel('Phase'), grid
[Nd, Wn] = cheb2ord(Wp/pi, Ws/pi, Rp, Rs);
input('coefficients for difference equation');
[B,A] = cheby2(Nd,Rs,Wn)
[Z,P,K] = cheby2(Nd,Rs,Wn);
filtnum=fftshift(fft(B,512));
filtden=fftshift(fft(A,512));
filtfreq=filtnum./filtden;
fmag=20*log10(abs(filtfreq));
fmag=fmag-max(fmag);
clf
subplot(121)
polar(0,1,'.')
hold on
plot(real(Z),imag(Z),'o','MarkerSize',20,'LineWidth',4);
plot(real(P),imag(P),'x','MarkerSize',20,'LineWidth',4);
hold off
subplot(122)
plot(freqomega,fmag,'LineWidth',4);
axis([-pi pi -60 0]);
hold on
plot([Wp Wp],[-60 0],'m--','LineWidth',4)
plot([Ws Ws],[-60 0],'m--','LineWidth',4)
hold off
title('Chebyshev II Filter'),...
xlabel('Omega'), ylabel('Magnitude (dB)'), grid

