%Design of IIR Filter
%Filter Specifications
figure(1);
f1=150; %Stopband
f2=450; 
as=40;  %Stopband att. in dB
flow=250;   %passband
fupper=350;
ap=0.5; %Passband att. in dB
fsamp=1e4;  %Sampling freq.
%specification in digital freq.
fomega=[f1,flow,fupper,f2]*2*pi/fsamp; %spec. vector
%Prewarping the digital freq.
T=2; %Parameter for prewarp. 
%Analog filter spec.
W_anal=2/T*tan(fomega/2);
%Design of anal. Ellip. filter
[N,wn]=ellipord(W_anal(2:3),W_anal(1:3:4),ap,as,'s'); 
%Note that 2N is the order (only for BPF ND NOTCH)
[B,A]=ellip(N,ap,as,wn,'s');    %analog filter coefficients
freqs(B,A) % Response of anal.

%Design of digital filter
%Using the Bilinear transf.
[b,a]=bilinear(B,A,1/T);
%b and a are the coeff. of the digital filter
f=[0:0.1:5e3]'; %freq. in Hz
omega=2*pi*f/fsamp; %dig. freq. 
H=freqz(b,a,omega); %response
magdb=20*log10(abs(H));
figure(2);
plot(f,magdb),grid on
axis([0,500,-45,1]);
xlabel('Frequency in Hz');
ylabel('Mag. Response in dB');
title(' Mag. Response of Digital Ellip BPF');
figure(3);
%plotting the attenuation
plot(f,-magdb),grid on
axis([0,500,-1,45]);
xlabel('Frequency in Hz');
ylabel('Attenuation, dB');
title('Attenuation of BPF');



