%
% Digital Chebyshev highpass design
%
% Analog specs
wpha=15; wsha=10; Gp=-1; Gs=-6.3;
T = pi/100;

% Step 1: prewarp pass and stop frequencies
wph=(2/T)*tan(wpha*T*0.5);
wsh=(2/T)*tan(wsha*T*0.5);

% Step 2: determine n
r=-Gp;
wp=1; ws=wph/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 analog prototype low pass
[num1, den1] = cheby1(n, r, wp, 's');

% Display prototype low pass transfer function
disp('The prototype lowpass transfer function is')
format short e
num1, den1

% Step 4 design analog high pass and transform to digital
[num1, den1] = cheby1(n, r, wph, 'high', 's');
[numd1, dend1] = bilinear(num1, den1, 1/T);

%  Now display final filter transfer function
disp('The final transfer function is')
numd1, dend1
format

%
% Plot frequency response as a check
% 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));
%ubplot(211);
%plot(wd, H1mag,  'c');
%title('Chebyshev design, Example 12.7');
%ylabel('Mag');
