DSP Lab Spring 23 Exp-12
DSP Lab Spring 23 Exp-12
(EL-3031)
LABORATORY MANUAL
(LAB # 12)
Student Name: ______________________________________________
____________________________________________________________________________________________________________________________________________________________
Lab #
of Computer and Emerging Sciences
(EL3031) Islamabad Spring 2023
_____________________________________________________________________________________
Lab # 12: IIR Filter Design
Learning Objectives
a. To design IIR digital filter using Butterworth analog filter
b. To design IIR digital filter using Chebyshev (Type I and II) analog filter
Equipment Required
1. PC
2. Matlab
1. Introduction
Filters are frequency selective network and are used for signal separation and restoration.
Filters can be classified into:
Analog filters- Realized by R, C and active devices, they are cheap, fast and large dynamic range, but
poor performance.
Digital filters- Realized by digital system like memory, adder and multipliers; they have better
performance in low frequency as well as high frequency operation.
There are two types of filters: IIR (Infinite Impulse Response) and FIR (Finite Impulse Response)
Ideal frequency response of filters:
_____________________________________________________________________________________________
Page 2 of 18
DSP - Lab National University Roll No: __________
12
Lab #
of Computer and Emerging Sciences
(EL3031) Islamabad Spring 2023
_____________________________________________________________________________________
where h[k] is the impulse response of the filter, bk and ak are the filter coefficients of the filter,
and x[n] and y[n] are the input and output to the filter. The transfer function for the IIR filter is,
_____________________________________________________________________________________________
Page 3 of 18
DSP - Lab National University Roll No: __________
12
Lab #
of Computer and Emerging Sciences
(EL3031) Islamabad Spring 2023
_____________________________________________________________________________________
Where K is the gain, z1, z2 ….are the zeros and p1, p2 ….are the poles of the transfer function,
H(z).
Bilinear Transformation
Bilinear transformation is much more important and useful than Impulse Invariant
Transformation method, especially in high pass, band pass and stop filters. The method is
derived by approximating the first order differential equation with a difference equation.
This transformation maps the analog transfer function, H(s) from the s-plane onto the
discrete transfer function, H(z) in the z-plane. The entire jΩ axis in the s-plane is mapped
onto the z-plane. The left half s-plane is mapped inside the unit circle and the right half s-
plane is mapped outside the unit circle in the z-plane.
MATLAB Functions:
BILINEAR Bilinear transformation with optional frequency prewarping.
[Zd,Pd,Kd] = BILINEAR(Z,P,K,Fs) converts the s-domain transfer function specified by Z, P, and
K to a z-transform discrete equivalent obtained from the bilinear transformation: where column
_____________________________________________________________________________________________
Page 4 of 18
DSP - Lab National University Roll No: __________
12
Lab #
of Computer and Emerging Sciences
(EL3031) Islamabad Spring 2023
_____________________________________________________________________________________
vectors Z and P specify the zeros and poles, scalar K specifies the gain, and Fs is the sample
frequency in Hz.
[NUMd,DENd] = BILINEAR(NUM,DEN,Fs), where NUM and DEN are row vectors containing
numerator and denominator transfer function coefficients, NUM(s)/DEN(s),
in descending powers of s, transforms to z-transform coefficients NUMd(z)/DENd(z).
c. Butterworth Approximation
The magnitude response of a practical low pass filter, Where δ is gain and ω is frequency
in radians/sec
The low pass Butterworth filter is characterized by the following magnitude squared
magnitude response,
where N is the order of the filter, Ω is analog frequency in radians; ΩC is 3dB cut-off
frequency in radians.
_____________________________________________________________________________________________
Page 5 of 18
DSP - Lab National University Roll No: __________
12
Lab #
of Computer and Emerging Sciences
(EL3031) Islamabad Spring 2023
_____________________________________________________________________________________
The advantage of this approximation is flat response over stop band and pass band. The
disadvantage is wide transition band.
The transfer function of the filter is:
For N is even
For N is odd
and ck=1
The parameter Bk can be obtained from:
for N is even
for N is odd
MATLAB functions:
1) BUTTORD Butterworth filter order selection.
[N, Wn] = BUTTORD(Wp, Ws, Rp, Rs) returns the order N of the lowest order digital
Butterworth filter that loses no more than Rp dB in the passband and has at least Rs dB
of attenuation in the stopband.
Wp and Ws are the passband and stopband edge frequencies normalized from 0 to 1
(where 1 corresponds to pi radians/sample). For example,
Lowpass: Wp = .1, Ws = .2
Highpass: Wp = .2, Ws = .1
_____________________________________________________________________________________________
Page 6 of 18
DSP - Lab National University Roll No: __________
12
Lab #
of Computer and Emerging Sciences
(EL3031) Islamabad Spring 2023
_____________________________________________________________________________________
Bandpass: Wp = [.2 .7], Ws = [.1 .8]
Bandstop: Wp = [.1 .8], Ws = [.2 .7]
BUTTORD also returns Wn, the Butterworth natural frequency (or, the "3 dB frequency")
to use with BUTTER to achieve the specifications.
[N, Wn] = BUTTORD(Wp, Ws, Rp, Rs, 's') does the computation for an analog filter, in
which case Wp and Ws are in radians/second.
When Rp is chosen as 3 dB, the Wn in BUTTER is equal to Wp in BUTTORD.
d. Chebyshev Approximation
The Butterworth approximation, which is monotonic in both the pass band and stopband, gives
us a magnitude response which exceeds the specifications for the pass band ripple. The
problem is that the order of the filter will be high. Usually, we can tolerate some amount of
ripple, and it would be advantageous to have a trade-off between the ripple and the filter order,
so that we can obtain a filter which meets the specifications and has a lower order than its
Butterworth counterpart. For this we use the Chebychev approximation. The characteristic of
Chebyshev approximation is that it minimizes the error between the idealized and the actual
filter over the range of filter allowing some ripples in pass band.
Type I, with equal ripple in the pass band, monotonic in the stop band.
Type II, with equal ripple in the stop band, monotonic in the pass band
_____________________________________________________________________________________________
Page 7 of 18
DSP - Lab National University Roll No: __________
12
Lab #
of Computer and Emerging Sciences
(EL3031) Islamabad Spring 2023
_____________________________________________________________________________________
Where is a Chebyshev polynomial which exhibits equal ripple in the pass band, N is the
order of the polynomial as well as that of the filter, and ɛ determines the pass band ripple, which in
decibels is given by,
Chebyshev polynomial is given by
Assume
The transfer function H(s) is similar to Butterworth filter, but the coefficients bk and ck is given by
_____________________________________________________________________________________________
Page 8 of 18
DSP - Lab National University Roll No: __________
12
Lab #
of Computer and Emerging Sciences
(EL3031) Islamabad Spring 2023
_____________________________________________________________________________________
For N is odd
Lab #
of Computer and Emerging Sciences
(EL3031) Islamabad Spring 2023
_____________________________________________________________________________________
in which case Wp and Ws are in radians/second.
2. Procedure
Step1: Get the digital filter specifications ( δ p δ s ω p ωs )
Step2: Convert to analog filter specifications with bilinear transformation.
Step3: Design analog transfer function Ha(s)
2 1−z−1
Step4: Transfer Ha(s) to H(z) since s= T ( −1
)
1+ z
_____________________________________________________________________________________________
Page 10 of 18
DSP - Lab National University Roll No: __________
12
Lab #
of Computer and Emerging Sciences
(EL3031) Islamabad Spring 2023
_____________________________________________________________________________________
Find orders and cut-off frequencies of the analog filters using buttord( ) function and display the
values for verification
8. Compute the transfer functions in s-domain using butter( )
9. Transform the transfer functions to z-domain by impinvar( ) and bilinear( )
10. Compute the frequency responses of each type using freqz( ) function
11. Plot the each magnitude and phase responses
12. Stop
MATLAB
%Butterworth filter
clc;
clear all;
close all;
disp('IIR filter design using Butterworth Approximation');
%Inputs
ftype=input('Enter filter type (LPF, HPF, BPF or BSF): ');
switch ftype
case 'LPF'
fp=input('Enter pass band frequency fp in Hz ');
fs=input('Enter stop band frequency fs in Hz ');
%Error message
if fp<=0
error('Pass band edge must be larger than zero');
end
if fs<=fp
error('Stop band edge must be larger than pass band edge');
end
case 'HPF'
fp=input('Enter pass band frequency fp in Hz ');
fs=input('Enter stop band frequency fs in Hz ');
%Error message
if fs<=0
error('Pass band edge must be larger than 0');
end
if fs>=fp
error('Stop band edge must be smaller than pass band edge');
end
case 'BPF'
fs=input('Enter stop band frequencies fs1 and fs2 (fs2 >fs1) in Hz ');
fp=input('Enter pass band frequencies fp1 and fs2 (fp1 >fs1 and fp2<fp2) in Hz ');
if (fp(1) || fp(2))<=0
error('Pass band edge must be larger than 0');
end
if fp(1)<=fs(1)
error('Pass band edge must be larger than stop band edge');
end
if fp(2)>=fs(2)
error('Pass band edge must be smaller than stop band edge');
end
case 'BSF'
fs=input('Enter stop band frequencies fs1 and fs2 (fs2 >fs1) in Hz ');
fp=input('Enter pass band frequencies fp1 and fs2 (fp1 < fs1 and fp2 > fs2) in Hz ');
if (fs(1) || fs(2))<=0
error('Stop band edge must be larger than zero');
_____________________________________________________________________________________________
Page 11 of 18
DSP - Lab National University Roll No: __________
12
Lab #
of Computer and Emerging Sciences
(EL3031) Islamabad Spring 2023
_____________________________________________________________________________________
end
if fs(1)<=fp(1)
error('Stop band edge must be larger than pass band edge');
end
if fs(2)>=fp(2)
error('Stop band edge must be smaller than pass band edge');
end
end
Fs=input('Enter sampling frequency F in samples/sec ');
a1=input('Enter pass band ripple in dB ');
a2=input('Enter stop band ripple in dB ');
%conversion and normalization of frequencies
%pi radians/second
wp=2*fp/Fs;
ws=2*fs/Fs;
%conversion of frequency for transformation
trans=input('Enter transformation type (IIT or BT): ');
switch trans
case 'IIT'
%Analog frequencies by IIT
Wp=wp*Fs*pi;
Ws=ws*Fs*pi;
case 'BT'
%Analog frequencies by BT
Wp=2*Fs*tan(wp*pi/2);
Ws=2*Fs*tan(ws*pi/2);
end
%Order and cut-off frequencies of filter
[N,Wc]=buttord(Wp,Ws,a1,a2,'s');
%Analog Filter response
switch ftype
case 'LPF'
[b,a]=butter(N,Wc,'s');
case 'HPF'
[b,a]=butter(N,Wc,'high','s');
case 'BPF'
[b,a]=butter(N,Wc,'bandpass','s');
case 'BSF'
[b,a]=butter(N,Wc,'stop','s');
end
%Transformation, Digital filter response & Frequency response
switch trans
case 'IIT'
[bz,az]=impinvar(b,a,Fs);
[H,f]=freqz(bz,az,512,Fs);
case 'BT'
[bz,az]=bilinear(b,a,Fs);
[H,f]=freqz(bz,az,512,Fs);
end
%display N and H(z)
disp('Order of the filter is:');
disp(N);
disp('Filter coefficients are:');
disp('a');
_____________________________________________________________________________________________
Page 12 of 18
DSP - Lab National University Roll No: __________
12
Lab #
of Computer and Emerging Sciences
(EL3031) Islamabad Spring 2023
_____________________________________________________________________________________
disp(az);
disp('b');
disp(bz);
%Plotting responses
subplot(2,1,1);
plot(f,abs(H));
xlabel('f in Hz');
ylabel('|H(w)|');
title(['Magnitude response of IIR ',ftype,' filter with Butterworth approximation using ',trans]);
grid on;
subplot(2,1,2);
plot(f,angle(H));
xlabel('f in Hz');
ylabel('Phase of H(w)');
title(['Phase response of IIR ',ftype,' filter with Butterworth approximation using ',trans]);
grid on;
MATLAB
%Chebychev filter
clc;
clear all;
close all;
disp('IIR filter design using Chebyshev Approximation');
%Inputs
ftype=input('Enter filter type (LPF, HPF, BPF or BSF): ');
switch ftype
case 'LPF'
fp=input('Enter pass band frequency fp in Hz ');
fs=input('Enter stop band frequency fs in Hz ');
%Error message
if fp<=0
error('Pass band edge must be larger than 0');
end
if fs<=fp
error('Stop band edge must be larger than pass band edge');
_____________________________________________________________________________________________
Page 13 of 18
DSP - Lab National University Roll No: __________
12
Lab #
of Computer and Emerging Sciences
(EL3031) Islamabad Spring 2023
_____________________________________________________________________________________
end
case 'HPF'
fp=input('Enter pass band frequency fp in Hz ');
fs=input('Enter stop band frequency fs in Hz ');
%Error message
if fs<=0
error('Pass band edge must be larger than 0');
end
if fs>=fp
error('Stop band edge must be smaller than pass band edge');
end
case 'BPF'
fs=input('Enter stop band frequencies fs1 and fs2 (fs2 >fs1) in Hz ');
fp=input('Enter pass band frequencies fp1 and fs2 (fp1 >fs1 and fp2<fp2) in Hz ');
if (fp(1) || fp(2))<=0
error('Pass band edge must be larger than 0');
end
if fp(1)<=fs(1)
error('Pass band edge must be larger than stop band edge');
end
if fp(2)>=fs(2)
error('Pass band edge must be smaller than stop band edge');
end
case 'BSF'
fs=input('Enter stop band frequencies fs1 and fs2 (fs2 >fs1) in Hz ');
fp=input('Enter pass band frequencies fp1 and fs2 (fp1 < fs1 and fp2 > fs2) in Hz ');
if (fs(1) || fs(2))<=0
error('Stop band edge must be larger than 0');
end
if fs(1)<=fp(1)
error('Stop band edge must be larger than pass band edge');
end
if fs(2)>=fp(2)
error('Stop band edge must be smaller than pass band edge');
end
end
Fs=input('Enter sampling frequency Fs in samples/sec ');
a1=input('Enter pass band ripple in dB ');
a2=input('Enter stop band ripple in dB ');
%conversion and normalization of frequencies
%pi radians/second
wp=2*fp/Fs;
ws=2*fs/Fs;
%conversion of frequency for transformation
trans=input('Enter transformation type (IIT or BT): ');
switch trans
case 'IIT'
%Analog frequencies by IIT
Wp=wp*Fs*pi;
Ws=ws*Fs*pi;
case 'BT'
%Analog frequencies by BT
Wp=2*Fs*tan(wp*pi/2);
Ws=2*Fs*tan(ws*pi/2);
_____________________________________________________________________________________________
Page 14 of 18
DSP - Lab National University Roll No: __________
12
Lab #
of Computer and Emerging Sciences
(EL3031) Islamabad Spring 2023
_____________________________________________________________________________________
end
%Cheby1 or Cheby2
type=input('Enter Chebyshev filter type (type-1 or type-2): ');
switch type
case 'type-1'
%Order and cut-off frequencies of filter
[N,Wc]=cheb1ord(Wp,Ws,a1,a2,'s');
switch ftype
case 'LPF' %Analog Filter response
[b,a]=cheby1(N,a1,Wc,'s');
case 'HPF'
[b,a]=cheby1(N,a1,Wc,'high','s');
case 'BPF'
[b,a]=cheby1(N,a1,Wc,'bandpass','s');
case 'BSF'
[b,a]=cheby1(N,a1,Wc,'stop','s');
end
case 'type-2'
%Order and cut-off frequencies of filter
[N,Wc]=cheb2ord(Wp,Ws,a1,a2,'s');
switch ftype
case 'LPF'
%Analog Filter response
[b,a]=cheby2(N,a2,Wc,'s');
case 'HPF'
[b,a]=cheby2(N,a2,Wc,'high','s');
case 'BPF'
[b,a]=cheby2(N,a2,Wc,'bandpass','s');
case 'BSF'
[b,a]=cheby2(N,a2,Wc,'stop','s');
end
end
%Transformation, Digital filter response & Frequency response
switch trans
case 'IIT'
[bz,az]=impinvar(b,a,Fs);
[H,f]=freqz(bz,az,512,Fs);
case 'BT'
[bz,az]=bilinear(b,a,Fs);
[H,f]=freqz(bz,az,512,Fs);
end
%display N and H(z)
disp('Order of the filter is:');
disp(N);
disp('Filter coefficients are:');
disp('a');
disp(az);
disp('b');
disp(bz);
%Plotting responses
subplot(2,1,1);
plot(f,20*log10(abs(H))); % Gain in dB
xlabel('f in Hz');
ylabel('|H(w)|');
_____________________________________________________________________________________________
Page 15 of 18
DSP - Lab National University Roll No: __________
12
Lab #
of Computer and Emerging Sciences
(EL3031) Islamabad Spring 2023
_____________________________________________________________________________________
title(['Magnitude response of IIR ',ftype,' filter with Chebyshev ',type,' approximation using
',trans]);
grid on;
subplot(2,1,2);
plot(f,angle(H));
xlabel('f in Hz');
ylabel('Phase of H(w)');
title(['Phase response of IIR ',ftype,' filter with Chebyshev ',type,' approximation using',trans]);
3. Exercise
Task#1 IIR Lowpass Filter Design:
Consider the low-pass discrete-time filter specification,
0.99≤|H(ejw)| ≤1.01 , |w|≤0.4π,
|H(ejw)| ≤0.001 , 0.6π ≤|w|≤ π
Design using MATLAB this low pass filter by butterworth, Chebyshev( I and II), elliptic
approximation. Plot the gain response, detailted plot of magnitude in pass-band and pole-zero
plot for each approximation.
Design using MATLAB this band-pass filter by butterworth, Chebyshev( I and II), elliptic
approximation. Plot the gain response, detailted plot of magnitude in pass-band and pole-zero
plot for each approximation
Student's feedback: Purpose of feedback is to know the strengths and weaknesses of the system
for future improvements. This feedback is for the 'current lab session'. Circle your choice:
_____________________________________________________________________________________________
Page 16 of 18
DSP - Lab National University Roll No: __________
12
Lab #
of Computer and Emerging Sciences
(EL3031) Islamabad Spring 2023
_____________________________________________________________________________________
[-3 = Extremely Poor, -2 = Very Poor, -1 = Poor, 0 = Average, 1 = Good, 2 = Very Good, 3 = Excellent]:
The following table should describe your experience with:
S# Field Rating Describe in words if required
1 Overall Session -
-2 -1 0 1 2 3
3
2 Lab Instructor -
-2 -1 0 1 2 3
3
3 Lab Staff -
-2 -1 0 1 2 3
3
4 Equipment -
-2 -1 0 1 2 3
3
5 Atmosphere -
-2 -1 0 1 2 3
3
Correctness
Conclusion
Originality
of results
Initiative
Neatness
MARKS
Attitude
TOTAL
TOTAL 10 10 10 20 20 30 100
EARNED
_____________________________________________________________________________________________
Page 17 of 18
DSP - Lab National University Roll No: __________
12
Lab #
of Computer and Emerging Sciences
(EL3031) Islamabad Spring 2023
_____________________________________________________________________________________
Assessment Rubric
LLO Statement Assessment Exemplary Proficient Developing Beginning Worst
Method (20%) (20%) (20%) (20%) Performance
(20%)
2 Design of finite- Practical Skill Able to attempt Able to attempt Able to attempt Able to attempt Able to attempt
impulse response Observation complete lab with 80% of the lab 60% of the lab 40% of the lab 20% of the lab
(FIR) and during proper tasks tasks tasks tasks
infinite-impulse experimentati labeling/explanatio
response (IIR) on & Lab n of results and
digital filters in Reports proper commenting
MATLAB. of the code
_____________________________________________________________________________________________
Page 18 of 18