0% found this document useful (0 votes)
12 views18 pages

IIR DESIGN

The document details the design of Digital IIR filters (Lowpass, Highpass, Bandpass, and Bandstop) using MATLAB, including the generation of an input signal and the application of these filters to obtain desired output signals. It provides specifications for each filter type, the design process using Butterworth filters, and plots of the frequency responses. The document also includes MATLAB code snippets for filter design and signal processing.

Uploaded by

talabattulanash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views18 pages

IIR DESIGN

The document details the design of Digital IIR filters (Lowpass, Highpass, Bandpass, and Bandstop) using MATLAB, including the generation of an input signal and the application of these filters to obtain desired output signals. It provides specifications for each filter type, the design process using Butterworth filters, and plots of the frequency responses. The document also includes MATLAB code snippets for filter design and signal processing.

Uploaded by

talabattulanash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Table of Contents

IIR FILTER DESIGN USING MATLAB ................................................................................ 1


Input Signal ....................................................................................................................... 1
LowPass Digital IIR Filter .................................................................................................... 3
High Digital IIR Filter ......................................................................................................... 7
BandPass Digital IIR Filter ................................................................................................. 10
BandStop Digital IIR Filter ................................................................................................. 14

IIR FILTER DESIGN USING MATLAB


This script designs Digital IIR Lowpass, highpass, bandpass and bandstop
filters and apply these filters on an input signal x[n] to get desired
signals y1[n], y2[n], y3[n] and y4[n] respectively. Frequency response
of the designed filters have been plotted. The input and all four filtered
have been plotted in both time and frequency domain.

[email protected]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Input Signal
% input signal generation
n = 0:1:100;

w1 = 0.1*pi; % w1 = 0.1pi rad/sample


w2 = 0.5*pi; % w2 = 0.5 pi rad/sample
w3 = 0.9*pi; % w3 = 0.9pi rad/sample

xn = cos(w1*n) + cos(w2*n) + cos(w3*n);

% compute N=256 point DFT


N_dft = 256;
X_dft = fft(xn, N_dft);

% find magnitude response of x[n]


X_dft_mag = abs(fftshift(X_dft));

% plot input signal in time and frequency domain


figure(1);
subplot(1,1,1);
stem(n,xn,'-b','LineWidth',2);
grid on;
axis([0 100 -2.2 2.2])
grid on;
xlabel('index n');
ylabel('Input Signal');

1
% plot magnitude response
figure(2);
w_k = (-128:128-1) * (2*pi/256);
subplot(1,1,1);
plot(w_k/pi, X_dft_mag, '-r','LineWidth',2);
grid on,
xlabel('frequency in pi units');
ylabel('Magnitude')
title('Magnitude Response of the Input Signal');

2
LowPass Digital IIR Filter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% In this task, we wish to pass only the first frequency w1 through
the
% filter and block rest of the two frequencies using a Lowpass
Digital IIR Filter.
%
% LOW PASS FILTER DESIGN
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Given the digital low pass filtr specifications


w_p = 0.2*pi; %pass band edge frequency in rad/sample (determine from
the problem)
w_s = 0.4*pi; % stop band edge frequency in rad/sample (determine
from the problem)
R_p = 3; % passband ripple in dB (given or assume)
A_s = 30; % stopband attenuation in dB

% Chose Ts
Ts = 2; % sec

% Derive the analog prototype low pass filter specifications


Omega_p = (2/Ts)*tan(w_p/2); %pre_warp pass band edge frequency

3
Omega_s = (2/Ts)*tan(w_s/2); % pre_warp stop band edge frequency

% Butterworth analog prototype filter design


% first of all, we will find the minimum order of the filter
% which satisfies the above requirements
[N,Omega_c] = buttord(Omega_p,Omega_s,R_p,A_s,'s');

% Find the analog Lowpass filter transfer function


[bs_LP, as_LP] = butter(N,Omega_c,'s');

% Analog to digital transformation (Bilinear transform)


[bz_LP, az_LP] = bilinear(bs_LP, as_LP, 1/Ts);

% plot magnitude and phase response of realized filter


% H is the N-point response at frequency vector w in radians/sample
[H_LP,w] = freqz(bz_LP, az_LP);
figure(3);
subplot(2,1,1);
plot (w/pi,20*log10(abs(H_LP)),'-b','linewidth',2);
axis([0 1 -80 5]);
grid on;
xlabel('frequency(Hz)'); ylabel('Gain, dB');
title('IIR Butterworth LowPass Filter');
subplot(2,1,2);
plot (w/pi, angle(H_LP)/pi,'-r','linewidth',2);
axis([0 1 -1.25 1.25]);
grid on;
xlabel('frequency(Hz)');
ylabel('Normalized phase (wrt pi)');
title('IIR Butterworth LowPass Filter');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% filer x[n] using the designed filter
yn = filter(bz_LP, az_LP, xn);

% compute N=100 point DFT


y_dft = fft(yn, N_dft);

% find magnitude
Y_dft_mag = abs(fftshift(y_dft));

% plot input and output


figure(4);
stem(n,yn,'-b','LineWidth',2);
axis([0 100 -1.2 1.2])
grid on;
xlabel('index n');
ylabel('Filtered Signal Using the Lowpass Filter');

% plot frequency response


figure(5);
plot(w_k/pi, Y_dft_mag,'-r', 'LineWidth',2);
grid on,
xlabel('frequency in pi units');
ylabel('Magnitude')

4
title('Magnitude Response of the Filtered Signal');

5
6
High Digital IIR Filter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% In this task, we wish to pass only the last frequency w3 through
the
% filter and block rest of the two frequencies w1 and w2 using a
Highpass Digital IIR Filter.
%
%
% HIGH PASS FILTER
DESIGN
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Given the digital high pass filter specifications


w_s = 0.6*pi; %Stop band edge frequency in rad/sample (determine from
the problem)
w_p = 0.8*pi; % Pass band edge frequency in rad/sample (determine
from the problem)
R_p = 3; % passband ripple in dB (given or assume)
A_s = 30; % stopband attenuation in dB

% Chose Ts
Ts = 2; % sec

% Determine the analog prototype high pass filter specifications


Omega_p_HP = (2/Ts)*tan(w_p/2); %pre_warp pass band edge frequency
Omega_s_HP = (2/Ts)*tan(w_s/2); % pre_warp stop band edge frequency

% Determine the analog prototype low pass filter specifications


Omega_p_LP = 1;
Omega_s_LP = (Omega_p_HP)*(Omega_p_LP)/Omega_s_HP;

% Butterworth analog prototype filter design


% first of all, we will find the minimum order of the filter
% which satisfies the above requirements
[N,Omega_c] = buttord(Omega_p_LP,Omega_s_LP,R_p,A_s,'s');

% Find the analog Lowpass filter transfer function


[bs_LP, as_LP] = butter(N,Omega_c,'s');

% Transform to the analog highpass transfer function


[bs_HP, as_HP] = lp2hp(bs_LP, as_LP,Omega_p_HP );

% Analog to digital transformation (Bilinear transform)


[bz_HP, az_HP] = bilinear(bs_HP, as_HP, 1/Ts);

% plot magnitude and phase response of realized filter


% H is the N-point response at frequency vector w in radians/sample
[H_HP,w] = freqz(bz_HP, az_HP);
figure(6);
subplot(2,1,1);

7
plot (w/pi,20*log10(abs(H_HP)),'-b','linewidth',2);
axis([0 1 -80 5]);
grid on;
xlabel('frequency(Hz)'); ylabel('Gain, dB');
title('IIR Butterworth HighPass Filter');
subplot(2,1,2);
plot (w/pi, angle(H_HP)/pi,'-r','linewidth',2);
axis([0 1 -1.25 1.25]);
grid on;
xlabel('frequency(Hz)');
ylabel('Normalized phase (wrt pi)');
title('IIR Butterworth HighPass Filter');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% filer x[n] using the designed filter
yn = filter(bz_HP, az_HP, xn);

% compute N=100 point DFT


y_dft = fft(yn, N_dft);

% find magnitude
Y_dft_mag = abs(fftshift(y_dft));

% plot input and output


figure(7);
stem(n,yn,'-b','LineWidth',2);
axis([0 100 -1.2 1.2])
grid on;
xlabel('index n');
ylabel('Filtered Signal Using the HighPass Filter');

% plot frequency response


figure(5);
plot(w_k/pi, Y_dft_mag,'-r', 'LineWidth',2);
grid on,
xlabel('frequency in pi units');
ylabel('Magnitude')
title('Magnitude Response of the Filtered Signal');

8
9
BandPass Digital IIR Filter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% In this task, we wish to pass only the middle frequency w2 through
the
% filter and block rest of the two frequencies w1 and w3 using a
Bandpass Digital IIR Filter.
%
%
% BAND PASS FILTER
DESIGN
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Given the digital band pass filter specifications


w_s1 = 0.2*pi; %Stop band lower edge frequency in rad/sample
(determine from the problem)
w_p1 = 0.4*pi; % Pass band lower edge frequency in rad/sample
(determine from the problem)
w_p2 = 0.6*pi; %Pass band upper edge frequency in rad/sample
(determine from the problem)
w_s2 = 0.8*pi; % Stop band upper edge frequency in rad/sample
(determine from the problem)
R_p = 3; % passband ripple in dB (given or assume)
A_s = 30; % stopband attenuation in dB

10
% Chose Ts
Ts = 2; % sec

% Determine the analog prototype Band pass filter specifications


Omega_s1_BP = (2/Ts)*tan(w_s1/2); %pre_warp stop band edge frequency
Omega_p1_BP = (2/Ts)*tan(w_p1/2); % pre_warp pass band edge frequency
Omega_p2_BP = (2/Ts)*tan(w_p2/2); %pre_warp pass band edge frequency
Omega_s2_BP = (2/Ts)*tan(w_s2/2); % pre_warp stop band edge frequency

% Determine the analog prototype low pass filter specifications (few


steps
% here)

% First check if Omega_s1_BP*Omega_s1_BP = Omega_s1_BP*Omega_s1_BP


% Else adjust one of the stop band edge frequencies

if (Omega_s1_BP*Omega_s2_BP ~= Omega_p1_BP*Omega_p2_BP)

Omega_s1_BP = (Omega_p1_BP*Omega_p2_BP)/ Omega_s2_BP;


end

% Determine the centre frequency and the BW


Omega_0 = sqrt(Omega_p1_BP*Omega_p2_BP);
BW_BP = Omega_p2_BP - Omega_p1_BP;

% Determine LowPass analog filter specifications


Omega_p_LP = 1;
Omega_s_LP = (Omega_p_LP* (Omega_0^2 - Omega_s1_BP^2))/
(Omega_s1_BP*BW_BP);

% Butterworth analog prototype filter design


% first of all, we will find the minimum order of the filter
% which satisfies the above requirements
[N,Omega_c] = buttord(Omega_p_LP,Omega_s_LP,R_p,A_s,'s');

% Find the analog Lowpass filter transfer function


[bs_LP, as_LP] = butter(N,Omega_c,'s');

% Transform to the analog highpass transfer function


[bs_BP, as_BP] = lp2bp(bs_LP, as_LP,Omega_0, BW_BP );

% Analog to digital transformation (Bilinear transform)


[bz_BP, az_BP] = bilinear(bs_BP, as_BP, 1/Ts);

% plot magnitude and phase response of realized filter


% H is the N-point response at frequency vector w in radians/sample
[H_BP,w] = freqz(bz_BP, az_BP);
figure(9);
subplot(2,1,1);
plot (w/pi,20*log10(abs(H_BP)),'-b','linewidth',2);
axis([0 1 -80 5]);
grid on;

11
xlabel('frequency(Hz)'); ylabel('Gain, dB');
title('IIR Butterworth BandPass Filter');
subplot(2,1,2);
plot (w/pi, angle(H_BP)/pi,'-r','linewidth',2);
axis([0 1 -1.25 1.25]);
grid on;
xlabel('frequency(Hz)');
ylabel('Normalized phase (wrt pi)');
title('IIR Butterworth BandPass Filter');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% filter x[n] using the designed filter
yn = filter(bz_BP, az_BP, xn);

% compute N=100 point DFT


y_dft = fft(yn, N_dft);

% find magnitude
Y_dft_mag = abs(fftshift(y_dft));

% plot input and output


figure(10);
stem(n,yn,'-b','LineWidth',2);
axis([0 100 -1.2 1.2])
grid on;
xlabel('index n');
ylabel('Filtered Signal Using the BandPass Filter');

% plot frequency response


figure(5);
plot(w_k/pi, Y_dft_mag, '-r','LineWidth',2);
grid on,
xlabel('frequency in pi units');
ylabel('Magnitude')
title('Magnitude Response of the Filtered Signal');

12
13
BandStop Digital IIR Filter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% In this task, we wish to block only the middle frequency w2
through the
% filter and pass rest of the two frequencies w1 and w3 using a
Bandstop Digital IIR Filter.
%
%
% BAND STOP DIGITAL IIR
FILTER DESIGN
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Given the digital bandstop filtr specifications


w_p1 = 0.2*pi; %pass band lower edge frequency in rad/sample
(determine from the problem)
w_s1 = 0.4*pi; % stop band lower edge frequency in rad/sample
(determine from the problem)
w_s2 = 0.6*pi; %stop band upper edge frequency in rad/sample
(determine from the problem)
w_p2 = 0.8*pi; % pass band upper edge frequency in rad/sample
(determine from the problem)
R_p = 3; % passband ripple in dB (given or assume)
A_s = 30; % stopband attenuation in dB

14
% Chose Ts
Ts = 2 ; % sec

% Determine the analog prototype Band pass filter specifications


Omega_p1_BS = (2/Ts)*tan(w_s1/2); %pre_warp pass band edge frequency
Omega_s1_BS = (2/Ts)*tan(w_p1/2); % pre_warp stop band edge frequency
Omega_s2_BS = (2/Ts)*tan(w_p2/2); %pre_warp stop band edge frequency
Omega_p2_BS = (2/Ts)*tan(w_s2/2); % pre_warp pass band edge frequency

% Determine the analog prototype low pass filter specifications (few


steps
% here)

% First check if Omega_p1_BS*Omega_p2_BS = Omega_s1_BS*Omega_s2_BS


% Else adjust one of the stop band edge frequencies

if (Omega_s1_BS*Omega_s2_BS ~= Omega_p1_BS*Omega_p2_BS)

Omega_p1_BS = (Omega_s1_BS*Omega_s2_BS)/ Omega_p2_BS;


end

% Determine the centre frequency and the BW


Omega_0 = sqrt(Omega_s1_BS*Omega_s2_BS);
BW_BS = Omega_s2_BS - Omega_s1_BS;

% Determine LowPass analog filter specifications


Omega_s_LP = 1;
Omega_p_LP = (Omega_s_LP* (Omega_p1_BS*BW_BS)/(Omega_0^2 -
Omega_p1_BS^2));

% Butterworth analog prototype filter design


% first of all, we will find the minimum order of the filter
% which satisfies the above requirements
[N,Omega_c] = buttord(Omega_p_LP,Omega_s_LP,R_p,A_s,'s');

% Find the analog Lowpass filter transfer function


[bs_LP, as_LP] = butter(N,Omega_c,'s');

% Transform to the analog bandstop transfer function


[bs_BS, as_BS] = lp2bs(bs_LP, as_LP,Omega_0, BW_BS );

% Analog to digital transformation (Bilinear transform)


[bz_BS, az_BS] = bilinear(bs_BS, as_BS, 1/Ts);

% plot magnitude and phase response of realized filter


% H is the N-point response at frequency vector w in radians/sample
[H_BS,w] = freqz(bz_BS, az_BS);
figure(12);
subplot(2,1,1);
plot (w/pi,20*log10(abs(H_BS)),'linewidth',2);
axis([0 1 -80 5]);
grid on;

15
xlabel('frequency(Hz)'); ylabel('Gain, dB');
title('IIR Butterworth BandStop Filter');
subplot(2,1,2);
plot (w/pi, angle(H_BS)/pi,'linewidth',2);
axis([0 1 -1.25 1.25]);
grid on;
xlabel('frequency(Hz)');
ylabel('Normalized phase (wrt pi)');
title('IIR Butterworth BandStop Filter');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% filer x[n] using the designed filter
yn = filter(bz_BS, az_BS, xn);

% compute N=100 point DFT


y_dft = fft(yn, N_dft);

% find magnitude
Y_dft_mag = abs(fftshift(y_dft));

% plot input and output


figure(13);
stem(n,yn,'-b','LineWidth',2);
axis([0 100 -1.2 1.2])
grid on;
xlabel('index n');
ylabel('Filtered Signal Using the BandStop Filter');

% plot frequency response


figure(5);
plot(w_k/pi, Y_dft_mag,'-r', 'LineWidth',2);
grid on,
xlabel('frequency in pi units');
ylabel('Magnitude')
title('Magnitude Response of the Filtered Signal]');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

16
17
Published with MATLAB® R2021a

18

You might also like