%% Bandpass IIR Filter Design Using Chebyshev Type I Analog Filter
% Design the prototype for a 10th-order Chebyshev type I bandpass filter with 3 dB of ripple in the passband. Convert it to state-space form.

[z,p,k] = cheb1ap(10,3);
[A,B,C,D] = zp2ss(z,p,k);  

%
% Create an analog filter with sample rate f_s =2\;\textrm{kHz}, prewarped band edges u_1 \;and u_2 in rad/s, bandwidth B_w =u_2 -u_1 and center frequency W_o =\sqrt{u_1 u_2 } for use with lp2bp. Specify the passband edge frequencies as 100 Hz and 500 Hz.

Fs = 2e3;
u1 = 2*Fs*tan(100*(2*pi/Fs)/2);   
u2 = 2*Fs*tan(500*(2*pi/Fs)/2); 
Bw = u2 - u1;                     
Wo = sqrt(u1*u2);                 
[At,Bt,Ct,Dt] = lp2bp(A,B,C,D,Wo,Bw);
[b,a] = ss2tf(At,Bt,Ct,Dt);        

%
% Calculate the frequency response of the analog filter using freqs. Plot the magnitude response and the prewarped frequency band edges.

[h,w] = freqs(b,a);     
plot(w,mag2db(abs(h)))
hold on
ylim([-165 5])
[U1,U2] = meshgrid([u1 u2],ylim);
plot(U1,U2)
legend('Magnitude response','Lower Passband Edge','Upper Passband Edge')
hold off
xlabel('Angular Frequency (rad/s)')
ylabel('Magnitude (dB)')
grid

%
% Use bilinear to create a digital bandpass filter with sample rate f_s and lower band edge 100 Hz. Convert the digital filter from state-space form to transfer function form using ss2tf.

[Ad,Bd,Cd,Dd] = bilinear(At,Bt,Ct,Dt,Fs);

[bz,az] = ss2tf(Ad,Bd,Cd,Dd);

%
% Use fvtool to plot the magnitude response of the digital filter.

fvtool(bz,az,'Fs',Fs)

%
% Copyright 2018 The MathWorks, Inc.