You are on page 1of 41

A REPORT ON

DIGITAL FILTER DESIGN USING MATLAB




A partial fulfillment of

COMPUTER ORIENTED PROJECT (BITS GC 331)

Under the guidance of Prof.S.P.Dubey

By

CH.Yeswanth
2004P3PS093



BIRLA INSTITUTE OF TECHNOLOGY & SCIENCE, PILANI
GOA CAMPUS
April,2007


TABLE OF CONTENTS

1. INTRODUCTION
a. Definition of a filter
b. Operation of a basic Digital Filter
c. Recursive & Non-recursive filters
d. Introduction to MATLAB
2. DIGITAL FILTER DESIGN
3. FINITE IMPULSE RESPONSE (FIR) FILTER DESIGN
a. Properties of Linear-phase FIR Filter
b. Windows Design Techniques
c. Frequency Sampling Design Techniques
d. Optimal Equiripple Design Technique
e. MATLAB implementation
4. INFINITE IMPULSE RESPONSE (IIR) FILTER DESIGN
a. Basic design of IIR filter
b. IIR filter design steps
c. Characteristics of Prototype Analog Filters
d. Analog-to-digital Filter Transformations
e. Frequency-band Transformations
f. MATLAB implementation
5. COMPARISON OF FIR vs. IIR FILTERS
a. MATLAB applications
6. CONCLUSION







1. INTRODUCTION

a. Definition of a filter:

The function of a filter is to remove unwanted parts of the signal, such as random noise,
or to extract useful parts of the signal, such as the components lying within a certain
frequency range.

The following block diagram illustrates the basic idea.
There are two main kinds of filter, analog and digital. They are quite different in their
physical makeup and in how they work.

An analog filter uses analog electronic circuits made up from components such as
resistors, capacitors and op-amps to produce the required filtering effect

A digital filter uses a digital processor to perform numerical calculations on sampled
values of the signal. The processor may be a general-purpose computer such as a PC, or a
specialized DSP (Digital Signal Processor) chip.

b. Operation of a basic Digital Filter:

The basic theory of the operation of digital filters is essential to an understanding of how
digital filters are designed and used.

Suppose the "raw" signal which is to be digitally filtered is in the form of a voltage
waveform described by the function
FILTER
Raw (unaltered)
signal
Filtered
signal
V= X(t)
where t is time.

This signal is sampled at time intervals h (the sampling interval). The sampled value at
time t = ih is
Xi= X(ih)
Thus the digital values transferred from the ADC to the processor can be represented by
the sequence
X0 , X1 ,X2 , X3 , ...

corresponding to the values of the signal waveform at
t = 0, h, 2h, 3h, ...
and t = 0 is the instant at which sampling begins.

At time t = nh (where n is some positive integer), the values available to the processor,
stored in memory, are
X0 , X1 , X2, X3 , ... X n
The digital output from the processor to the DAC consists of the sequence of values
Y0 , Y1 , Y2 , Y3 , ... Y n
In general, the value of Yn is calculated from the values X0, X1, X2, X3, .... Xn. The way
in which the y's are calculated from the x's determines the filtering action of the digital
filter.

c. Recursive & Non-recursive filters:

From above, it is clear that the values of output variables
A recursive filter is one which in addition to input values also uses previous output
values. These, like the previous input values, are stored in the processor's memory.

The word recursive literally means "running back", and refers to the fact that previously-
calculated output values go back into the calculation of the latest output. The expression
for a recursive filter therefore contains not only terms involving the input values
(xn, xn-1, xn-2, ...) but also terms in yn-1, yn-2, ...

Some people prefer an alternative terminology in which a non-recursive filter is known as
an FIR (or Finite Impulse Response) filter, and a recursive filter as an IIR (or Infinite
Impulse Response) filter.

d. Introduction to MATLAB:

MATLAB is an interactive, matrix-based system for scientific and engineering numeric
computation and visualization. Its strength lies in the fact that complex numerical
problems can be solved easily and in a fraction of the time. It is also powerful in the
sense that by using its relatively simple programming capability, MATLAB can be easily
extended to create new commands and functions.

The DSP applications are primarly algorithms that are implemented either on a DSP
processor or in software, a fair amount of programming is required. Using interactive
software, such as MATLAB, it is now possible to place more emphasis on learning new
and difficult concepts than on programming algorithms.












2. DIGITAL FILTER DESIGN

The design of a digital filter is carried out in three steps:
i. Specifications: they are determined by the applications
ii. Approximations: once the specification 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.
iii. 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.

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.

Absolute specifications: Provide a set of requirements on the magnitude response
function |H(ejw)|. Generally used for FIR filters.

Relative specifications: Provide requirements in decibels (dB), given by



This is used for both FIR and IIR filters.




0
| ) ( |
| ) ( |
log 20
max
10
=
jw
jw
e H
e H
scale dB
3. FINITE IMPULSE RESPONSE (FIR) FILTER DESIGN

a. Properties of Linear-phase FIR Filter:

Let h(n), n=0,1,,M-1 be the impulse response of length (or duration) M. Then the
system function is


Which has (M-1) poles at the origin (trivial poles) and M-1 zeros located anywhere in
the z-plane. The frequency response function is


For impulse response, h(n), if we impose a linear-phase constraint


Where alpha is a constant phase delay, then the h(n) must be symmetric about
alpha,which is index of symmetry, that is


There are two possibilities of symmetry, that is when M is odd and M is even.
If the phase response satisfy the condition


Which is a straight line but not through the origin. In this case alpha is not a constant
phase delay, but


Is constant, which is the group delay. Therefore alpha is a constant group delay. In this
case, as a group, frequencies are delayed at a constant rate. For this type of linear phase
one can show that



=

= =
1
0
1 ) 1 (
1
0
) ( ) ( ) (
M
n
n M M
M
n
n
z n h z z n h z H
< =

w e n h e H
M
n
jwn jw
, ) ( ) (
1
0
< = w w e H
jw
, ) (
2
1
, 1 0 ), 1 ( ) (

= =
M
with M n n M h n h
< = w w e H
jw
, ) (
=

dw
e H d
jw
) (
2
,
2
1
, 1 0 ), 1 ( ) (

=

= =
M
with M n n M h n h
This means that the impulse response h(n) is antisymmetric. The index of symmetry is
still alpha=(M-1)/2. Once again we have two possible types, one for M odd and one for
M even.

b. Windows Design Techniques:

There are several design methods, to design a FIR filter in MATLAB. One of the most
useful technique is Window design technique.

The basic idea is to choose a proper ideal frequency-selective filter (which always has a
noncausal, infinite-duration impulse response) and then truncate (or window) its impulse
response to obtain a linear-phase and causal FIR filter.

An ideal LPF of bandwidth wc<pi is given by


From this the ideal impulse response is


To obtain a causal and linear-phase FIR filter h(n) of length M, we must have


This operation is called Windowing.



Depending on how we define w(n) above, we obtain different window design.



<

=

| | 0
| | , 1
) (
w w
w w e
e H
c
c
w j
jw
d
| |
) (
)] ( sin[
) (
2
1
) ( ) (
1



= = =

+

n
n wc
dw e e H e H F n h
jwn jw
d
jw
d d
2
1
,
0
1 0 ) (
) (

=


=
M
and
elsewhere
M n n h
n h
d


=
=
otherwise
M n over to respect with function symmetric some
n w
n w n h n h
d
, 0
1 0
) (
) ( ) ( ) (

There are different window design methods:


Rectangular window:
This is the simplest window function but provides the worst performance from the
viewpoint of stopband attenuation.






Bartlett window:
Bartlett suggested a more gradual transition in the form of a triangular window





Hanning window:
This is a raised cosine window function.



Hamming window:
This is similar to Hanning window except that it has a small amount of
discontinuity and is given by



Blackman window:
This is also similar to the previous two but contains a second harmonic term and
is given by
( )
( )
( )
( )
1 , ) (
2
1
) (
2
1
) (
sin
sin
) (
sin
sin
) (
, 0
1 0 , 1
) (
2
2
2
2 2
1
>> =
=
(


=

+


M d W d W w H
w W e e W
otherwise
M n
n w
c c
M
w w
r
w w
r r
w
wM
r
jw
w
wM
jw

=
otherwise
M n
M
M
n
M
n
M
n
n w
, 0
1
2
1
,
1
2
2
2
1
0 ,
1
2
) (

\
|

=
otherwise
M n
M
n
n w
, 0
1 0 ,
1
2
cos 1 5 . 0
) (

\
|

=
otherwise
M n
M
n
n w
, 0
1 0 ,
1
2
cos 46 . 0 54 . 0
) (




Kaiser window:
This is an adjustable window function that is widely used in practice. The window
function is given by




Where I0[] is the modified zero-order Bessel function. The parameter beta
controls tha minimum stopband attenuation As and can be chosen to yield
different transition widths for near-optimum As.
Kaiser has developed empirical design equations, given below








Window name Transistion

Approximation
Width

Exact values
Minimum
stopband
attenuation
Rectangular 4/M 1.8/M 21dB
Bartlett 8/M 6.1/M 25dB
Hann 8/M 6.2/M 44dB
Hamming 8/M 6.6/M 51dB
Blackman 12/M 11/M 74dB

\
|

+
|

\
|

=
otherwise
M n
M
n
M
n
n w
, 0
1 0 ,
1
4
cos 08 . 0
1
2
cos 5 . 0 42 . 0
) (

1 0 ,
] [
1
2
1 1
) (
0
2
0

(
(

\
|


= M n
I
M
n
I
n w

< < +

=
+

= =
50 21 ) 21 ( 07886 . 0 ) 21 ( 5842 . 0
50 ), 7 . 8 ( 1102 . 0
1
36 . 14
95 . 7
2
.
, , ,
4 . 0
s s s
s s
s
p s
s p s p
A A A
A A
parameter
f
A
M order filter
w w
f width transition Norm
A and R w w Given

c. Frequency Sampling Design Techniques:



In this design approach we use the fact that the system function H(z) can be obtained from
the samples H(k) of the frequency response H(ejw).

The basic idea is, for a given ideal lowpass filter Hd(ejw), choose the filter length M and
then sample Hd(ejw) at M equispaced frequencies between 0 and 2pi. The actual response
is the interpolation of the samples is given by






The approximation error that is difference between the ideal and the actual responseis
zero at the sampled frequencies. 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. The error is larger near the band edge and smaller within
the band.

Two Design Approaches:
Nave design method:
Use the basic idea literally and provide no constraints on the approximation error,
that is, we accept whatever error we get from the design.

Optimum design method:
To try minimize error in the stop band by varying values of the transition band
samples.





= =
1
0
1
0
/ 2 1
1
) ( 1
) ( ) (
M
n
M
k
M k j
M
n
e z
k H
M
z
z n h z H

)] ( [ ) ( k H IDFT n h =
d. Optimal Equiripple Design Technique:

The disadvantages of the window design and the frequency sampling designis that, we
cannot specify the band frequencies wp and ws precisely in the design. We cannot specify
both delta1 and delta2 ripple factors simultaneously. The approximation errorthat is,
the difference between the ideal response and the actual responseis not uniformly
distributed over the band intervals.

The techniques to eliminate the above three problems are:
For linear-phase FIR filter 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 filter because the approximation
error is uniformly distributed in both the passband and the stopband.
This results in low-order filter.

Parks-McClellan Algorithm:

It was solved by Remez.
1. Estimate the filter length order M by (7.48)
2. Guess extrema frequencies wi (i= 1:L+2)
3. Find an Lth polynomial that fits these points
4. Determine new wis by interpolation of the polynomial
5. Iteration from beginning
6. Determing a(i) and Emax by min(max(E(w)))





2
,
6 . 14
log 20

10 * ) 1 (
1
log 20
1 10
1 10
1
1
log 20
2 1 10
20 /
1 2
1
2
10
20 /
20 /
1
1
1
10
p s
A
s
R
R
p
w w
f
f
M
A
R
s
p
p

=
+ =
+
=
+

=
+

e. MATLAB implementation:

MATLAB provides several functions to implement window functions, as given below

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) returns the beta-valued M-point Kaiser window function in array
W.

Using the above window functions we write a MATLAB program to design a FIR digital
filter from given specifications.

Example: Design a digital lowpass filter with Wp = 0.2*pi, Ws = 0.3*pi, As = 50dB.
Use all window design methods such that user can select any window at a time for the
above specifications.
MATLAB code:
%This program uses the window method to design low pass FIR
%filter.
%The following windows are supported: Rectangular, Hamming,
%Hanning, Bartlett, Blackman and Kaiser.


% wtype: 1 for Rectangular window
% 2 for Barlett window
% 3 for Hanning window
% 4 for Hamming window
% 5 for Blackman window
% 6 for Kaiser window

clear all;
wtype = 5;
wp = 0.2*pi; ws = 0.3*pi; As = 50; tr_width = ws-wp;
if wtype==1
M = ceil(1.8*pi/tr_width)+1
n = [0:1:M-1];
wc = (ws+wp)/2;
alpha = (M-1)/2; n = [0:1:(M-1)];
m = n-alpha; fc = ws/pi; hd = fc*sinc(fc*m);
w_rect = (boxcar(M))';
h = hd .* w_rect;
[H,w] = freqz(h,[1],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(h,[1],w);
delta_w = 2*pi/1000;
% plots
subplot(2,2,2); stem(n,w_rect); title('Rectangular window')
axis([0 M-1 0 1.1]); xlabel('n'); ylabel('w(n)')

elseif wtype==2
M = ceil(6.1*pi/tr_width)+1
n = [0:1:M-1];
wc = (ws+wp)/2;
alpha = (M-1)/2; n = [0:1:(M-1)];
m = n-alpha; fc = ws/pi; hd = fc*sinc(fc*m);
w_bart = (bartlett(M))';
h = hd .* w_bart;
[H,w] = freqz(h,[1],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(h,[1],w);
delta_w = 2*pi/1000;
% plots
subplot(2,2,2); stem(n,w_bart); title('Bartlett window')
axis([0 M-1 0 1.1]); xlabel('n'); ylabel('w(n)')

elseif wtype==3
M = ceil(6.2*pi/tr_width)+1
n = [0:1:M-1];
wc = (ws+wp)/2;
alpha = (M-1)/2; n = [0:1:(M-1)];
m = n-alpha; fc = ws/pi; hd = fc*sinc(fc*m);
w_hann = (hanning(M))';
h = hd .* w_hann;
[H,w] = freqz(h,[1],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(h,[1],w);
delta_w = 2*pi/1000;
% plots
subplot(2,2,2); stem(n,w_hann); title('Hanning window')
axis([0 M-1 0 1.1]); xlabel('n'); ylabel('w(n)')

elseif wtype==4
M = ceil(6.6*pi/tr_width)+1
n = [0:1:M-1];
wc = (ws+wp)/2;
alpha = (M-1)/2; n = [0:1:(M-1)];
m = n-alpha; fc = ws/pi; hd = fc*sinc(fc*m);
w_ham = (hamming(M))';
h = hd .* w_ham;
[H,w] = freqz(h,[1],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(h,[1],w);
delta_w = 2*pi/1000;

subplot(2,2,2); stem(n,w_ham); title('Hamming window')
axis([0 M-1 0 1.1]); xlabel('n'); ylabel('w(n)')


elseif wtype==5
M = ceil(11*pi/tr_width)+1
n = [0:1:M-1];
wc = (ws+wp)/2;
alpha = (M-1)/2; n = [0:1:(M-1)];
m = n-alpha; fc = ws/pi; hd = fc*sinc(fc*m);
w_black = (blackman(M))';
h = hd .* w_black;
[H,w] = freqz(h,[1],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(h,[1],w);
delta_w = 2*pi/1000;
% plots
subplot(2,2,2); stem(n,w_black); title('Blackman window')
axis([0 M-1 0 1.1]); xlabel('n'); ylabel('w(n)')

elseif wtype==6
M = ceil((As-7.95)/(2.285*tr_width)+1)+1
n = [0:1:M-1]; beta = 0.1102*(As-8.7)
wc = (ws+wp)/2;
alpha = (M-1)/2; n = [0:1:(M-1)];
m = n-alpha; fc = ws/pi; hd = fc*sinc(fc*m);
w_kai = (kaiser(M,beta))';
h = hd .* w_kai;
[H,w] = freqz(h,[1],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(h,[1],w);
delta_w = 2*pi/1000;
% plots
subplot(2,2,2); stem(n,w_kai); title('Kaiser window')
axis([0 M-1 0 1.1]); xlabel('n'); ylabel('w(n)')

end
subplot(2,2,1); stem(n,hd); title('Ideal Impulse Response')
axis([0 M-1 -0.1 0.3]); xlabel('n'); ylabel('hd(n)')
subplot(2,2,3); stem(n,h); title('Actual impulse response')
axis([0 M-1 -0.1 0.3]); xlabel('n'); ylabel('h(n)')
subplot(2,2,4); plot(w/pi,db); title('magnitude response in db');grid
axis([0 1 -100 10]); xlabel('frequency in pi units');
ylabel('decibels')

Output:
For wtype=1, that is Rectangular window
M =19




For wtype=2, that is Bartlett window
M = 62


For wtype=3, that is Hanning window
M = 63

For wtype=4, that is Hamming window
M = 67


For wtype=5, that is Blackman window
M = 111


For wtype=6, that is Kaiser window
M = 61
Beta = 4.5513














4. INFINITE IMPULSE RESPONSE (IIR) FILTER DESIGN

a. Basic design of IIR filter:

IIR filter have infinite-duration impulse responses, hence they can be matched to analog
filters, all of which generally have infinitely long impulse responses.

The basic technique of IIR filter design transforms well-known analog filters into digital
filters using complex-valued mappings. The advantage of this technique lies in the fact
that both analog filter design (AFD) tables and the mappings are available extensively in
the literature.

The basic technique is called the A/D filter transformation. However, the AFD tables are
available only for lowpass filters. We also want design other frequency-selective filters
(highpass, bandpass, bandstop, etc.). To do this, we need to apply frequency-band
transformations to lowpass filters. These transformations are also complex-valued
mappings.

There are two approaches to this basic technique of IIR filter design:
Approach 1:




This approach used in MATLAB.
Approach 2:



This is a study-oriented approach.

Design analog
lowpass filter
Apply Freq. band
transformation
s-->s
Apply filter
transformation
s-->z
Desired
IIR filter
Design analog
lowpass filter
Apply filter
transformation
s-->z
Apply Freq. band
transformation
z-->z
Desired
IIR filter
b. IIR Filter Design Steps:

Design analog lowpass filter
Study and apply filter transformations to obtain digital lowpass filter
Study and apply frequency-band transformations to obtain other digital filters
from digital lowpass filter.

c. Characteristics of Prototype Analog Filters:

IIR filter design techniques rely on existing analog filter filters to obtain digital filters.
We designate these analog filters as prototype filters.
Three prototypes are widely used in practice:
Butterworth lowpass
Chebyshev lowpass (Type I and II)
Elliptic lowpass

I. Butterworth:
This filter is characterized by the property that its magnitude response is flat in both
passband and stopband.



The magnitude-squared response of an N-order lowpass filter, where Omega_c is the
cutoff frequency in rad/sec.

II. Chebyshev-I:
Chebyshev-I filters have equiripple response in the passband.
Chebyshev-II filters have equiripple response in stopband.
Butterworth filters have monotonic response in both bands.
We note that by choosing a filter that has an equripple rather than a monotonic
behavior, we can obtain a low-order filter.
N
c
a
j H
2
2
1
1
| ) ( |
|
|

\
|

+
=
Therefore Chebyshev filters provide lower order than Buttworth filters for the
same specifications.





Where N is the order of the filter, Epsilon is the passband ripple factor and Tn(x) is
the Nth-order Chebyshev polynomial.

III. Chebyshev-II:
It has a monotone passband and an equiripple stopband, which implies that this filter
has both poles and zeros in the s-plane.

Therefore the group delay characteristics are better (and the phase response more
linear) in the passband than the Chebyshev-I prototype.



IV. Elliptical:
These filters exhibit equiripple behavior in the passband as well as the stopband. They
are similar in magnitude response characteristics to the FIR equiripple filters.

Therefore elliptic filters are optimum filters in that they achieve the minimum order N
for the given specifications.

These filters, for obvious reasons, are very difficult to analyze and therefore, to
design. It is not possible to design them using simple tools, and often programs or
tables are needed to design them.

< <

=
|
|

\
|

+
=

x x
x x N
x T
T
j H
N
c
N
a
1 )), ( cosh(cosh
1 0 )), ( cos cos(
) (
1
1
| ) ( |
1
1
2 2
2

( ) ( )
1
2 2
2
/ 1
1
| ) ( |

+
=
c N
a
T
j H

|
|

\
|

+
=
c
N
a
U
j H
2 2
2
1
1
| ) ( |


Where N is the order, epsilon is passband ripple, Un() is the Nth order Jacobian elliptic
function. For computation of filter order N, we have




d. Analog-to-digital Filter Transformations:

After discussing different approaches to the design of analog filters, to transform them
into digital filters. These transformations are derived by preserving different aspects of
analog and digital filters.
Impulse invariance transformation: It preserves the shape of the impulse response
from Analog to Digital filter. Convert a differential equation representation into a
corresponding difference equation and also preserves the shape of the step
response. The digital filter impulse response to look similar to that of a
frequency-selective analog filter.


Frequency-domain aliasing formula.
Bilinear Transformation: This mapping is the best transformation method.


It preserves the system function representation from Analog to Digital domain. It
is a stable design and there is no aliasing. There is no restriction on the type of
filter that can be transformed.

e. Frequency-band Transformations:

Using the results of Low-pass filter and Frequency Band Transformation, we can design
other kinds of filters, such as High-pass, Band-pass, Bnad-stop filters.
( )
( )


=

=
2 /
0 2 2 2
1
2
1
2
1
sin 1
) ( ,
1
, ,
1 ) (
1 ) (


x
d
x K
A
k k
k K k K
k K k K
N
s
p

=
|

\
|
=
k
a
k
T
j s H
T
z H
2 1
) (
2 / 1
2 / 1
1
1 2
1
1
sT
sT
z
z
z
T
s

+
=
+


Let HLP(Z) be the given prototype lowpass digital filter, and let H(z) be the desired
frequency-selective digital filter. Define a mapping of the form


Given that HLP(Z) is a stable and causal filter, we also want H(z) to be stable and causal.
This imposes the following requirements:
G( ) must be a rational function in 1/z so that H(z) is implementable.
The unit circle of the Z-plane must map onto the unit circle of the z-plane.
For stable filters, the inside of the unit circle of the Z-plane must also map onto
the inside of the unit circle of the z-plane.
The general form of the function G() that satisfies the above requirements is a rational
function of the all-pass type given by

Now by choosing an appropriate order n and the coefficients {alpah_k}, we can obtain a
variety of mappings.

f. MATLAB implementation:

Now, we design an IIR digital filter in MATLAB software using above steps and
methods.

Example: Design an IIR digital filter with Butterworth, Chebyshev, Inverse Chebyshev
or Elliptic characteristic using bilinear transformation
MATLAB code:
%output arguments: %b,a - coefficients of digital filter
%ftype - 'low' for lowpass filter
% 'high' for highpass filter
% 'bandpass' for bandpass filter
% 'stop' for bandstop filter
%ctype - 1 for Butterworth filter
% 2 for Chebyshev filter
1 | | ,
1
) (
1
1
1
1 1
<


= =

=

k
k
k n
k
z
z
z G Z


) (
1 1
1 1
| ) ( ) ( that such ) (

=

= =
z G Z
LP
Z H z H z G Z
% 3 for Inverse Chebyshev filter
% 4 for Elliptic filter

%steps of designing a digital filter implied in the called
functions:
% step 1: estimate the minimum order of the filter from
specifications
% step 2: get analog, pre-warped frequencies
% step 3: convert to low-pass prototype estimate
% step 4: Get n-th order analog lowpass prototype with
desired filter %characters
% step 5: Transform to lowpass, bandpass, highpass, or
bandstop of desired Wn
% step 6: Use Bilinear transformation to find discrete
equivalent:

clear all; format;

Fs = 100000; %sampling frequency(Hz).
Wp = [20500 23500]/(Fs/2); %passband edge frequency
normalised by Fs/2.
Ws = [19000 25000]/(Fs/2); %stopband edge frewquency
normalised by Fs/2.
Rp = 0.25; %passband attenuation in dB.
Rs = 45; %stopband attenuation in dB
ctype = 4; %character of filter
ftype = 'bandpass'; %type of filter

if ctype==1
[n,Wn]=buttord(Wp,Ws,Rp,Rs);
[b,a]=butter(n,Wn,ftype);
elseif ctype==2
[n,Wn]=cheb1ord(Wp,Ws,Rp,Rs);
[b,a]=cheby1(n,Rp,Wn,ftype);
elseif ctype==3
[n,Wn]=cheb2ord(Wp,Ws,Rp,Rs);
[b,a]=cheby2(n,Rs,Wn,ftype);
elseif ctype==4
[n,Wn]=ellipord(Wp,Ws,Rp,Rs);
[b,a]=ellip(n,Rp,Rs,Wn,ftype);
end

%Output the result
disp('Numerator coefficients (in descending powers of
z):'); disp(b);
disp('Denominator coefficients (in descending powers of
z):'); disp(a);
freqz(b,a,1024,Fs);

%Impulse invariance method of anolog-to-digital filter
conversion
%a,b -- s-plane coefficients
%az,bz -- digital filter coefficients

clear all;
b = 1;
a = [1.84496 1.920675 1];
[bz,az]=impinvar(b,a) %get z-plane coefficients using
impulse Inv.
freqz(bz,az,1024);



Output:
The is given for elliptical bandpass filter.
Numerator coefficients (in descending powers of z):
Columns 1 through 5

0.0061 -0.0083 0.0236 -0.0221 0.0351

Columns 6 through 9

-0.0221 0.0236 -0.0083 0.0061

Denominator coefficients (in descending powers of z):
Columns 1 through 5

1.0000 -1.4483 4.4832 -4.2207 6.6475

Columns 6 through 9

-3.9458 3.9187 -1.1828 0.7634


bz =

0 0.5677







Example: Designing an elliptical lowpass filter using both bilinear and impulse invariant
transformations.
MATLAB code:
% Comparison of magnitude and phase responses of an
analogue % and equivalent BZT and impulse invariant
discrete-time filters
% and pole-zero diagrams (elliptic low pass filter)
%
Fs=10000; FN=Fs/2;
fp=1000; fs=3000;
wp=fp*2*pi; ws=fs*2*pi;
Ap=1; As=60;
%
% Calculate filter coefficients and frequency responses
[N, wc]=ellipord(wp, ws, Ap, As,'s'); % analog filter
[B, A]=ellip(N, Ap, As, wc, 's');
[bBZT, aBZT]=bilinear(B,A,Fs); % BZT filter
[bIIT,aIIT]=impinvar(B,A,Fs); % Impulse
invariance filter
%
% Compute frequency response
[Ha, wa]=freqs(B,A);
[HBZT, fBZT]=freqz(bBZT, aBZT, 512, Fs);
[HIIT, fIIT]=freqz(bIIT, aIIT, 512, Fs);
%
% Plot magnitude frequency responses
%
figure(1); % Plot analogue magnitude
response
plot(wa/(2*pi), 20*log10(abs(Ha)))
hold on
figure (1);
plot(fBZT, 20*log10(abs(HBZT)), 'r:') % Plot BZT
magnitude response
hold on
figure (1);
plot(fIIT, 20*log10(abs(HIIT)), 'g:') % Plot Impinv
magnitude response
legend('Analog', 'BZT', 'Imp Invar');
axis([0 10000 -120 0])
ylabel('Magnitude (dB)')
xlabel('Frequency (Hz)')
title('Filter magnitude responses')
hold off;
%
% Plot phase responses
%
figure(2);
plot(wa/(2*pi), angle(Ha)*180/pi) % Plot analogue
phase response
hold on
figure (2);
plot(fBZT, angle(HBZT)*180/pi, 'r:') % Plot BZT phase
response
hold on
figure(2);
plot(fIIT, angle(HIIT)*180/pi, 'g.') %ImpInvar phase
response
legend('Analog', 'BZT','Imp Invar');
axis([0 10000 -360 360])
ylabel('Phase (Degrees)')
xlabel('Frequency (Hz)')
title('Filter Phase Responses')
hold off
%
% Plot pole-zero diagrams
%
figure (3);
zplane(bBZT, aBZT)
title('Pole-zero diagram - BZT filter')
figure (4);
zplane(bIIT, aIIT)
xmin=-1; xmax=1; ymin=-1; ymax=1; % Scale the z-plane for
the Impulse Inva.
axis([xmin xmax ymin ymax])
title('Pole-zero diagram - impulse invariance filter')










Output:










5. COMPARISION OF FIR vs. IIR FILTERS

So far we have seen many techniques for designing both FIR and IIR filters. In practice
one wonders about which filter should be chosen for a given application and which
method should be used to design it. Some meaning comparisons can be attempted if we
focus on the minimax optimal (or equiripple) filters. In the case of FIR filters these
optimal filters are the equiripple filters designed via the Parks-McClellan algorithm,
while in the case of IIR filters these are the elliptical filters.

One basis of comparison is the number of multiplications required to compute one output
sample in the sample realization of these filters. For FIR filters the standard realization is
the linear-phase direct form, while for elliptic filters cascade forms are widely used. Let
M be the length of a linear phase FIR filter (assume M be odd). Then we need

1
2 2
M M +
; (for large M)

multiplications per output sample. Let N (assume be even) be the order of an elliptic filter
with the cascade form realization. Then there are N/2 second-order sections, each
requiring 3 multiplications. There are an additional three multiplications in the overall
structure for a total of

3
3 3
2 2
N N
+ ; (for large N)
multiplications per output sample.

Now if we assume that each filter meets exactly the same specifications i.e. p, s,
1 (or passband ripple), and 2 (or stopband attenuation) for a lowpass filter, then these
two filters are equivalent if
1 3 3 1
3 3
2 2
M N M
N N
+ +
= = + ; (for large N)
Then means that if the ratio M/N = 3, then two filters are roughly efficient. However, an
equiripple FIR filter is more efficient if M/N < 3, or an elliptic IIR filter is more efficient
if M/N >3.

a. MATLAB applications:

Consider an example of filter which passes all frequencies upto 100Hz and attenuates
above 175 Hz, with a passband ripple of 0.34dB and stopband attenuation of 40dB.
Design two FIR filters (both Parks-McClellan algorithm and Hamming window method)
(i) Use the Parks-McClellan algorithm to design the minimum-length filter that meets the
specifications.
(ii) Using same length (M) obtained in part (i), design an FIR lowpass filter using the
window method with a Hamming window. Does this filter meet the specifications? If not,
find the smallest length Hamming window design that has the desired characteristics.
Design two IIR filters both Butterworth and Elliptic filters.
Compare the magnitude responses of each filter.

Answer:
The MATLAB codes are given below along with magnitude responses of each filter.
Clearly in IIR filter, Elliptic gives more accurate response for desired specifications than
Butterworth.
In FIR filter, Hamming window having M=26, does not has the required response, but it
satisfies for order M=45. Both Hamming and Parks-McClellan algorithm satisfies the
specifications, but equiripple filter design by Parks-McClellan algorithm is preferred as it
has order of M = 26. This is because of Optimal Equiripple Design.

MATLAB code:
Design 1: IIR Elliptic filter
Fs=1000; FN=Fs/2;
fp=100; fs=175;
wp=fp*2*pi; ws=fs*2*pi;
Ap=0.34; As=40;
[N, wc]=ellipord(wp, ws, Ap, As,'s'); % analog filter
[B, A]=ellip(N, Ap, As, wc, 's');
[bBZT, aBZT]=bilinear(B,A,Fs); % BZT filter
[Ha, wa]=freqs(B,A);
[HBZT, fBZT]=freqz(bBZT, aBZT, 512, Fs);
% Plot magnitude frequency responses
figure(1); % Plot analogue magnitude
response
plot(wa/(2*pi), 20*log10(abs(Ha)))
hold on
figure (1);
plot(fBZT, 20*log10(abs(HBZT)), 'r:') % Plot BZT
magnitude response
legend('Analog', 'BZT', 'Imp Invar');
axis([0 1000 -120 0])
ylabel('Magnitude (dB)')
xlabel('Frequency (Hz)')
title('Filter magnitude responses')
hold off
figure(2);
plot(wa/(2*pi), angle(Ha)*180/pi) % Plot analogue
phase response
hold on
figure (2);
plot(fBZT, angle(HBZT)*180/pi, 'r:') % Plot BZT phase
response
legend('Analog', 'BZT','Imp Invar');
axis([0 1000 -360 360])
ylabel('Phase (Degrees)')
xlabel('Frequency (Hz)')
title('Filter Phase Responses')

Output:






Design 2: IIR Butterworth filter
Fs=1000; FN=Fs/2;
fp=100; fs=175;
wp=fp*2*pi; ws=fs*2*pi;
Ap=0.34; As=40;
[N, wc]=buttord(wp, ws, Ap, As,'s'); % analog filter
[B, A]=butter(N, Ap, As, wc, 's');
[bBZT, aBZT]=bilinear(B,A,Fs); % BZT filter
[Ha, wa]=freqs(B,A);
[HBZT, fBZT]=freqz(bBZT, aBZT, 512, Fs);
% Plot magnitude frequency responses
%
figure(1); % Plot analogue magnitude
response
plot(wa/(2*pi), 20*log10(abs(Ha)))
hold on
figure (1);
plot(fBZT, 20*log10(abs(HBZT)), 'r:') % Plot BZT
magnitude response
legend('Analog', 'BZT', 'Imp Invar');
axis([0 1000 -120 0])
ylabel('Magnitude (dB)')
xlabel('Frequency (Hz)')
title('Filter magnitude responses')
hold off
figure(2);
plot(wa/(2*pi), angle(Ha)*180/pi) % Plot analogue
phase response
hold on
figure (2);
plot(fBZT, angle(HBZT)*180/pi, 'r:') % Plot BZT phase
response
legend('Analog', 'BZT','Imp Invar');
axis([0 1000 -360 360])
ylabel('Phase (Degrees)')
xlabel('Frequency (Hz)')
title('Filter Phase Responses')

Output:




Design 3: FIR Equiripple filter using Parks-McClellan algorithm
wp = 0.2*pi; ws = 0.35*pi;
delta1 = 0.02; delta2 = 0.0102;
[N,f,m,weights] =
firpmord([wp,ws]/pi,[1,0],[delta1,delta2]);
h = firpm(N,f,m,weights);
[H,w] = freqz(h,[1],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(h,[1],w);
delta_w = 2*pi/1000;wsi = ws/delta_w+1; wpi = wp/delta_w;
Asd = -max(db(wsi:1:501))
N = N+1
h = firpm(N,f,m,weights);
[H,w] = freqz(h,[1],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(h,[1],w);
Asd = -max(db(wsi:1:501))
N = N+1
h = firpm(N,f,m,weights);
[H,w] = freqz(h,[1],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(h,[1],w);
Asd = -max(db(wsi:1:501))
M = N+1
n = [0:1:M-1];
subplot(2,1,1); stem(n,h); title('Actual impulse response')
axis([0 M-1 -0.1 0.3]); xlabel('n'); ylabel('h(n)')
subplot(2,1,2); plot(w/pi,db); title('magnitude response in
db'); grid
axis([0 1 -100 10]); xlabel('frequency in pi units');
ylabel('decibels')








Output:



Design 4: FIR filter using Hamming window of order M=26
wp = 0.2*pi; ws = 0.35*pi; tr_width = ws-wp;
M = 26;
n = [0:1:M-1];
wc = (ws+wp)/2;
alpha = (M-1)/2; n = [0:1:(M-1)];
m = n-alpha; fc = ws/pi; hd = fc*sinc(fc*m);
w_ham = (hamming(M))';
h = hd .* w_ham;
[H,w] = freqz(h,[1],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(h,[1],w);
delta_w = 2*pi/1000;
subplot(2,1,1); stem(n,h); title('Actual impulse response')
axis([0 M-1 -0.1 0.3]); xlabel('n'); ylabel('h(n)')
subplot(2,1,2); plot(w/pi,db); title('magnitude response in
db'); grid
axis([0 1 -100 10]); xlabel('frequency in pi units');
ylabel('decibels')
Output:



Design 5: FIR filter using Hamming window of order M=45
wp = 0.2*pi; ws = 0.35*pi; tr_width = ws-wp;
M = ceil(6.6*pi/tr_width)+1
n = [0:1:M-1];
wc = (ws+wp)/2;
alpha = (M-1)/2; n = [0:1:(M-1)];
m = n-alpha; fc = ws/pi; hd = fc*sinc(fc*m);
w_ham = (hamming(M))';
h = hd .* w_ham;
[H,w] = freqz(h,[1],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(h,[1],w);
delta_w = 2*pi/1000;
subplot(2,1,1); stem(n,h); title('Actual impulse response')
axis([0 M-1 -0.1 0.3]); xlabel('n'); ylabel('h(n)')
subplot(2,1,2); plot(w/pi,db); title('magnitude response in
db'); grid
axis([0 1 -100 10]); xlabel('frequency in pi units');
ylabel('decibels')

Output:


Clearly from the above, FIR equiripple filter has order M=26 and IIR elliptic filter has
order N=6. As M/N > 3, IIR elliptic filter is desirable for the given specifications.












6. CONCLUSION

All Window design methods are used but, according to the need of use, required
order of filter, we have choose different windows which are appropriate to our
need.
To obtain Gibbs phenomenon Bartlett window is used. Kaiser window is widely
used, as the beta parameter, gives the transition width, from the order of filter.
Other windows are also used in various applications.
The design of IIR filters becomes easy when the required analog design is done.
The advantage of this technique lies in the fact that both analog filter design
(AFD) tables and the mappings
For FIR filter design, linear phase constraint is necessarily required, to make the
design easy. But for IIR filter design this cannot be done.
For most applications IIR elliptic filter are desirable from the computational point
of view. If we take into account the phase equalizers, then FIR filter designs look
good because of their exact linear-phase characteristics.
The most favorable conditions for FIR filters are large values of 1 , small values
of 2 and large transition width.
MATLAB is a widely used software for Filter designing. This design method can
be implemented in the software itself as a part of certain real systems.

You might also like