0% found this document useful (0 votes)
104 views

Lab Sheet 6 Filter Design Using MATLAB: Lab Session 6.1: Implementation of Linear Phase FIR Filter

This document provides information about designing digital filters using MATLAB. It discusses linear phase FIR filters, including the four basic types of linear phase impulse responses and MATLAB functions to implement each type. It also covers window design techniques, including commonly used windows like rectangular, Hamming, Hanning, and Blackman. MATLAB functions for designing FIR filters using different windows are presented. The document provides examples of designing lowpass, bandpass, and bandstop filters in MATLAB using functions like fir1, kaiserord, and firpm. It instructs students to try different parameters and window types and comment on the results.

Uploaded by

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

Lab Sheet 6 Filter Design Using MATLAB: Lab Session 6.1: Implementation of Linear Phase FIR Filter

This document provides information about designing digital filters using MATLAB. It discusses linear phase FIR filters, including the four basic types of linear phase impulse responses and MATLAB functions to implement each type. It also covers window design techniques, including commonly used windows like rectangular, Hamming, Hanning, and Blackman. MATLAB functions for designing FIR filters using different windows are presented. The document provides examples of designing lowpass, bandpass, and bandstop filters in MATLAB using functions like fir1, kaiserord, and firpm. It instructs students to try different parameters and window types and comment on the results.

Uploaded by

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

Lab Sheet

Filter Design 6
using MATLAB
• A fundamental knowledge on MATLAB

• A theoretical knowledge on Digital Filter.

• design digital filter using MATLAB.

• understand different characteristics of Digital Filter.

• Lab Session 6.1: Implementation of linear phase FIR filter


• Introduction
• Specification of a lowpass filter
• Types of linear phase FIR filter
• MATLAB Functions to implement Linear Phase FIR Filters
• Lab Session 6.2: Window Design Techniques
6.2.1 Introduction
6.2.2 Commonly used windows
6.2.3 MATLAB built in functions to design FIR filter using windows
6.2.4 Design Example
• Lab Session 6.3: In Lab Evaluation
• Home work

Lab Session 6.1: Implementation of linear phase FIR


filter
6.1.1. Introduction
In many applications like speech or audio signal processing, digital filters are used to
implement frequency-selective operations. Therefore, specifications are required in the
frequency-domain in terms of the desired magnitude and phase response of the filter.
Generally, a linear phase response in the passband is desirable. In the case of FIR filters, it is
possible to have exact linear phase. In the case of IIR filters a linear phase in the passband is
not achievable. Hence we will consider magnitude-only specifications.

The magnitude specifications in general provide requirements in decibels (dB), given by

This approach is the most popular one in practice and is used for both FIR and IIR filters.

6.1.2. Specification of a lowpass filter:


A typical absolute and relative specification of a lowpass filter are shown in Figure 6.1a,b in
which

Fig 6.1: FIR filter specifications: (a) absolute (b) relative


A typical absolute specification of a lowpass filter is shown in Figure 6.1a, in which,

• band [0, ωp] is called the passband, and δ1 is the tolerance (or ripple) that we are
willing to accept in the ideal passband response,
• band [ωs, π] is called the stopband, and δ2 is the corresponding tolerance (or
ripple),
• band [ωp, ωs] is called the transition band, and there are no restrictions on the
magnitude response in this band.
A typical relative specification of a lowpass filter is shown in Figure 6.1b,
in which
• Rp is the passband ripple in dB.
• As is the stopband attenuation in dB.
The parameters given in these two specifications are obviously related.
Since |H(ejω)|max in absolute specifications is equal to (1 + δ1), we have

Example 6.0 : In a certain filter’s specifications the passband ripple is 0.25 dB, and the
stopband attenuation is 50 dB. Determine δ1 and δ2.
Solution:

6.1.3 Types of linear phase FIR filter


FOUR TYPES OF LINEAR-PHASE FIR FILTERS
Linear-phase FIR filter can be divided into four basic types.
impulse response

Type

I symmetric

II symmetric

III anti-symmetric

IV anti-symmetric

6.1.4. MATLAB Functions to implement Linear Phase FIR Filters


Type-1:

function [Hr,w,a,L] = Hr_Type1(h);


% Computes Amplitude response Hr(w) of a Type-1 LP FIR filter
% -----------------------------------------------------------
% [Hr,w,a,L] = Hr_Type1(h)
% Hr = Amplitude Response
% w = 500 frequencies between [0 pi] over which Hr is computed
% a = Type-1 LP filter coefficients
% L = Order of Hr
% h = Type-1 LP filter impulse response
%
M = length(h); L = (M-1)/2;
a = [h(L+1) 2*h(L:-1:1)]; % 1x(L+1) row vector
n = [0:1:L]; % (L+1)x1 column vector
w = [0:1:500]'*pi/500; Hr = cos(w*n)*a';
end
Type-2
function [Hr,w,b,L] = Hr_Type2(h);
% Computes Amplitude response of a Type-2 LP FIR filter
% -----------------------------------------------------
% [Hr,w,b,L] = Hr_Type2(h)
% Hr = Amplitude Response
% w = frequencies between [0 pi] over which Hr is computed
% b = Type-2 LP filter coefficients
% L = Order of Hr
% h = Type-2 LP impulse response
%
M = length(h); L = M/2;
b = 2*[h(L:-1:1)]; n = [1:1:L]; n = n-0.5;
w = [0:1:500]'*pi/500; Hr = cos(w*n)*b';
end
Type-3
function [Hr,w,c,L] = Hr_Type3(h);
% Computes Amplitude response Hr(w) of a Type-3 LP FIR filter
% -----------------------------------------------------------
% [Hr,w,c,L] = Hr_Type3(h)
% Hr = Amplitude Response
% w = frequencies between [0 pi] over which Hr is computed
% c = Type-3 LP filter coefficients
% L = Order of Hr
% h = Type-3 LP impulse response
%
M = length(h); L = (M-1)/2;
c = [2*h(L+1:-1:1)]; n = [0:1:L];
w = [0:1:500]'*pi/500; Hr = sin(w*n)*c';
end
Type-4
function [Hr,w,d,L] = Hr_Type4(h);
% Computes Amplitude response of a Type-4 LP FIR filter
% -----------------------------------------------------
% [Hr,w,d,L] = Hr_Type4(h)
% Hr = Amplitude Response
% w = frequencies between [0 pi] over which Hr is computed
% d = Type-4 LP filter coefficients
% L = Order of d
% h = Type-4 LP impulse response
%
M = length(h); L = M/2;
d = 2*[h(L:-1:1)]; n = [1:1:L]; n = n-0.5;
w = [0:1:500]'*pi/500; Hr = sin(w*n)*d';
end

Example 6.1:<Type-1 Linear Phase FIR Filter>


Solution:

MATLAB Code:
Output:
Similar problems:(Solve in lab)
Type-2:

Type-3:

Type-4:

6.1.5 FIR Filter Design using fir1 function


The MATLAB function fir1() designs conventional lowpass, highpass, bandpass, and
bandstop linear-phase FIR filters based on the windowing method. The command:
b = fir1(N,Wn)
returns in vector b the impulse response of a lowpass filter of order N. The cut-off frequency
Wn must be between 0 and 1 with 1 corresponding to the half sampling rate.

The command: b = fir1(N,Wn,'high')returns the impulse response of a highpass filter of


order N with normalized cutoff frequency Wn.

Similarly, b = fir1(N,Wn,'stop') with Wn a two-element vector designating the stopband


designs a bandstop filter.
Without explicit specification, the Hamming window is employed in the design. Other
windowing functions can be used by specifying the windowing function as an extra argument
of the function. For example, Blackman window can be used instead by the command b =
fir1(N, Wn, blackman(N)).

Example 6.2
Design a 48th-order FIR bandpass filter with passband 0.35 ≤ w ≤ 0.65.
MATLAB Code:

Output:

Example 6.3
Design a lowpass filter with passband defined from 0 to 1 kHz and stopband
defined from 1500 Hz to 4 kHz. Specify a passband ripple of 5% and a
stopband attenuation of 40 dB. Use kaiserord FIR order estimator (lowpass,
highpass, bandpass, multiband).

[N,Wn,BTA,FILTYPE] = kaiserord(F,A,DEV,Fs) is the approximate order N,


normalized frequency band edges Wn, Kaiser window beta parameter BTA
and filter type FILTYPE to be used by the FIR1 function:
B = FIR1(N, Wn, FILTYPE, kaiser( N+1,BTA ), 'noscale' )

MATLAB Code:
If empty or omitted, fir1 uses a Hamming window of length n+1.
Output:
W = kaiser(N,BTA) returns the BETA-valued N-point Kaiser window.

Task1: Try changing the parameters such as fcuts and fsamp and make comments in your
report. There is a restriction between cut-off frequency and sampling frequency, can you find
that ?

Task2: Use Rectangular, Hamming, Hanning and Blackman window instead of Kaiser
window (use same window length). Which one is the best? and make comments in your report.
W=window(@rectwin,n+1)
W=window(@hamming,n+1)
W=window(@hann,n+1)
W=window(@blackman,n+1)

Parks-McClellan FIR filter design


Parks-McClellan is the standard method for FIR filter design.
The MATLAB function firpm() designs a linear-phase FIR filter using the Parks-McClellan
algorithm. The Parks-McClellan algorithm uses the Remez exchange algorithm and Chebyshev
approximation theory to design filters with an optimal fit between the desired and actual
frequency responses. The filters are optimal in the sense that the maximum error between the
desired frequency response and the actual frequency response is minimized.
Example 6.4: Graph the desired and actual frequency responses of a 17th-order Parks-
McClellan bandpass filter:
MATLAB Code:

Output:
Task1: Try changing the parameters and make comments in your report

Lab Session 6.2: Window Design Techniques


6.2.1 Introduction
Windows are used in the design of digital filters to convert an "ideal" impulse response
of infinite duration, such as a sinc function, to a finite impulse response (FIR) filter
design. In general, we need a IIR filter to get an ideal frequency response, but IIR is
computationally expensive. So we move to FIR filter to save time, and it requires that we
must truncate the impulse response at both ends with respect to the central. Even we
truncate the impulse response when it is small enough, but such a sudden cutoff will cause
some undesired effects. The window method will reduce them.
In the time domain, windowing means that the we multiply the desired (usually ideal)
infinite duration impulse response hd(n) by a finite duration window (or window
function) w(n) to get a soft truncation. The resulted impulse response h(n) of the designed
filter is the product
h(n) = hd(n) w(n), 0≤n≤M

6.2.2 Commonly used windows


Formula for calculation of M and β

6.2.3 MATLAB built in functions to design FIR filter using windows


MATLAB provides several functions to implement window functions discussed in this section.
A brief description of these functions follow.

Function name Return value


w=boxcar(M) returns the M-point rectangular window function in array w.
w=bartlett(M) returns the M-point Bartlett window function in array w.
w=hann(M) returns the M-point Hann window function in array w.
w=hamming(M) returns the M-point Hamming window function in array w.
w=blackman(M) returns the M-point Blackman window function in array w.
w=kaiser(M,beta) returns the beta-valued M-point rectangular window function in array

6.2.4 Design Example:


First we create the following two functions :
The ideal_lp function
function hd = ideal_lp(wc,M);
% Ideal LowPass filter computation
% --------------------------------
% [hd] = ideal_lp(wc,M)
% hd = ideal impulse response between 0 to M-1
% wc = cutoff frequency in radians
% M = length of the ideal filter
%
alpha = (M-1)/2; n = [0:1:(M-1)];
m = n - alpha; fc = wc/pi; hd = fc*sinc(fc*m);
end

The freqz_m function


function [db,mag,pha,grd,w] = freqz_m(b,a);
% Modified version of freqz subroutine
% ------------------------------------
% [db,mag,pha,grd,w] = freqz_m(b,a);
% db = Relative magnitude in dB computed over 0
to pi radians
% mag = absolute magnitude computed over 0 to
pi radians
% pha = Phase response in radians over 0 to pi
radians
% grd = Group delay over 0 to pi radians
% w = 501 frequency samples between 0 to pi
radians
% b = numerator polynomial of H(z) (for FIR:
b=h)
% a = denominator polynomial of H(z) (for FIR:
a=[1])
%
[H,w] = freqz(b,a,1000,'whole');
H = (H(1:1:501))'; w = (w(1:1:501))';
mag = abs(H); db =
20*log10((mag+eps)/max(mag));
pha = angle(H); grd = grpdelay(b,a,w);
end

Example 6.5: Design a digital FIR lowpass filter with the following specifications:
Choose Hamming window function. Determine the impulse response and provide a plot of the
frequency response of the designed filter.
Solution:
MATLAB Code:

Output:

At command window:
M = 67 wc = 0.7854 As = 52
Comment: Note that the filter length is M = 67, the actual stopband attenuation is 52 dB,
and the actual passband ripple is 0.0394 dB. Clearly, the passband ripple is satisfied by this
design. This practice of verifying the passband ripple is strongly recommended.
Example 6.6:
Repeat the Example using Kaiser window.
MATLAB Code:
Output:
At Command window: M = 61 beta = 4.5513 As = 52
Example 6.7:Design the following digital bandpass filter using Blackman window.

Solution: MATLAB Code:

Output:
At command window:
r_width = 0.4712, M =75.Rp = 0.0030,As = 75
Lab Session 6.3: In Lab Evaluation
ILE6.1
Homework:
1.
2.

3.
4.

You might also like