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

Lab Sheet 7 Digital Filter Design Using MATLAB Toolbox: D Designfilt (Resp, Name, Value)

The document describes using MATLAB's digital filter design toolbox to design various types of digital filters (FIR and IIR). It provides examples of designing lowpass, highpass, and bandpass filters by specifying parameters like filter order, cutoff frequencies, passband/stopband ripple, and sample rate. The filter objects created using the designfilt function can then be analyzed using fvtool to view the frequency and impulse responses.

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)
59 views

Lab Sheet 7 Digital Filter Design Using MATLAB Toolbox: D Designfilt (Resp, Name, Value)

The document describes using MATLAB's digital filter design toolbox to design various types of digital filters (FIR and IIR). It provides examples of designing lowpass, highpass, and bandpass filters by specifying parameters like filter order, cutoff frequencies, passband/stopband ripple, and sample rate. The filter objects created using the designfilt function can then be analyzed using fvtool to view the frequency and impulse responses.

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/ 17

Lab Sheet 7

Digital Filter Design Using MATLAB


Toolbox

1. A fundamental knowledge on MATLAB


2. A theoretical knowledge on digital filter.

1. design digital filter using MATLAB toolbox.


2. understand different characteristics of Digital Filter.

1. Introduction
2. Lab Session 7.1: FIR and IIR filter design Using fvtool toolbox
3. Lab Session 7.2: Filter Design Using fdatool
4. Lab Session 7.3: In Lab Evaluation
5. Home work

Introduction:
Methods of the design of FIR and IIR filters.
1. Using fvtool toolbox
2. Using fdatool
Lab Session 1.1: FIR and IIR filter design Using fvtool toolbox

Used function : designfilt

Syntax
d = designfilt(resp,Name,Value)
fvtool (d)

About the input arguments:


d = designfilt(resp, Name, Value)

1|Page
Filter response and type: Value Pair Arguments
any one of the following Specify optional comma-separated pairs of Name, Value
'lowpassfir' arguments. Name is the argument name and Value is the
'lowpassiir' corresponding value. Name must appear inside single
'highpassfir' quotes (' '). You can specify several name and value pair
'highpassiir' arguments in any order as
'bandpassfir' Name1,Value1,...,NameN, ValueN.

Some Terminology:
Name Meaning
'FilterOrder' Some design methods let you specify the order. Others produce
minimum-order designs. That is, they generate the smallest filters that
satisfy the specified constraints.
'DesignMethod' It is the algorithm used to design the filter. Examples include
constrained least squares ('cls') and windowing ('window'). For some
specification sets, there are multiple design methods available to
choose from. In other cases, you can use only one method to meet the
desired specifications.
'SampleRate' It is the frequency at which the filter operates. designfilt has a default
sample rate of 2 Hz. Using this value is equivalent to working with
normalized frequencies.

Design options They are parameters specific to a given design method. Examples
include 'Window' for the windowing method and optimization
'Weights' for arbitrary-magnitude equiripple designs. (See the complete
list under Name-Value Pair Arguments.) designfilt provides default
values for design options left unspecified.
Example 7.1: Design a lowpass FIR with the following specifications:
FrequencyResponse: 'lowpass'
ImpulseResponse: 'fir'
SampleRate: 2000
FilterOrder: 25
PassbandFrequency: 400
StopbandFrequency: 550
DesignMethod: 'ls'

Solution: MATLAB Code:


d = designfilt('lowpassfir', ... % Response type
'FilterOrder',25, ... % Filter order
'PassbandFrequency',400, ... % Frequency constraints
2|Page 'StopbandFrequency',550, ...
'DesignMethod','ls', ... % Design method
'PassbandWeight',1, ... % Design method options
'StopbandWeight',2, ...
'SampleRate',2000); % Sample rate
fvtool(d)
Output:
Magnitude Response (dB)

-10

-20
Magnitude (dB)

-30

-40

-50

-60

-70
0 100 200 300 400 500 600 700 800 900
Frequency (Hz)

Note: In the figure window observe the following portion. You can click any menu for
corresponding output:

Example 7.2: Design a lowpass IIR with the following specifications:


SampleRate: 200000
FilterOrder: 8
PassbandFrequency: 35000
PassbandRipple: 0.2000
DesignMethod: 'cheby1'
MATLAB Code:
lpFilt = designfilt('lowpassiir','FilterOrder',8, ...
'PassbandFrequency',35e3,'PassbandRipple',0.2, ...
'SampleRate',200e3)
fvtool(lpFilt)

3|Page
Output:
Magnitude Response (dB)

Magnitude (dB) -50

-100

-150

-200

-250
0 10 20 30 40 50 60 70 80 90
Frequency (kHz)

Example 7.3: Design a highpass FIR with the following specifications:


SampleRate: 2
StopbandFrequency: 0.2500
PassbandFrequency: 0.3500
StopbandAttenuation: 65
PassbandRipple: 0.5000
DesignMethod: 'kaiserwin'
Solution:
MATLAB Code:
hpFilt = designfilt('highpassfir','StopbandFrequency',0.25, ...
'PassbandFrequency',0.35,'PassbandRipple',0.5, ...
'StopbandAttenuation',65,'DesignMethod','kaiserwin')
fvtool(hpFilt)

Output:
Magnitude Response (dB)
0

-10

-20
Magnitude (dB)

-30

-40

-50

-60

-70

-80

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9


Normalized Frequency ( rad/sample)

Example 7.4: Design a highpass IIR with the following specifications:

4|Page
SampleRate: 200000
FilterOrder: 8
PassbandFrequency: 75000
PassbandRipple: 0.2000
DesignMethod: 'cheby1'

Solution: MATLAB Code:


hpFilt = designfilt('highpassiir','FilterOrder',8, ...

'PassbandFrequency',75e3,'PassbandRipple',0.2, ...
'SampleRate',200e3);
fvtool(hpFilt)

Output:
Magnitude Response (dB)

-50
Magnitude (dB)

-100

-150

-200

-250

0 10 20 30 40 50 60 70 80 90
Frequency (kHz)

Example 7.5: Design a bandpass FIR with the following specifications:


SampleRate: 1500
FilterOrder: 20
CutoffFrequency1: 500
CutoffFrequency2: 560
DesignMethod: 'window'
MATLAB Code:
bpFilt = designfilt('bandpassfir','FilterOrder',20, ...
'CutoffFrequency1',500,'CutoffFrequency2',560, ...
'SampleRate',1500);
fvtool(bpFilt)

Output:

5|Page
Magnitude Response (dB)
0

-10

-20

Magnitude (dB)
-30

-40

-50

-60

-70
0 100 200 300 400 500 600 700
Frequency (Hz)

Example 7.6: Design a bandpass IIR with the following specifications:


SampleRate: 1500
FilterOrder: 20
HalfPowerFrequency1: 500
HalfPowerFrequency2: 560
DesignMethod: 'butter'

MATLAB Code:
bpFilt = designfilt('bandpassiir','FilterOrder',20, ...
'HalfPowerFrequency1',500,'HalfPowerFrequency2',560, ...
'SampleRate',1500)
fvtool(bpFilt)
dataIn = randn([1000 1]); dataOut = filter(bpFilt,dataIn);

Output:
Magnitude Response (dB)

-50

-100
Magnitude (dB)

-150

-200

-250

-300

-350

-400
0 100 200 300 400 500 600 700
Frequency (Hz)

Example 7.7: Design a bandstop FIR with the following specifications:


Specifications:
SampleRate: 1500
FilterOrder: 20
CutoffFrequency1: 500
CutoffFrequency2: 560
DesignMethod: 'window'
6|Page
MATLAB Code:
bsFilt = designfilt('bandstopfir','FilterOrder',20, ...
'CutoffFrequency1',500,'CutoffFrequency2',560, ...
'SampleRate',1500)
fvtool(bsFilt)

Output:
Magnitude Response (dB)
0

-1
Magnitude (dB)

-2

-3

-4

-5
0 100 200 300 400 500 600 700
Frequency (Hz)

Example 7.8: Design a bandstop IIR with the following specifications:


SampleRate: 1500
FilterOrder: 20
HalfPowerFrequency1: 500
HalfPowerFrequency2: 560
DesignMethod: 'butter'

MATLAB Code:
bsFilt = designfilt('bandstopiir','FilterOrder',20, ...
'HalfPowerFrequency1',500,'HalfPowerFrequency2',560, ...
'SampleRate',1500)
fvtool(bsFilt)

Output:
Magnitude Response (dB)

-20
Magnitude (dB)

-40

-60

-80

-100

-120
0 100 200 300 400 500 600 700
Frequency (Hz)

7|Page
Lab Session 7.2: Filter Design Using fdatool
The Filter Design and Analysis Tool (FDATool) is a powerful user interface for designing
and analyzing filters quickly. FDATool enables you to design digital FIR or IIR filters by
setting filter specifications, by importing filters from your MATLAB workspace, or by
adding, moving or deleting poles and zeros. FDATool also provides
tools for analyzing filters, such as magnitude and phase response and pole-zero plots.

Introduction to the Filter Design and Analysis Tool (FDATool):


If you type

>>fdatool

in command window, FDAtool will be opened. There you can select FIR or IIR filter, order
of filter and cutoff frequency of a filter (either HPF, LPF or BPF). That code will
automatically generate .m file for you.

The GUI has three main regions:

 The Current Filter Information region


 The Filter Display region and
 The Design panel

The upper half of the GUI displays information on filter specifications and responses
for the current filter. The Current Filter Information region, in the upper left, displays
filter properties, namely the filter structure, order, number of sections used and
whether the filter is stable or not. It also provides access to the Filter manager for
working with multiple filters.

8|Page
The Filter Display region, in the upper right, displays various filter responses, such
as, magnitude response, group delay and filter coefficients.

The lower half of the GUI is the interactive portion of FDATool. The Design Panel,
in the lower half is where you define your filter specifications. It controls what is
displayed in the other two upper regions. Other panels can be displayed in the lower
half by using the sidebar buttons.

Designing a Filter:
 Low Pass FIR filter
 Filter Order 30
 A passband frequency 0.2 [Normalized (0 to 1)]
 A stopband frequency 0.5 [Normalized (0 to 1)]

To implement this design, we will use the following specifications:

 Select Lowpass from the dropdown menu under Response Type and
Equiripple under FIR Design Method.In general, when you change the
Response Type or Design Method, the filter parameters and Filter Display
region update automatically.

 Select Specify order in the Filter Order area and enter 30.

 The FIR Equiripple filter has a Density Factor option which controls the
density of the frequency grid.Increasing the value creates a filter which more
closely approximates an ideal equiripple filter, but more time is required as
the computation increases. Leave this value at 20.

 Select Normalized (0 to 1) in the Units pull down menu in the Frequency


Specifications area.
9|Page
 Enter 0.2 for wpass and 0.5 for wstop in the Frequency Specifications area.

 Wpass and Wstop, in the Magnitude Specifications area are positive


weights, one per band, used during optimization in the FIR Equiripple filter.
Leave these values at 1.

 After setting the design specifications, click the Design Filter button at the
bottom of the GUI to design the filter.

 The magnitude response of the filter is displayed in the Filter Analysis area
after the coefficients are computed.

Viewing other Analyses:

Once you have designed the filter, you can view the following filter analyses in the
display window by clicking any of the buttons on the toolbar:

In order from left to right, the buttons are


 Magnitude response
 Phase response
 Magnitude and Phase responses
 Group delay response
 Phase delay response

10 | P a g e
 Impulse response
 Step response
 Pole-zero plot
 Filter Coefficients
 Filter Information
Changing Axes Units:
You can change the x- or y-axis units by right-clicking the mouse on an axis label
and selecting the desired units.

Marking Data Points:

In the Display region, you can click on any point in the plot to add a data marker,
which displays the values at that point. Right-clicking on the data marker displays a
menu where you can move, delete or adjust the appearance of the data markers.

Exporting to a Simulink Model:


1. After designing your filter, click the Realize Model sidebar button or select File >
Export to Simulink Model. The Realize Model panel is displayed.

11 | P a g e
2. Specify the name to use for your block in Block name.
3. Select the Destination — Current to insert the block into the current (most recently
selected) Simulink model or New to open a new model.
4. If you want to overwrite a block previously created from this panel, check Overwrite
generated `Filter' block.
5. Click the Realize Model button to create the filter block. The filter is implemented as
a subsystem block using Sum, Gain, and Integer Delay blocks.

Generating an M-File:
FDATool allows you to generate M-code to re-create your filter. This enables you to embed
your design into existing code or automate the creation of your filters in a script.
Select Generate M-file from the File menu and specify the filename in the Generate M-file
dialog box.
The following code was generated from the minimum order filter we designed above:

Examples of Filter design using fdatool :


12 | P a g e
Example 7.9
Design an FIR filter with the following specifications using Kaiser window :
Passband edge 2 rad/sec
Stopband edge 4 rad/sec
Maximum passband ripple 0.1dB
Minimum stopband attenuation 40 dB
Sampling frequency 20 rad/sec
Show (i) magnitude response (ii) impulse response (iii) Pole zero plot
Solution :
MATLAB code to find the order and Kaiser parameter

fs=20;
f=[2 4];
a=[1 0];
Rp=0.1;
Rs=40;
dp=1-10^(-Rs/20);
ds=10^(-Rs/20);
dev=[dp ds];
[N,Wn,beta,ftype]=kaiserord(f,a,dev,fs)
The order N and β are found to be,
N = 23 and, beta = 3.3953

Now use these data to design the filter.


Insert data as shown in the following figure:

13 | P a g e
Ma g n itu d e R e s p o n s e ( d B )

-10

-20
Magnitude (dB)

-30

-40

-50

-60

0 1 2 3 4 5 6 7 8 9
Fr e q u e n c y ( Hz )

Ph a s e R e s p o n s e

-5
Phase (radians)

-10

-15
0 1 2 3 4 5 6 7 8 9
Fr e q u e n c y ( H z )
Po le /Z e r o Plo t

0 .8

0 .6

0 .4
Imaginary Part

0 .2
23
0

- 0 .2

- 0 .4

- 0 .6

- 0 .8

-1

-1 - 0 .5 0 0 .5 1 1 .5 2
Re a l Pa r t

Example 7.10 :
Design an IIR Butterworth bandpass filter with the following specifications :

14 | P a g e
Normalized passband edges are at 0.4 and 0.65
Normalized stopband edges at 0.3 and 0.75
Passband ripple 1 dB
Minimum stopband attenuation 40dB.
Show (i) Magnitude response (ii) Phase response (iii) Group delay response (iv) Pole zero
plot
Solution :
Use the specifications as shown in the figure :

15 | P a g e
Ma g n itu d e R e s p o n s e ( d B )

-20

-40

-60

Magnitude (dB)
-80

-100

-120

-140

-160

-180

0 0 .1 0 .2 0 .3 0 .4 0 .5 0 .6 0 .7 0 .8 0 .9
No r ma liz e d Fr e q u e n c y (   r a d /s a m p le )

Ph a s e Re s p o n s e

10

5
Phase (radians)

-5

-10

0 0 .1 0 .2 0 .3 0 .4 0 .5 0 .6 0 .7 0 .8 0 .9
No r m a liz e d Fr e q u e n c y (   r a d /s a m p le )

G r o u p De la y

25

20
Group delay (in samples)

15

10

0 0 .1 0 .2 0 .3 0 .4 0 .5 0 .6 0 .7 0 .8 0 .9
No r m a liz e d Fr e q u e n c y (   r a d /s a mp le )

16 | P a g e
Po le /Z e r o Plo t

0 .8

0 .6

0 .4
Imaginary Part
0 .2
8 8
0

- 0 .2

- 0 .4

- 0 .6

- 0 .8

-1
- 1 .5 -1 - 0 .5 0 0 .5 1 1 .5
Re a l Pa r t

Lab Session 7.3: In Lab Evaluation


Design a bandstop FIR with the following specifications:
Specifications:
SampleRate: 1000
FilterOrder: 30
CutoffFrequency1: 300
CutoffFrequency2: 500
DesignMethod: 'window'

Home Work:
1. Use designfilt to design FIR and IIR versions of a highpass filter. Specify a normalized
stopband frequency of 0.3 and a normalized passband frequency of 0.6. Verify that each
filter is of the correct class. Display the frequency responses of the filters.[Hints: Use
isfir(fir) and
isfir( ) functions]
2. Use the window method to design a tenth-order lowpass FIR filter with normalized cutoff
frequency 0.55. Verify that the filter has linear phase. .[Hints: Use islinphase( ) ]
3. An ideal low pass filter requires an infinite impulse response. Truncating (or windowing) the
impulse response results in the so-called window method of FIR filter design. Consider a simple
design of a low pass filter with a cutoff frequency of 0.4*pi radians per sample.

17 | P a g e

You might also like