%
%
% Digital Butterworth bandpass
% satisfy stop band spec
%
%  T = 1/1000
%
% 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 ) ) ];
wp=1; ws=min(wsh)
P1=-Gs/10; P2=-Gp/10; Wsp=ws/wp;
nc=log((10^P1 - 1)/(10^P2 - 1))/(2*log(Wsp));
n= ceil(nc);
fprintf('Filter order is %i\n', n)
%
% Step 3: determine two wc's, one that exactly satisfies passband,
%  and the other satisfies stopband.
Wcp = wp/(10^P2-1)^(1/(2*n));
Wcs = ws/(10^P1-1)^(1/(2*n));
Wcp, Wcs
%
% Determine two prototype LP transfer functions
[num1, den1] = butter(n, Wcp, 's');
[num2, den2] = butter(n, Wcs, 's');
%
%  Display prototype filter transfer function
%  Cutoff freq is the one that meets stopband spec.
disp('The prototype analog lowpass transfer function is')
format short e
disp('This satisfies pass band and oversatisfies stop band');
num1, den1
disp('This satisfies stop band and oversatisfies pass band');
num2, den2
%
%  Now design bandpass
% first adjust Wp's based on cutoff freq of Butterworth
%  Conversion from buttord() function
W0 = [-Wcp Wcp];
Whcp = -W0*(wp2-wp1)/2 + sqrt( W0.^2/4*(wp2-wp1)^2 + wp1*wp2);
Whcp = sort(abs(Whcp));
W0 = [-Wcs Wcs];
Whcs = -W0*(wp2-wp1)/2 + sqrt( W0.^2/4*(wp2-wp1)^2 + wp1*wp2);
Whcs = sort(abs(Whcs));
%
% Determine two transfer functions and display magnitudes
[num1, den1] = butter(n, Whcp, 's');
[num2, den2] = butter(n, Whcs, 's');
[numd1, dend1] = bilinear(num1, den1, 1/T);
[numd2, dend2] = bilinear(num2, den2, 1/T);
%
%  Now display final filter transfer function
%  Cutoff freq is the one that meets stopband spec.
disp('The final bandpass transfer function is')
format short e
disp('This satisfies pass band and oversatisfies stop band');
numd1, dend1
disp('This satisfies stop band and oversatisfies pass band');
numd2, dend2
%
% 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
mag2s=abs(freqz(numd2, dend2, zs));
mag2p=abs(freqz(numd2, dend2, zp));
mag2s, mag2p
%
%
%
%
%  Do comparison between two digital filters
%
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));
H=freqz(numd2, dend2, z);
H2mag=abs(H);
H2phs=180/pi*unwrap(angle(H));
subplot(211);
plot(wd, H1mag,  'c');
title('Example 12.8, Butterworth design, red meets stop spec');
ylabel('Mag');
xlabel('Discrete freq (rad/sec)');
hold on;
plot(wd, H2mag, 'r');
grid;
hold off;
zoom on;
subplot(212);
plot(wd, H1phs,  'c');
ylabel('Phase (deg)');
hold on;
plot(wd, H2phs, 'r');
grid;
hold off;
%
%
%  Now repeat using buttord and butter 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] = buttord( Wpa, Wsa, -Gp, -Gs);
[b,a]=butter(n,Wn);
%
% Display this result
disp('The transfer function using buttord and butter is')
b,a