%
% Chebychev bandstop
%
% Step 1; determine n of prototype lowpass filter
wp1=60; wp2=260; ws1=100; ws2=150; Gp=-2.2; Gs=-20;

r=-Gp;
wsh = [ ( ws1 * ( wp2-wp1 ) / (  wp1*wp2 - ws1*ws1) );
        ( ws2 * ( wp2-wp1 ) / ( -wp1*wp2 + ws2*ws2) ) ];
wp=1; ws=min(wsh)
P1=-Gs/10; P2=r/10; Wsp=ws;
nc=acosh( sqrt( (10^P1-1)/(10^P2-1) ) )/(acosh(Wsp));
n= ceil(nc);
fprintf('Filter order is %i\n', n)

% Determine two transfer functions and display magnitudes
[num1, den1] = cheby1(n, r, 1, 's');

w=logspace(-.3, .3);
[mag1, phase1, w] = bode( num1, den1, w);
semilogx(w, mag1);
title('Prototype lowpass filters');
hold off

%  Now design bandstop
Whcp = [wp1 wp2];
[num1, den1] = cheby1(n, r, Whcp, 'stop', 's');

w=logspace(1.0, 3.0);
[mag1, phase1, w] = bode( num1, den1, w);
semilogx(w, mag1);
title('Final Bandstop filter');
hold off

%  Now display final filter transfer function

disp('The final bandstop transfer function is')
format short e
num1, den1
format

% Figure response at stop and pass
[mag1s, phase1, x] = bode(num1, den1, [ws1 ws2]);
[mag1p, phase1, x] = bode(num1, den1, [wp1 wp2]);
mag1s, mag1p

