%
% Digital Chebyshev bandpass
%
%
% Analog specs
wp1a=120; wp2a=300; ws1a=45; ws2a=450; Gp=-2; Gs=-12;
T = 1/1000;
%
% Step 1: prewarp stop and pass frequencies
%
wp1=(2/T)*tan(wp1a*T*0.5);
wp2=(2/T)*tan(wp2a*T*0.5);
ws1=(2/T)*tan(ws1a*T*0.5);
ws2=(2/T)*tan(ws2a*T*0.5);
%
% Step 2: determine n of prototype lowpass filter
%
wsh = [ (  wp1*wp2 - ws1*ws1) / ( ws1 * ( wp2-wp1 ) );
        ( -wp1*wp2 + ws2*ws2) / ( ws2 * ( wp2-wp1 ) ) ];
r=-Gp;
wp=1; ws=min(wsh)
P1=-Gs/10; P2=r/10; Wsp=ws/wp;
nc=acosh( sqrt( (10^P1-1)/(10^P2-1) ) )/(acosh(Wsp));
n= ceil(nc);
fprintf('Filter order is %i\n', n)
%
% Step 3: determine transfer function of prototype lowpass
%
% Determine analog prototype LP transfer function
[num1, den1] = cheby1(n, r, 1, 's');
%  Display prototype filter transfer function
%
disp('The prototype lowpass transfer function is')
format short e
num1, den1
%
%  Now design bandpass
Whcp = [wp1 wp2];
[num1, den1] = cheby1(n, r, Whcp, 's');
[numd1, dend1] = bilinear(num1, den1, 1/T);
%
%  Now display final filter transfer function
%
disp('The final discrete bandpass transfer function is')
format short e
numd1, dend1
%
%
% Check magnitude at pass and stop freq
% must transform frequencies to discrete
disp('Checking magnitude at pass and stop frequencies:')
% transform discrete frequency to radians around unit circle for freqz()
zs=[ws1a ws2a]*T; zp=[wp1a wp2a]*T;
mag1s=abs(freqz(numd1, dend1, zs));
mag1p=abs(freqz(numd1, dend1, zp));
mag1s, mag1p
%
%
%
%  Display digital filter frequency response
%
figure(1);
%
% z is the radian frequency to be passed to freqz(), and is the
% product of wd, the discrete frequency and T, the sample period.
% wd, the discrete frequency, is z/T
%
%
z=0:pi/200:pi/2;
wd=z/T;
H=freqz(numd1, dend1, z);
H1mag=abs(H);
H1phs=180/pi*unwrap(angle(H));
subplot(211);
plot(wd, H1mag,  'c');
title('Example 12.8, Chebyshev design');
ylabel('Mag');
xlabel('Discrete freq (rad/sec)');
grid;
zoom on;
subplot(212);
plot(wd, H1phs,  'c');
ylabel('Phase (deg)');
grid;
%
%
%  Now repeat using cheb1ord and cheby1 for discrete
%  To use these functions, remember that the radian sampling
%  frequency is 2. Must normalize all pass and stop freqs by
%  multiplying by T/pi
Wpa=[wp1a wp2a]*(T/pi); Wsa=[ws1a ws2a]*(T/pi);
[n, Wn] = cheb1ord( Wpa, Wsa, -Gp, -Gs);
[b,a]=cheby1(n,-Gp,Wn);
%
% Display this result
disp('The transfer function using cheb1ord and cheby1 is')
b,a