% Demonstrate twostage() by cascading some filters
freq = 10.^(-3:0.001:3);

% Begin with a clean graphical slate...
for fig=1:6
   figure(fig)
   hold off
endfor

% 8-pole cascaded, buffered LC
Q = [0.5, 0.5, 0.5, 0.5];
fn = 3.324*[1,1,1,1];
LC8 = multistage(freq, Q, fn);
bodeplot(freq, LC8, 'k')

% 8-pole Butterworth
Q = [0.510, 0.601, 0.900, 2.564];
fn = [1,1,1,1];
Butter8 = multistage(freq, Q, fn);
bodeplot(freq, Butter8, 'm')

% 8-pole Bessel
Q = [0.506, 0.560, 0.711, 1.225];
fn = [1.781, 1.835, 1.956, 2.192];
Bessel8 = multistage(freq, Q, fn);
bodeplot(freq, Bessel8, 'r')

% 8-pole Chebyshev, 0.5dB ripple
Q = [0.677, 1.610, 3.460, 11.494];
fn = [0.297, 0.599, 0.861, 1.006];
Cheb8a = multistage(freq, Q, fn);
bodeplot(freq, Cheb8a, 'g')

% 8-pole Chebyshev, 2dB ripple
Q = [0.892, 2.532, 5.587, 18.519];
fn = [0.238, 0.572, 0.842, 0.990];
Cheb8b = multistage(freq, Q, fn);
bodeplot(freq, Cheb8b, 'b')


legendtext=['Cascaded LC';'Butterworth';'Bessel';...
   'Chebyshev (0.5dB ripple)';'Chebyshev (2.0dB ripple)'];

figure(1)
title('Gain of 8th-order filters','fontsize',18)
xlabel('Frequency (Hz)','fontsize',16)
ylabel('Gain (dB)','fontsize',16)
legend(legendtext)
axis([0.01,100,-160,2])
print('8thOrderGain.pdf')
axis([0.1,10,-20,5])
print('8thOrderGainZoom.pdf')

figure(2)
title('Phase of 8th-order filters','fontsize',18)
xlabel('Frequency (Hz)','fontsize',16)
ylabel('Phase (degrees)','fontsize',16)
legend(legendtext)
print('8thOrderPhase.pdf')

figure(3)
title('Time delay of 8th order filters','fontsize',18)
xlabel('Frequency (Hz)','fontsize',16)
ylabel('Time delay (fc = 1)','fontsize',16)
legend(legendtext)
print('8thOrderTimeDelay.pdf')

figure(4)
title('Time delay vs. attenuation of 8th order filters','fontsize',18)
xlabel('Attenuation (dB)','fontsize',16)
ylabel('Time delay (s)','fontsize',16)
legend(legendtext)
axis([-3,10])
print('8thOrderTimeDelay-Attenuation.pdf')

figure(5)
title('Step response of 8th order filters','fontsize',18)
xlabel('Time (s)','fontsize',16)
ylabel('Response','fontsize',16)
legend(legendtext)
axis([-6,16])
print('8thOrderStepResponse.pdf')

figure(6)
title('Impulse response of 8th order filters','fontsize',18)
xlabel('Time (s)','fontsize',16)
ylabel('Response','fontsize',16)
legend(legendtext)
axis([-6,16])
print('8thOrderImpulseResponse.pdf')
