Ex No 7
10.10.2020
IIR Filter Design
Aim:
To generate a MATLAB code that designs various types of filters, to plot the transfer characteristics, and to filter
out input data sequences
Software used:
MATLAB
Code:
1.Design a digital Butterworth IIR filter using bilinear transformation to meet the following specification:
Pass band edge frequency:200 Hz = 0.2pi rad/sec = 1256.64 rad/sec
Stop band edge frequency:800 Hz = 0.8pi rad/sec = 5026.55 rad/sec
Pass band attenuation: 2dB ; Stop band attenuation:35dB ; Sampling rate: 2kHz
(i)Find the magnitude response and phase response of the system
(ii)Find the spectrum of input and output signals if the input signal is
x(t)=cos(2πf1t)+2cos(2πf2t)+2sin#(2πf3t), where, f1=50Hz, f2=100Hz and f3=950Hz.
%LPF analog design to dgital design(butter)
%Initiaizing the inputs
fs=2000;
N=fs;
f1=50;f2=100;f3=950;
t=0:N-1;
%Finding order and cutoff frequency of the filter
[order,cut_freq]=buttord(2*pi*200,2*pi*800,2,35,'s');
[num,denom]=butter(order,cut_freq,'low','s'); %Coefficients of transfer function
w=0:6000;
[h,W]=freqs(num,denom,w); %Finding the transfer function
disp('The order of the filter is');
The order of the filter is
disp(order);
disp('The coefficients of numerator polynomial are:');
The coefficients of numerator polynomial are:
disp(num);
1.0e+13 *
1
0 0 0 0 1.1354
disp('The coefficients of denominator polynomial are:');
The coefficients of denominator polynomial are:
disp(denom);
1.0e+13 *
0.0000 0.0000 0.0000 0.0016 1.1354
figure(1)
w1=w/(2*pi); %Normalising to frequency scale
plot(w1,20*log10(abs(h)));
title('Analog HPF Filter response');
xlabel('Frequencies(Hz)');
ylabel('H[w] in db');
[num_d,denom_d]=bilinear(num,denom,fs); %Analog to Digital
figure(2)
freqz(num_d,denom_d);
title('Digital LPF Filter response');
2
x = cos(2*pi*f1*t/fs)+2*cos(2*pi*f2*t/fs) + 2*sin(2*pi*f3*t/fs); %Input signal
ip_dft=fft(x,N);
y=filter(num_d,denom_d,x);
op_dft=fft(y,N);
figure(3)
%subplot(211)
stem(abs(ip_dft)); %Plotting input spectrum
ylim([0 max(abs(ip_dft))+1]);
xlabel('K');
ylabel('Amplitude');
title('InputSpectrum');
3
figure(4)
%subplot(212)
stem(abs(op_dft)); %Plotting input spectrum
ylim([0 max(abs(ip_dft))+1]);
xlabel('K');
ylabel('Amplitude');
title('OututSpectrum');
4
2.Design a digital Chebyshev IIR filter using bilinear transformation to meet the following specification:
Pass band edge frequency:800 Hz = 0.8pi rad/sec = 5026.55 rad/sec
Stop band edge frequency:200 Hz = 0.2pi rad/sec = 1256.64 rad/sec
Pass band attenuation: 2dB ; Stop band attenuation:35dB ; Sampling rate: 2kHz
(i)Find the magnitude response and phase response of the system
(ii)Find the spectrum of input and output signals if the input signal is
x(t)=cos(2πf1t)+2cos(2πf2t)+2sin#(2πf3t), where, f1=50Hz, f2=100Hz and f3=950Hz.
%HPF analog design - dgital design(Cheby)
%Initiaizing the inputs
fs=2000;
N=fs;
f1=50;f2=100;f3=950;
t=0:N-1;
[order,cut_freq]=cheb1ord(2*pi*800,2*pi*200,2,35,'s'); %Finding order and cutoof frequency
[num,denom]=cheby1(order,2,2*pi*800,'high','s');
w=0:6000;
[h,W]=freqs(num,denom,w); %Finding the transfer function
disp('The order of the filter is');
The order of the filter is
5
disp(order);
disp('The coefficients of numerator polynomial are:');
The coefficients of numerator polynomial are:
disp(num);
1 0 0 0
disp('The coefficients of denominator polynomial are:');
The coefficients of denominator polynomial are:
disp(denom);
1.0e+11 *
0.0000 0.0000 0.0006 3.8852
figure(1)
w1=w/(2*pi); %Normalising to frequency scale
plot(w1,20*log10(abs(h)));
title('Analog HPF Filter response');
xlabel('Frequencies(Hz)');
ylabel('H[w] in db');
6
[num_d,denom_d]=bilinear(num,denom,fs); %Analog to digital
figure(2)
freqz(num_d,denom_d);
title('Digital HPF Filter response');
x = cos(2*pi*f1*t/fs)+2*cos(2*pi*f2*t/fs) + 2*sin(2*pi*f3*t/fs); %Input signal
ip_dft=fft(x,N);
y=filter(num_d,denom_d,x);
op_dft=fft(y,N);
figure(3)
%subplot(211)
stem(abs(ip_dft)); %Plotting input spectrum
ylim([0 max(abs(ip_dft))+1]);
xlabel('K');
ylabel('Amplitude');
title('InputSpectrum');
7
figure(4)
%subplot(212)
stem(abs(op_dft)); %Plotting output spectrum
ylim([0 max(abs(ip_dft))+1]);
xlabel('K');
ylabel('Amplitude');
title('OututSpectrum');
8
3.Design a digital Butterworth IIR to meet the following specifications:
Pass band edge frequency:200 Hz = 0.2pi rad/sec = 0.1571 rad/sec(normalised)
Stop band edge frequency:800 Hz = 0.8pi rad/sec = 0.6283 rad/sec(normalised)
Pass band attenuation: 2dB ; Stop band attenuation:35dB ;Sampling rate: 2kHz
(i)Find the magnitude response and phase response of the system
(ii)Find the spectrum of input and output signals if the input signal is
x(t)=cos(2πf1t)+2cos(2πf2t)+2sin#(2πf3t), where, f1=50Hz, f2=100Hz and f3=950Hz.
%LPF direct Digital design(butter)
%Initializing Inputs
fs=2000;
N=fs;
f1=50;f2=100;f3=950;
t=0:N-1;
wp=200*2*pi/8000; %Normalizing the frequencies
ws=800*2*pi/8000;
[order,cut_freq]=buttord(wp,ws,2,35); %Finding order and cutoff frequency
[num,denom]=butter(order,cut_freq,'low'); %Finding transfer function coefficients
disp('The order of the filter is');
The order of the filter is
9
disp(order);
disp('The coefficients of numerator polynomial are:');
The coefficients of numerator polynomial are:
disp(num);
0.0285 0.0855 0.0855 0.0285
disp('The coefficients of denominator polynomial are:');
The coefficients of denominator polynomial are:
disp(denom);
1.0000 -1.5216 0.9624 -0.2127
figure(1)
freqz(num,denom);
title('Digital LPF Filter response');
x = cos(2*pi*f1*t/fs)+2*cos(2*pi*f2*t/fs) + 2*sin(2*pi*f3*t/fs); %Input signal
ip_dft=fft(x,N);
y=filter(num,denom,x);
op_dft=fft(y,N);
10
figure(2)
%subplot(211)
stem(abs(ip_dft)); %Plotting input spectrum
ylim([0 max(abs(ip_dft))+1]);
xlabel('K');
ylabel('Amplitude');
title('InputSpectrum');
figure(3)
%subplot(212)
stem(abs(op_dft)); %Plotting output spectrum
ylim([0 max(abs(ip_dft))+1]);
xlabel('K');
ylabel('Amplitude');
title('OututSpectrum');
11
4.Design a digital Chebyshev IIR to meet the following specifications:
Pass band edge frequency:800 Hz = 0.8pi rad/sec = 0.6283 rad/sec(normalised)
Stop band edge frequency:200 Hz = 0.2pi rad/sec = 0.1571 rad/sec(normalised)
Pass band attenuation: 2dB ; Stop band attenuation:35dB ; Sampling rate: 2kHz
(i)Find the magnitude response and phase response of the system
(ii)Find the spectrum of input and output signals if the input signal is
x(t)=cos(2πf1t)+2cos(2πf2t)+2sin#(2πf3t), where, f1=50Hz, f2=100Hz and f3=950Hz.
%HPF direct Digital design(chebyshev)
%Initializing Inputs
fs=2000;
N=fs;
f1=50;f2=100;f3=950;
t=0:N-1;
wp=800*2*pi/8000; %Normalizing the frequencies
ws=200*2*pi/8000;
[order,cut_freq]=cheb1ord(wp,ws,2,35); %Finding order and cutoff frequency
[num,denom]=cheby1(order,2,cut_freq,'high'); %Finding transfer function coefficients
disp('The order of the filter is');
The order of the filter is
12
disp(order);
disp('The coefficients of numerator polynomial are:');
The coefficients of numerator polynomial are:
disp(num);
0.0465 -0.1395 0.1395 -0.0465
disp('The coefficients of denominator polynomial are:');
The coefficients of denominator polynomial are:
disp(denom);
1.0000 1.3603 1.1584 0.4263
figure(1)
freqz(num,denom);
title('Digital HPF Filter response');
x = cos(2*pi*f1*t/fs)+2*cos(2*pi*f2*t/fs) + 2*sin(2*pi*f3*t/fs); %Input signal
ip_dft=fft(x,N);
y=filter(num,denom,x);
op_dft=fft(y,N);
13
figure(2)
%subplot(211)
stem(abs(ip_dft)); %Plotting input spectrum
ylim([0 max(abs(ip_dft))+1]);
xlabel('K');
ylabel('Amplitude');
title('InputSpectrum');
figure(3)
%subplot(212)
stem(abs(op_dft)); %Plotting output spectrum
ylim([0 max(abs(ip_dft))+1]);
xlabel('K');
ylabel('Amplitude');
title('OututSpectrum');
14
5.(i)Design an Analog BandPass IIR filter to meet the following specifications:
Pass band lower cut-off frequency: 50 Hz = 314.16 rad/sec
Pass band upper cut-off frequency: 20 kHz = 125.667 krad/sec
Stop band lower cut-off frequency: 20 Hz = 125.67 rad/sec
Stop band upper cut-off frequency: 45 kHz = 282.745 krad/sec
Pass band attenuation: 3dB ; Stop band attenuation:20dB ; Find the monotonic frequency response of the
system.
%Initialising the inputs
wp=[2*pi*50,2*pi*20000];
ws=[2*pi*20,2*pi*45000];
[order,cut_freq]=buttord(wp,ws,3,20,'s'); %Finding order and cutoff frequency
[num,denom]=butter(order,cut_freq,'bandpass','s'); %Finding transfer function coefficients
disp('The order of the filter is');
The order of the filter is
disp(order);
disp('The coefficients of numerator polynomial are:');
15
The coefficients of numerator polynomial are:
disp(num);
1.0e+15 *
0 0 0 2.2684 0 0 0
disp('The coefficients of denominator polynomial are:');
The coefficients of denominator polynomial are:
disp(denom);
1.0e+22 *
0.0000 0.0000 0.0000 0.0000 0.0001 0.0410 6.1529
w=0:500000;
[h,W]=freqs(num,denom,w); %Finding the transfer function
%w1=w/(2*pi); %Normalising to frequency scale
figure(1)
plot(w,20*log10(h));
Warning: Imaginary parts of complex X and/or Y arguments ignored.
xlim([min(w)-100000 inf]);
title('Analog BPF Filter response');
xlabel('w(rad/sec)');
ylabel('H[w] in db');
16
5.(ii)Design a Digital BandPass IIR filter using impulse invariance method to meet the following specifications:
Pass band lower cut-off frequency: 50 Hz = 314.16 rad/sec
Pass band upper cut-off frequency: 20 kHz = 125.667 krad/sec
Stop band lower cut-off frequency: 20 Hz = 125.67 rad/sec
Stop band upper cut-off frequency: 45 kHz = 282.745 krad/sec
Pass band attenuation: 3dB ; Stop band attenuation:20dB ; Sampling rate: 100kHz ;Find the monotonic
frequency response of the system.
fs=100000;
wp=[2*pi*50,2*pi*20000];
ws=[2*pi*20,2*pi*45000];
[order,cut_freq]=buttord(wp,ws,3,20,'s'); %Finding order and cutoff frequency
[num,denom]=butter(order,cut_freq,'bandpass','s'); %Finding transfer function coefficients
disp('The order of the filter is');
The order of the filter is
disp(order);
disp('The coefficients of numerator polynomial are:');
The coefficients of numerator polynomial are:
disp(num);
1.0e+15 *
0 0 0 2.2684 0 0 0
disp('The coefficients of denominator polynomial are:');
The coefficients of denominator polynomial are:
disp(denom);
1.0e+22 *
0.0000 0.0000 0.0000 0.0000 0.0001 0.0410 6.1529
figure(1)
[num_d,denom_d]=impinvar(num,denom,fs);
freqz(num_d,denom_d);
title('Digital BPF after Impulse invariance');
17
6.(i)Design an Analog Bandstop IIR filter to meet the following specifications:
Pass band lower cut-off frequency: 25 Hz = 157.07 rad/sec
Pass band upper cut-off frequency: 225 Hz = 1413.72 rad/sec
Stop band lower cut-off frequency: 100 Hz = 628.32 rad/sec
Stop band upper cut-off frequency: 150 Hz = 942.48 rad/sec
Pass band attenuation: 3dB ; Stop band attenuation:18dB ;Find the monotonic frequency response of the
system.
%Initialising the inputs
wp=[2*pi*25,2*pi*225];
ws=[2*pi*100,2*pi*150];
[order,cut_freq]=buttord(wp,ws,3,18,'s'); %Finding order and cutoff frequency
[num,denom]=butter(order,cut_freq,'stop','s'); %Finding transfer function coefficients
disp('The order of the filter is');
The order of the filter is
disp(order);
disp('The coefficients of numerator polynomial are:');
18
The coefficients of numerator polynomial are:
disp(num);
1.0e+11 *
0.0000 0 0.0000 0 3.5067
disp('The coefficients of denominator polynomial are:');
The coefficients of denominator polynomial are:
disp(denom);
1.0e+11 *
0.0000 0.0000 0.0000 0.0074 3.5067
figure(1)
w=0:2500;
[h,W]=freqs(num,denom,w); %Finding the transfer function
plot(w,20*log10(abs(h)));
title('Analog BSF Filter response');
xlabel('w(rad/sec)');
ylabel('H[w] in db');
6.(ii)Design a Digital Bandstop IIR filter using bilinear transformation to meet the following specifications:
Pass band lower cut-off frequency: 25 Hz = 157.07 rad/sec
19
Pass band upper cut-off frequency: 225 Hz = 1413.72 rad/sec
Stop band lower cut-off frequency: 100 Hz = 628.32 rad/sec
Stop band upper cut-off frequency: 150 Hz = 942.48 rad/sec
Pass band attenuation: 3dB ; Stop band attenuation:18dB ;Sampling rate: 500Hz
Find the monotonic frequency response of the system.
fs=500;
wp=[2*pi*25,2*pi*225];
ws=[2*pi*100,2*pi*150];
[order,cut_freq]=buttord(wp,ws,3,20,'s'); %Finding order and cutoff frequency
[num,denom]=butter(order,cut_freq,'stop','s'); %Finding transfer function coefficients
disp('The order of the filter is');
The order of the filter is
disp(order);
disp('The coefficients of numerator polynomial are:');
The coefficients of numerator polynomial are:
disp(num);
1.0e+11 *
0.0000 0 0.0000 0 3.5067
disp('The coefficients of denominator polynomial are:');
The coefficients of denominator polynomial are:
disp(denom);
1.0e+11 *
0.0000 0.0000 0.0000 0.0083 3.5067
figure(1)
[num_d,denom_d]=bilinear(num,denom,fs);
freqz(num_d,denom_d);
title('Digital BSF after BLT');
20
21