You are on page 1of 78

Digital Signal Processing ECR 305_L10

Lecture – 10 Objectives

FIR Filter
To learn and understand
 design procedure of FIR filter.
 the frequency response, and
Design its properties of FIR filter.

IUB Dr. Abdur Razzak 1


Design of digital filters ECR 305_L10

The design of a digital filter is carried out in three steps:


Specifications: Before we can design a filter, we must have some
specifications. These specifications are determined by the
applications.

Approximations: Once the specifications are defined, we use


various concepts and mathematics that we studied so far to come
up with a filter description that approximates the given set of
specifications. This step is the topic of filter design.

Implementation: The product of the above step is a filter


description in the form of either a difference equation, or a
system function H(z) or an impulse response h(n). From this
description we implement the filter in hardware or through
software on a computer.
IUB Dr. Abdur Razzak 2
Design of digital filters (contd..) ECR 305_L10

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 pass band 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 pass band is not
achievable. Hence we will consider magnitude-only
specifications.

IUB Dr. Abdur Razzak 3


Frequency response of ideal filters ECR 305_L10

Low pass filter:


H()

0 
c 
High pass filter:
H()

H()


0
H() 0
c  

0
c 
IUB Dr. Abdur Razzak 4
Frequency response of ideal filters (contd..) ECR 305_L10

Band pass filter:

Band stop filter:


0
0 c1 0
c1 c2 
0
c2
IUB Dr. Abdur Razzak 5
Filter specifications ECR 305_L10

The magnitude specifications are given in one of two ways:

 The first approach is called absolute specifications, which provide a set of


requirements on the magnitude response function IH(ej)I. These
specifications are generally used for FIR filters. IIR filters are specified in
a somewhat different way, which we will discuss in lecture 11.

 The second approach is called relative specifications, which provide


requirements in decibels (dB) given by

H  
dB scale  20 log10 0
H   max

This approach is the most popular one in practice and is used for both FIR
and IIR filters. To illustrate these specifications, we will consider a
lowpass filter design as an example.
IUB Dr. Abdur Razzak 6
Absolute specifications ECR 305_L10

A typical absolute specification of a lowpass filter is shown in


figure below in which
 band [ 0, p ] is called the passband, and l 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), and
 band [p, s ] is called the transition band, and there are no restrictions on
the magnitude response in this band.

IUB Dr. Abdur Razzak 7


Relative specifications ECR 305_L10

A typical relative specification of a lowpass filter is shown in


figure below, in which

 Rp is the passband ripple in dB, and

 As is the stopband attenuation in dB.

IUB Dr. Abdur Razzak 8


Relation among Rp, As, l & 2 ECR 305_L10

The parameters given in the above two specifications are


obviously related.

Since IH()Imax in absolute specifications is equal to (1+l), we


have

1  1
R p  20 log 10  0 0
1  1

2
As  20 log 10  0 1
1  1

IUB Dr. Abdur Razzak 9


Example-1 ECR 305_L10

The passband ripple and stopband attenuation of a digital filter is


given by 0.25 dB and 50 dB, respectively. Determine the
passband tolerance 1 and stopband tolerance 2.

Solution:
1  1 2
R p  20 log 10 As  20 log 10
1  1 1  1
1  1 2
 0.25  20 log 10  50  20 log 10
1  1 1  0.0144
1  1
 10
0.125

1  1   2  1.014410 50 / 20
 1  0.0144  0.0032

IUB Dr. Abdur Razzak 10


Example-2 ECR 305_L10

The passband tolerance 1 and stopband tolerance 2 of a digital


filter is given by 0.01 and 0.001, respectively. Determine the
passband ripple and stopband attenuation.

Solution:

1  1 1  0.01
R p  20 log 10  20 log 10  0.1737 dB
1  1 1  0.01

2 0.001
As  20 log 10  20 log 10  60 dB
1  1 1  0.01

IUB Dr. Abdur Razzak 11


Design goal ECR 305_L10

The above specifications were given for a lowpass filter. Similar


specifications can also be given for other types of frequency-
selective filters, such as highpass, bandpass or bandstop.

However, the most important design parameters are frequency-


band tolerances (or ripples) and band-edge frequencies. Whether
the given band is a passband or a stopband is a relatively minor
issue. Therefore in describing design techniques, we will
concentrate on a lowpass filter.

Goal: Design a lowpass filter (i.e., obtain its system function


H(z) or its difference equation) that has a passband [0, p ] with
tolerance l (or Rp in dB) and a stopband [s, ] with tolerance 2
(or As in dB).
IUB Dr. Abdur Razzak 12
Advantages of FIR filters ECR 305_L10

In this lecture, we turn our attention to the design and


approximation of FIR digital filters. These filters have several
design and implementation advantages:

 The phase response can be exactly linear.

 They are relatively easy to design since there are no stability


problems.

 They are efficient to implement.

 The DFT can be used in their implementation.

IUB Dr. Abdur Razzak 13


Advantages of linear-phase filters ECR 305_L10

We are generally interested in linear-phase frequency-selective


FIR filters. Advantages of a linear-phase response are:

 design problem contains only real arithmetic and not complex


arithmetic;

 linear-phase filters provide no delay distortion and only a


fixed amount of delay;

 for the filter of length M (or order M–1) the number of


operations are of the order of M/2 as we discussed in the
linear-phase filter implementation.

IUB Dr. Abdur Razzak 14


Example-3 ECR 305_L10

The impulse response hn    1, 1, 1. Determine and draw the


frequency responses. 

Solution: 2

Transfer
H    
n 0
hn e  jn  1  e  j  e  j 2
function
 
 e  j e j  1  e  j  1  2 cos e  j

Magnitude & H    1  2 cos  , 0    


phase is
corresponding   , 0    2 / 3
phase response H     piecewise linear
   , 2 / 3    

Amplitude & H r    1  2 cos ,      phase is truly


corresponding
phase response H     ,      linear

IUB Dr. Abdur Razzak 15


Design techniques ECR 305_L10

We will discuss three design techniques, namely

the window design,

the frequency sampling design, and

the optimal equiripple design techniques for linear-phase


FIR filters.

IUB Dr. Abdur Razzak 16


Window design techniques ECR 305_L10

With a cutoff frequency of c, the frequency response of an ideal


lowpass filter of bandwidth c <  is given by
1 e  j ,   c
H d    
0, c    

where  is the sample delay with  = (M–1)/2.


The impulse response of this filter is of finite duration and is
given by 1 
hd n   F H d    H d  e
2 
1 jn
d


1 c sin c n   
 
2 c
1  e  j e jn d 
 n   
sin f c n   
 fc  f c sinc f c n   
f c n   
IUB Dr. Abdur Razzak 17
Window design techniques (contd..) ECR 305_L10

The rectangular window is defined as

1, 0  n  M 1
wn   
0, otherwise

The impulse response h(n) can be written using windowing


operation as

hn   hn n wn 
hd n , 0  n  M 1

 0, otherwise

IUB Dr. Abdur Razzak 18


Window design techniques (contd..) ECR 305_L10

In frequency domain the FIR filter response is given by the


periodic convolution.

   

H    H d    W   
1
2 
W e j H d e j    d

Fig.: Windowing operation in frequency-domain


IUB Dr. Abdur Razzak 19
Various window function ECR 305_L10

Bartlett window:

 2n M 1
 M 1 , 0n
2


wn    2  2n , M 1
 n  M 1
 M 1 2


 0, otherwise

Hann window:
   2n  0  n  M  1
0.5 1  cos ,
wn      M  1 
0, otherwise

IUB Dr. Abdur Razzak 20


Various window function (contd..) ECR 305_L10

Hamming window:

  2n  0  n  M  1
0.54  0.46 cos ,
wn     M 1 
 otherwise
0,

Blackman window:

  2n   4n  0  n  M  1
0.42  0.5 cos    
wn   
0.08 cos ,
 M 1   M 1  otherwise

 0 ,

IUB Dr. Abdur Razzak 21


Various window function (contd..) ECR 305_L10

Kaiser window:

  2n 
2

I 0  1  1   
  M 1  
wn   , 0  n  M 1
I o  

with I0 [·] is the modified zero-order Bessel function given by

 x / 2 
2
 k
I 0 x   1    
k 0  k! 

IUB Dr. Abdur Razzak 22


Various window function (contd..) ECR 305_L10

Advantages of Kaiser window: The performance of Kaiser window is


comparable to that of the Hamming window. Kaiser window provides flexible
transition bandwidth.

Disadvantages of Kaiser window: Due to complexity of the Bessel function,


the design equations for this window are not easy to derive . However Kaiser
developed empirical design equations:

Transition width:    s   p

As  7.95
Filter length: M  1
2.285
0.1102 As  8.7 , As  50

 parameter:   0.5842 As  21 0.4

  0.07886 A  21, 21  A  50
 s s

IUB Dr. Abdur Razzak 23


Various window function (contd..) ECR 305_L10

IUB Dr. Abdur Razzak 24


Example-4 ECR 305_L10

Design a digital FIR low-pass filter with the following


specifications:
p = 0.2 s = 0.3
Rp = 0.25 dB As = 50 dB
Solution:

IUB Dr. Abdur Razzak 25


Example-5 ECR 305_L10

Design a digital FIR high-pass filter with the following


specifications:
s = 0.2 p = 0.3
Rp = 0.25 dB As = 50 dB
Solution:

IUB Dr. Abdur Razzak 26


Example-6 ECR 305_L10

Design a digital FIR low-pass filter with the following


specifications:
c = 0.2 Rp = 0.25 dB As = 50 dB
M = 30, 40, 50
Solution:

IUB Dr. Abdur Razzak 27


Example-7 ECR 305_L10

Design a digital FIR band-pass filter with the following specifications:


lower stopband edge: 1s = 0.2, As = 60 dB
lower passband edge: 1p = 0.3, Rp =1 dB
upper passband edge: 2p = 0.7, Rp =1 dB
upper stopband edge: 2s = 0.8, As = 60 dB
Solution:

IUB Dr. Abdur Razzak 28


Example-8 ECR 305_L10

The frequency response of an ideal bandstop filter is given by


1, 0    /3

H    0,  / 3    2 / 3
1, 2 / 3    

Using a Kaiser window, design a bandstop filter of length 45 with stopband
attenuation of 60 dB.
Solution:

IUB Dr. Abdur Razzak 29


Example-9 ECR 305_L10

Design the following digital FIR filter with the specifications:

Low-pass: c = 0.25 N = 50

High-pass: c = 0.25 N = 50

Band-pass: c = [0.25, 0.75] N = 50

Band-stop: c = [0.35, 0.65] N = 50

IUB Dr. Abdur Razzak 30


Example-9 (contd..) ECR 305_L10

IUB Dr. Abdur Razzak 31


Example-9 (contd..) ECR 305_L10

IUB Dr. Abdur Razzak 32


Frequency sampling design technique ECR 305_L10

Basic idea: Given the ideal lowpass filter Hd(), choose the
filter length M and then sample Hd() at M equispaced
frequencies between 0 and 2.
Similar steps apply to other frequency-selective filters.
Furthermore, this idea can also be extended for approximating
arbitrary frequency-domain specifications.
There are two design approaches:
 In the first approach we use the basic idea literally and provide no
constraints on the approximation error; that is, we accept whatever error
we get from the design. This approach is called a naive design method.
 In the second approach we try to minimize error in the stopband by
varying values of the transition band samples. It results in a much better
design called an optimum design method.
IUB Dr. Abdur Razzak 33
Frequency sampling technique (contd..) ECR305_L10

The pictorial description of frequency sampling technique is


shown in figure below. From figure we observe the following:
1. The approximation error-that is, the difference between the ideal and the
actual response is zero at the sampled frequencies.
2. The approximation error at all other frequencies depends on the shape of
the ideal response; that is, the sharper the ideal response, the larger the
approximation error.
3. The error is larger near the band edges and smaller within the band.

IUB Dr. Abdur Razzak 34


Optimal equiripple design technique ECR 305_L10

The last two techniques-namely, the window design and the frequency
sampling design were easy to understand and implement. However,
they have some disadvantages.

First, we cannot specify the band frequencies p and s precisely in the


design; that is, we have to accept whatever values we obtain after the
design.

Second, we cannot specify both l and 2 ripple factors simultaneously.


Either we have l = 2 in the window design method, or we can optimize
only 2 in the frequency sampling method.

Finally, the approximation error that is, the difference between the ideal
response and the actual response is not uniformly distributed over the
band intervals. It is higher near the band edges and smaller in the regions
away from band edges.

IUB Dr. Abdur Razzak 35


Optimal equiripple design technique (contd..) ECR 305_L10

By distributing the error uniformly, we can obtain a lower-order


filter satisfying the same specifications. Fortunately, a technique
exists that can eliminate the above three problems. This technique
is somewhat difficult to understand and requires a computer for
its implementation.

For linear-phase FIR filters it is possible to derive a set of


conditions for which it can be proved that the design solution is
optimal in the sense of minimizing the maximum approximation
error (sometimes called the minimax or the Chebyshev error).

Filters that have this property are called equiripple filters because
the approximation error is uniformly distributed in both the pass-
band and the stop-band. This results in lower-order filters.
IUB Dr. Abdur Razzak 36
Optimal equiripple design technique (contd..) ECR 305_L10

IUB Dr. Abdur Razzak 37


MATLAB implementation ECR 305_L10

MATLAB
Examples

IUB Dr. Abdur Razzak 38


MATLAB Example 7.3 ECR 305_L10

The impulse response hn    1, 1, 1. Determine and draw the


frequency responses. 

Solution:
    hne  1  e  e
2
j  jn  j  j 2
H e
Transfer n 0
function
 e e  1  e   1  2 cos e
 j j  j  j

Magnitude &  
H e j  1  2 cos , 0    
phase is
corresponding   , 0    2 / 3
phase response H e   j

piecewise linear
   , 2 / 3    

Amplitude & H r    1  2 cos  ,      phase is truly


 
corresponding linear
phase response H e j   ,     
IUB Dr. Abdur Razzak 39
MATLAB Example 7.3 (contd..) ECR 305_L10

% File name: ex7p3.m Subplot(2,2,2);


plot(w/pi, angH/pi); grid
% Definition title('Piecewise linear phase response','fontsize', 15);
w = [0:500]*pi/500; ylabel('Phase in pi Radians','fontsize', 15);
axis([0 1 -1 1]);
% Calculation
Subplot(2,2,3);
% Magnitude response plot(w/pi, Hr); grid
H = (1+2*cos(w)).*exp(-j*w); title('Amplitude response','fontsize', 15);
magH = abs(H); angH = angle(H); ylabel('Amplitude','fontsize', 15);
xlabel('Frequency in pi units','fontsize', 15);
% Amplitude response
Hr = 1+2*cos(w); PhaHr = -w; Subplot(2,2,4);
plot(w/pi, PhaHr/pi);
% Plotting grid
Subplot(2,2,1); title('Linear phase response','fontsize', 15);
plot(w/pi, magH); grid xlabel('Frequency in pi units','fontsize', 15);
title('Magnitude response','fontsize', 15); ylabel('Phase in pi Radians','fontsize', 15);
ylabel('Magnitude','fontsize', 15); axis([0 1 -1 1]);

IUB Dr. Abdur Razzak 40


MATLAB Example 7.3 (contd..) ECR 305_L10

IUB Dr. Abdur Razzak 41


MATLAB function for ideal lowpass impulse response ECR 305_L10

function hd = ideal_lp(wc,M);

% Ideal LowPass filter computation


% --------------------------------------------
% hd = ideal lowpass impulse response
% wc = cutoff frequency in radians
% M = length of the ideal filter

alpha = (M-1)/2;
n = 0:M-1;
m = n-alpha;
fc = wc/pi;
hd = fc*sinc(fc*m);

IUB Dr. Abdur Razzak 42


MATLAB functions ECR 305_L10

MATLAB provides several functions to implement window


functions:
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 = harm (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 w.

Using these functions, we can use MATLAB to design FIR filters


based on the window technique, which also requires an ideal
lowpass impulse response hd(n). Therefore it is convenient to
have a simple routine that creates hd (n).
IUB Dr. Abdur Razzak 43
MATLAB example 7.8 ECR 305_L10

Design a digital FIR lowpass filter with the following


specifications:

p = 0.2 s = 0.3

Rp = 0.25 dB As = 50 dB

Solution:

IUB Dr. Abdur Razzak 44


MATLAB example 7.8 (contd..) ECR 305_L10

% Definition % Plotting
wp = 0.2*pi; ws = 0.3*pi; tr_width = ws-wp;
M = ceil(6.6*pi/tr_width)+1;
n = 0:M-1;
wc = (ws+wp)/2; % Ideal LPF cutoff frequency subplot(2,1,1)
stem(n,w_ham);
% Calculation grid
% Impulse response title('Hamming Window');
hd = ideal_lp(wc,M); xlabel('n','fontsize',15);
w_ham = (hamming(M))'; ylabel('w(n)','fontsize',15);
h = hd.*w_ham;
subplot(2,1,2)
% Frequency response plot(w/pi,db);
[H w] = freqz(h,[1],1000,'whole'); grid
H = (H(1:501))'; w = (w(1:501))'; title('Magnitude response in dB');
mag = abs(H); xlabel('Frequency in pi units');
db = 20*log10((mag+eps)/max(mag)); ylabel('Decibels','fontsize',15);

IUB Dr. Abdur Razzak 45


MATLAB example 7.8 (contd..) ECR 305_L10

IUB Dr. Abdur Razzak 46


MATLAB example 7.9 ECR 305_L10

Design a digital FIR lowpass filter with the following


specifications using the Kaiser window:

p = 0.2 s = 0.3

Rp = 0.25 dB As = 50 dB

Solution:

IUB Dr. Abdur Razzak 47


MATLAB example 7.9 (contd..) ECR 305_L10

% Definition % Plotting
wp = 0.2*pi; ws = 0.3*pi;
tr_width = ws-wp; As = 50;
M = ceil((As-7.95)/(2.285*tr_width)+1)+1
n = 0:M-1; subplot(2,1,1)
wc = (ws+wp)/2; % Ideal LPF cutoff frequency stem(n,w_kai);
beta = 0.1102*(As-8.7) grid
title(‘Kaiser Window');
% Calculation xlabel('n','fontsize',15);
% Impulse response ylabel('w(n)','fontsize',15);
hd = ideal_lp(wc,M); w_kai = (kaiser(M,beta))';
h = hd.*w_kai; subplot(2,1,2)
plot(w/pi,db);
% Frequency response grid
[H w] = freqz(h,[1],1000,'whole'); title('Magnitude response in dB');
H = (H(1:501))'; w = (w(1:501))'; mag = abs(H); xlabel('Frequency in pi units');
db = 20*log10((mag+eps)/max(mag)); ylabel('Decibels','fontsize',15);

IUB Dr. Abdur Razzak 48


MATLAB example 7.9 (contd..) ECR 305_L10

IUB Dr. Abdur Razzak 49


MATLAB example 7.81 ECR 305_L10

Design a digital FIR highpass filter with the following


specifications:

s = 0.4 p = 0.5

Rp = 0.25 dB As = 50 dB

Solution:


0

c
0

c
0

IUB Dr. Abdur Razzak 50


MATLAB example 7.81 (contd..) ECR 305_L10

% Definition % Plotting
ws = 0.2*pi; wp = 0.3*pi; tr_width = wp-ws;
M = ceil(6.6*pi/tr_width)+1
n = 0:M-1;
wc = (ws+wp)/2; % Ideal LPF cutoff frequency
subplot(2,1,1)
% Calculation stem(n,w_ham);
% Impulse response grid
hd = ideal_lp(pi,M)-ideal_lp(wc,M); title('Hamming Window');
w_ham = (hamming(M))'; xlabel('n','fontsize',15);
h = hd.*w_ham; ylabel('w(n)','fontsize',15);

% Frequency response subplot(2,1,2)


[H w] = freqz(h,[1],1000,'whole'); plot(w/pi,db);
H = (H(1:501))'; grid
w = (w(1:501))'; title('Magnitude response in dB');
mag = abs(H); xlabel('Frequency in pi units');
db = 20*log10((mag+eps)/max(mag)); ylabel('Decibels','fontsize',15);

IUB Dr. Abdur Razzak 51


MATLAB example 7.81 (contd..) ECR 305_L10

IUB Dr. Abdur Razzak 52


MATLAB example 7.88 ECR 305_L10

Design a digital FIR lowpass filter with the following


specifications:

c = 0.2 Rp = 0.25 dB As = 50 dB

M = 10, 20, 30, 40, 50, 500000

Solution:

IUB Dr. Abdur Razzak 53


MATLAB example 7.88 (contd..) ECR 305_L10

% Definition [H2 w] = freqz(h2,[1],1000,'whole');


wc = 0.2*pi; M1 = 30; M2 = 40; M3 = 50; H2 = (H2(1:501))';
n1 = 0:M1-1; n2 = 0:M2-1; n3 = 0:M3-1; w = (w(1:501))';
mag2 = abs(H2);
% Calculation db2 = 20*log10((mag2+eps)/max(mag2));
hd1 = ideal_lp(wc,M1);
w_ham1 = (hamming(M1))'; [H3 w] = freqz(h3,[1],1000,'whole');
h1 = hd1.*w_ham1; H3 = (H3(1:501))'; w = (w(1:501))';
hd2 = ideal_lp(wc,M2); mag3 = abs(H3);
w_ham2 = (hamming(M2))'; db3 = 20*log10((mag3+eps)/max(mag3));
h2 = hd2.*w_ham2;
hd3 = ideal_lp(wc,M3);
w_ham3 = (hamming(M3))';
h3 = hd3.*w_ham3; % Plotting
plot(w/pi,db1,w/pi,db2,w/pi,db3);
% Frequency response legend('M = 30','M = 40','M = 50'); grid
[H1 w] = freqz(h1,[1],1000,'whole'); title('Magnitude response in dB');
H1 = (H1(1:501))'; w = (w(1:501))'; xlabel('Frequency in pi units','fontsize',15);
mag1 = abs(H1); ylabel('Decibels','fontsize',15);
db1 = 20*log10((mag1+eps)/max(mag1)); axis([0 1 -60 25]);

IUB Dr. Abdur Razzak 54


MATLAB example 7.88 (contd..) ECR 305_L10

IUB Dr. Abdur Razzak 55


MATLAB example 7.10 ECR 305_L10

Design a digital FIR bandpass filter with the following


specifications:
lower stopband edge: 1s = 0.2, As = 60 dB
lower passband edge: 1p = 0.3, Rp =1 dB
upper passband edge: 2p = 0.7, Rp =1 dB
upper stopband edge: 2s = 0.8, As = 60 dB

Solution:

IUB Dr. Abdur Razzak 56


MATLAB example 7.10 (contd..) ECR 305_L10

% Definition % Actual passband ripple


ws1 = 0.2*pi; wp1 = 0.35*pi; delta_w = 2*pi/1000;
ws2 = 0.8*pi; wp2 = 0.65*pi; As = 60; Rp = -min(db(wp1/delta_w+1:wp2/delta_w))
tr_width = min((wp1-ws1),(ws2-wp2));
% Actual stopband attenuation
M = ceil(11*pi/tr_width)+1 As = -round(max(db(ws2/delta_w+1:501)))
n = 0:M-1;
wc1 = (ws1+wp1)/2; wc2 = (ws2+wp2)/2; % Plotting
subplot(2,1,1)
% Calculation stem(n,w_black); grid
% Impulse response title('Blackman Window');
hd = ideal_lp(wc2,M)-ideal_lp(wc1,M); xlabel('n','fontsize',15);
w_black = (blackman(M))'; h = hd.*w_black; ylabel('w(n)','fontsize',15);

% Frequency response subplot(2,1,2)


[H w] = freqz(h,[1],1000,'whole'); plot(w/pi,db); grid
H = (H(1:501))'; w = (w(1:501))'; title('Magnitude response in dB');
mag = abs(H); xlabel('Frequency in pi units');
db = 20*log10((mag+eps)/max(mag)); ylabel('Decibels','fontsize',15);

IUB Dr. Abdur Razzak 57


MATLAB example 7.10 (contd..) ECR 305_L10

IUB Dr. Abdur Razzak 58


MATLAB example 7.11 ECR 305_L10

The frequency response of an ideal bandstop filter is given by


1, 0    /3

H    0,  / 3    2 / 3
1, 2 / 3    

Using a Kaiser window, design a bandstop filter of length 45 with
stopband attenuation of 60 dB.
Solution:


0

/3 /3 2/3 


0 0

0
2/3
IUB Dr. Abdur Razzak 59
MATLAB example 7.11 (contd..) ECR 305_L10

% Definition % Plotting
M = 45; As = 60; n = 0:M-1;
beta = 0.1102*(As-8.7)
wc1 = pi/3; wc2 = 2*pi/3;
subplot(2,1,1)
% Calculation stem(n,w_kai);
% Impulse response grid
hd = ideal_lp(pi,M)-ideal_lp(wc2,M) title('Kaiser Window','fontsize',15);
+ ideal_lp(wc1,M); xlabel('n','fontsize',15);
w_kai = (kaiser(M,beta))'; ylabel('w(n)','fontsize',15);
h = hd.*w_kai;

% Frequency response subplot(2,1,2)


[H w] = freqz(h,[1],1000,'whole'); plot(w/pi,db);
H = (H(1:501))'; grid
w = (w(1:501))'; title('Magnitude response in dB','fontsize',15);
mag = abs(H); xlabel('Frequency in pi units','fontsize',15);
db = 20*log10((mag+eps)/max(mag)); ylabel('Decibels','fontsize',15);

IUB Dr. Abdur Razzak 60


MATLAB example 7.11 (contd..) ECR 305_L10

IUB Dr. Abdur Razzak 61


MATLAB implementation ECR 305_L10

The SP toolbox provides a function called firi which designs


conventional lowpass, highpass, and other multiband FIR filters
using window technique. This function’s syntax has several forms
including:
 h = fir1(N,c) designs an Nth-order (N = M - 1) lowpass FIR filter and
returns the impulse response in vector h. By default this is a Hamming-
window based, linear-phase design with normalized cutoff frequency in
c which is a number between 0 and 1, where 1 corresponds to 
rad/sample. If c is a two-element vector, i.e., c = [c1, c2], then firs
returns a bandpass filter with passband cutoffs c1 and c2. If c is a
multi-element (more than two) vector, then firl returns multiband filter
with cutoffs given in c.
 h = firl(N,wc,‘ftype’) specifies a filter type, where ftype is: (a) ‘high’ for a
highpass filter with cutoff frequency n. (b) 'stop' for a bandstop filter, if
c = [c1, c2]. The stopband frequency range is specified by this
interval.
IUB Dr. Abdur Razzak 62
MATLAB implementation (contd..) ECR 305_L10

 h = fir1(N,wc,‘ftype’,window) or h = firl(N,wc,window) uses the vector


window of length N+1 obtained from one of the specifies: MATLAB
window function. The default window function used is Hamming window.

To design FIR filters using the Kaiser window, the SP toolbox provides the
function kaiserord which estimates window parameters that can be used in the
firl function.

 The basic syntax is [N,wc,beta,ftype] = kaiserord(f,m,ripple). The


function computes the window order N, the cutoff frequency vector wc,
parameter  in beta, and the filter type ftype as discussed above. The
vector f is a vector of normalized band edges and m is a vector specifying
the desired amplitude on the bands defined by f. The length of f is twice
the length of m minus 2; i.e., f does not contain 0 or 1. The vector ripple
specifies tolerances in each band (not in decibels). Using the estimate
parameters, Kaiser window array can be computed and used in the fir1
function.
IUB Dr. Abdur Razzak 63
MATLAB example 7.82, 7.83 ECR 305_L10

Design the following digital FIR filter with the specifications:

Lowpass: c = 0.25 N = 50

Highpass: c = 0.25 N = 50

Bandpass: c = [0.25, 0.75] N = 50

Bandstop: c = [0.35, 0.65] N = 50

IUB Dr. Abdur Razzak 64


MATLAB example 7.82 (contd..) ECR 305_L10

% lowpass definition % highpass definition


wc = 0.25; N = 50; wc = 0.25; N = 50;

% Calculation (Impulse response) % Calculation (Impulse response)


h1 = fir1(N,wc); h2 = fir1(N,wc,'high');

% Frequency response % Frequency response


[H1 w] = freqz(h1,[1],1000,'whole'); [H2 w] = freqz(h2,[1],1000,'whole');
H1 = (H1(1:501))'; w = (w(1:501))'; H2 = (H2(1:501))'; w = (w(1:501))';
mag1 = abs(H1); mag2 = abs(H2);
db1 = 20*log10((mag1+eps)/max(mag1)); db2 = 20*log10((mag2+eps)/max(mag2));

% Plotting
plot(w/pi,db1,w/pi,db2); grid
legend('lowpass','highpass','fontsize',15)
title('Magnitude response in dB','fontsize',15);
xlabel('Frequency in pi units','fontsize',15);
ylabel('Decibels','fontsize',15); axis([0 1 -100 20]);

IUB Dr. Abdur Razzak 65


MATLAB example 7.82 (contd..) ECR 305_L10

IUB Dr. Abdur Razzak 66


MATLAB example 7.83 (contd..) ECR 305_L10

% Bandpass definition % Stopband definition


wcb = [0.25, 0.75]; N = 50; wcs = [0.35, 0.65]; N = 50;

% Calculation (Impulse response) % Calculation (Impulse response)


hb = fir1(N,wcb); hs = fir1(N,wcs,'stop');

% Frequency response % Frequency response


[Hb w] = freqz(hb,[1],1000,'whole'); [Hs w] = freqz(hs,[1],1000,'whole');
Hb = (Hb(1:501))'; w = (w(1:501))'; Hs = (Hs(1:501))'; w = (w(1:501))';
magb = abs(Hb); mags = abs(Hs);
dbb = 20*log10((magb+eps)/max(magb)); dbs = 20*log10((mags+eps)/max(mags));

% Plotting
plot(w/pi,dbb,w/pi,dbs,'r'); grid
legend('bandpass','stopband');
title('Magnitude response in dB','fontsize',15);
xlabel('Frequency in pi units','fontsize',15);
ylabel('Decibels','fontsize',15); axis([0 1 -80 20])

IUB Dr. Abdur Razzak 67


MATLAB example 7.83 (contd..) ECR 305_L10

IUB Dr. Abdur Razzak 68


MATLAB implementation ECR 305_L10

The SP toolbox provides a function called fir2 which combines


frequency sampling technique with the window technique to
design arbitrary shaped magnitude response FIR filters.

After computing filter impulse response using the naive design


method, fir2 then applies a selected window to minimize ripples
near the band-edge frequencies. This function's syntax also has
several forms including:

 h = fir2(N,f,m) designs an Nth-order (N = M-1) lowpass FIR


filter and returns the impulse response in vector h. The
desired magnitude response of the filter is supplied in vectors
f and m, which must be of the same length.

IUB Dr. Abdur Razzak 69


MATLAB implementation (contd..) ECR 305_L10

h = fir2 (N, f , m, window) uses the vector window of length N+1


obtained from one of the specified MATLAB window function. The
vector f contains normalized frequencies in the range from 0 to 1,
where 1 corresponds to  rad/sample. The first value of f must be 0 and
the last value 1. The vector m, contains the samples of the desired
magnitude response at the values specified in f. The desired frequency
response is then interpolated onto a dense, evenly spaced grid of length
512. Thus, this syntax corresponds to the naive design method. The
default window function used is the Hamming window.

h = fir2(N,f,m,npt) or h = fir2(N,f,m,npt,window) specifies the number


of points, npt, for the grid onto which fir2 interpolates the frequency
response. The default npt value is 512.

It should be noted that the fir2 does not implement the classic optimum
frequency sampling method.
IUB Dr. Abdur Razzak 70
MATLAB example 7.86 ECR 305_L10

Design a 50-order FIR lowpass and highpass filter with a cutoff


frequency of 0.3 rad/sec.

% Lowpass filter definition % Frequency response


f = [0,0.3,1]; [H w] = freqz(h,[1],1000,'whole');
a = [1,0,0]; H = (H(1:501))'; w = (w(1:501))';
N = 50; mag = abs(H);
db = 20*log10((mag+eps)/max(mag));
% Highpass filter definition
% f = [0,0.3,1]; % Plotting
% a = [0,0,1]; plot(w/pi,db); grid
% N = 50; title('Magnitude response in
dB','fontsize',15);
% Calculation xlabel('Frequency in pi units','fontsize',15);
% Impulse response ylabel('Decibels','fontsize',15);
h = fir2(N,f,a); axis([0 1 -150 50]);

IUB Dr. Abdur Razzak 71


MATLAB example 7.86 (contd..) ECR 305_L10

IUB Dr. Abdur Razzak 72


MATLAB example 7.86 (contd..) ECR 305_L10

IUB Dr. Abdur Razzak 73


MATLAB functions ECR 305_L10

The Parks-McClellan algorithm is available in MATLAB as a


function called firpm, the most general syntax of which is:
h = firpm(N,f,m,weights,ftype). There are several versions of this
syntax.

 [h] = firpm (N, f , m) designs an Nth-order (note that the length of the
filter is M = N + 1) FIR digital filter whose frequency response is
specified by the arrays f and m. The filter coefficients (or the impulse
response) are returned in array h of length M. The array f contains band-
edge frequencies in units of , that is, 0.0 < f < 1.0. These frequencies
must be in increasing order, starting with 0.0 and ending with 1.0. The
array m contains the desired magnitude response at frequencies specified
in f. The lengths of f and m arrays must be same and must be an even
number. The weighting function used in each band is equal to unity, which
means that the tolerances (i’s) in every band are the same.

IUB Dr. Abdur Razzak 74


MATLAB functions (contd..) ECR 305_L10

 [h] = firpm(N,f,m,weights) is similar to the above case except


that the array weights specifies the weighting function in each
band.
 [h] = firpm(N,f,m,ftype) is similar to the first case except when
ftype is the string 'differentiator' or `hilbert', it designs digital
differentiators or digital Hilbert transformers, respectively. For
the digital Hilbert transformer the lowest frequency in the f array
should not be 0, and the highest frequency should not be 1. For
the digital differentiator, the m vector does not specify the desired
slope in each band but the desired magnitude.
 [h] = firpm(N,f,m,weights,ftype) is similar to the above case
except that the array weights specifies the weighting function in
each band.
IUB Dr. Abdur Razzak 75
MATLAB functions (contd..) ECR 305_L10

 To estimate the filter order N, the SP toolbox provides the


function firpmord which also estimates other parameters that can
be used in the firpm function.
 The basic syntax is [N,f0,m0,weights] = firpmord(f,m,delta).
 The function computes the window order N, the normalized
frequency band edges in f0, amplitude response in m0, and the
band weights in weights. The vector f is a vector of normalized
band edges and m is a vector specifying the desired amplitude on
the bands defined by f. The length of f is two less than twice the
length of m; i.e., f does not contain 0 or 1. The vector delta
specifies tolerances in each band (not in decibels). The estimated
parameters can now be used in the firpm function.

IUB Dr. Abdur Razzak 76


References ECR 305_L10

1. Vinay K. Ingle, and John G. Proakis, Digital Signal


Processing using MATLAB, Thomson Learning
Bookware Companion Series, 2007. (pp. 231–312)

IUB Dr. Abdur Razzak 77


Next class… ECR 305_L10

IIR Filter
Design
IUB Dr. Abdur Razzak 78

You might also like