%HW 8 butterworth bandstop design
close all; %close any open figure windows
n=10; %filter order
[z,p,k]=buttap(n); %10-pole butterworth low-pass prototype
% with cutoff = 1 rad/s and dc gain = 1
[num,den]=zp2tf(z,p,k); %compute low-pass polynomial coefficients
h=tf(num,den); %create low-pass transfer function object
bode(h); %draw Bode plot for low-pass
title('Bode Plot for Low-pass Prototype')

%Now use lp2bs to convert low-pass prototype to desired bandstop.
%help entry for lp2bs command:
%LP2BS Lowpass to bandstop analog filter transformation.
% [NUMT,DENT] = LP2BS(NUM,DEN,Wo,Bw) transforms the lowpass filter
% prototype NUM(s)/DEN(s) with unity cutoff frequency to a
% bandstop filter with center frequency Wo and bandwidth Bw.

w1 = 1; w2 = 2; %cutoff frequencies for desired bandstop filter
cf=sqrt(w1*w2); % center frequency = geometric average of cutoffs
bw=w2-w1; %bandwidth = difference of cutoffs
[numt,dent]=lp2bs(num,den,cf,bw); %transformed polynomials
hbs=tf(numt,dent); %transformed transfer function
figure; bode(hbs); %draw Bode plot
title('Bode Plot for Bandstop')
figure; zplane(numt,dent); %draw pole-zero plot
title('Pole-Zero Plot for Bandstop')