% A quick demonstration of bodeplot and twopole, using a low pass filter.


freq = logspace(-2,2,300);

Q = [1/3, 1/2, .577, 1/sqrt(2), 1];
colors='rgbkm';

% Prepare to make 6 figures with bodeplot.
for fig=1:6
   figure(fig)
   hold off
endfor

% Make the plots
for n=1:numel(colors) % Cycling through all the desired filter parameters
   resp = twopole(Q(n), freq);
   bodeplot(freq, resp, colors(n));
endfor
figure(1)
title('Gain of 2nd-order filters','fontsize',18)
xlabel('Frequency (Hz)','fontsize',16)
ylabel('Gain (dB)','fontsize',16)
legend('RC-RC (Q=1/3)', 'RC>RC (Q=1/2)',...
   'Bessel (Q=.577)', 'Butterworth (Q=.707)', 'Q=1')
axis([0.01,100,-80,5])
print('2ndOrderGain.pdf')
axis([0.1,10,-20,5])
print('2ndOrderGainZoom.pdf')

figure(2)
title('Phase of 2nd order filters','fontsize',18)
xlabel('Frequency (Hz)','fontsize',16)
ylabel('Phase (degrees)','fontsize',16)
legend('RC-RC (Q=1/3)', 'RC>RC (Q=1/2)',...
   'Bessel (Q=.577)', 'Butterworth (Q=.707)', 'Q=1')
print('2ndOrderPhase.pdf')

figure(3)
title('Time delay of 2nd order filters','fontsize',18)
xlabel('Frequency (Hz)','fontsize',16)
ylabel('Time delay (fc = 1)','fontsize',16)
legend('RC-RC (Q=1/3)', 'RC>RC (Q=1/2)',...
   'Bessel (Q=.577)', 'Butterworth (Q=.707)', 'Q=1',...
   'location','southeast')
print('2ndOrderTimeDelay.pdf')

figure(4)
title('Time delay vs. attenuation of 2nd order filters','fontsize',18)
xlabel('Attenuation (dB)','fontsize',16)
ylabel('Time delay (s)','fontsize',16)
legend('RC-RC (Q=1/3)', 'RC>RC (Q=1/2)',...
   'Bessel (Q=.577)', 'Butterworth (Q=.707)', 'Q=1')
axis([-3,10])
print('2ndOrderTimeDelay-Attenuation.pdf')

figure(5)
title('Step response of 2nd order filters','fontsize',18)
xlabel('Time (s)','fontsize',16)
ylabel('Response','fontsize',16)
legend('RC-RC (Q=1/3)', 'RC>RC (Q=1/2)',...
   'Bessel (Q=.577)', 'Butterworth (Q=.707)', 'Q=1')
axis([-1,2])
print('2ndOrderStepResponse.pdf')

figure(6)
title('Impulse response of 2nd order filters','fontsize',18)
xlabel('Time (s)','fontsize',16)
ylabel('Response','fontsize',16)
legend('RC-RC (Q=1/3)', 'RC>RC (Q=1/2)',...
   'Bessel (Q=.577)', 'Butterworth (Q=.707)', 'Q=1')
axis([-1,2])
print('2ndOrderImpulseResponse.pdf')
