Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Lab 09-Design of Filters -2

Lab Engineer: Muhammad Hammad

1 Objectives:
. This Lab provides a complete example of using the GUI-based interactive tools to design and
implement an FIR digital filter, apply it to a signal, and display signals and spectra.
window method.
2 Time Required: 3 hrs
.
3 Programming Language: MATLAB
.
4 Software Required:
. (a). MATLAB 7 or above

SPTOOL:

This section provides a complete example of using the GUI-based interactive tools to design and
implement an FIR digital filter, apply it to a signal, and display signals and spectra.

The steps include:

•Importing and naming a signal using SPTool


•Designing a filter using the Filter Designer
•In SPTool, applying the filter to the signal to create another signal
•Viewing the time domain information of the original and filtered signals using the Signal
Browser
•Comparing the spectra of both signals using the Spectrum Viewer

Create, Import, and Name a Signal

You can import an existing signal into SPTool, or you can create a new signal and edit and name
it in SPTool. In this step, you’ll create a new signal at the command line and then import it into
SPTool.

1) At the command line, create a random signal by typing


x = randn(5000,1);

2) Activate SPTool by typing


sptool
The SPTool window is displayed.

3) Select Import... from the File menu:


The Import to SPTool window is displayed.
Notice that the variable x is displayed in the Workspace Contents list. (If it is not, click the From
Workspace radio button to display the contents of the workspace.)

4) Name the signal and import it into SPTool:


• Make sure that Signal is selected in the Import As pop-up menu.
• Click in the Data field and type x.
• You can also move the variable x into the Data field by clicking on x in the Workspace
Contents list and then clicking on the arrow to the left of the Data field.
• Click in the Sampling Frequency field and type 5000.
• Name the signal by clicking in the Name field and typing noise.
• Press OK.
The SPTool window is reactivated, and the signal noise[vector] is selected in the Signals list.
Design a Filter
1. You can import an existing filter into SPTool, or you can design and edit a new filter
using the Filter Designer. In this step, you’ll create a default filter and customize it in the
Filter Designer.
2. Press New Design in SPTool to activate the Filter Designer and generate a default filter.
The Filter Designer window is displayed with the default filter filt1.
3. Change the filter sampling frequency to 5000 by entering this value in the Sampling
Frequency text box in the Filter Designer.
Specify the filter parameters shown:
• Make sure Equiripple FIR is selected in the Algorithm pop-up menu.
• Select bandpass from the Type pop-up menu.
• Set the passband edge frequencies by entering 750 for Fp1 and 1250 for Fp2.
• Set the stopband edge frequencies by entering 500 for Fs1 and 1500 for Fs2.
• Type .01 into the Ap field and 75 into the As fields. Ap sets the maximum passband
ripple and As sets the stopband attenuation for the filter.
• Press the Apply button to compute the new filter.
When the new filter is computed, the magnitude response of the filter is displayed with a
solid line in the main axes display area. The resulting filter is an order 48 bandpass equiripple
filter.
Magnitude Response (dB)

-10

Magnitude (dB) -20

-30

-40

-50

-60

-70

-80
0 0.5 1 1.5 2
Frequency (kHz)

Apply the Filter to a Signal


In this step, you apply the filter to the signal in SPTool. The new, filtered signal is automatically
created in SPTool:
1) Activate SPTool from the Window menu in the Filter Designer.

2) Click to select the signal noise[vector] from the Signals list and click to select the filter
(named filt1[design]) from the Filters list, as shown below.

3) Press Apply to apply the filter filt1 to the signal noise. The Apply Filter dialog box is
displayed.
4) Name the new signal by clicking in the Output Signal field and typing blnoise.
5) Press OK.

The filter is applied to the selected signal. The new, filtered signal blnoise[vector] is displayed in
the Signals list.

View, Play, and Print the Signals


You can view and print the time domain information of the signals using the Signal Browser.
You can also play the signals, if your computer has audio output capabilities. In this step, you’ll
display both signals in the Signal Browser, select and play each signal, and print both signals:

1) Shift-click on the noise and blnoise signals in the Signals list of SPTool to select both signals.
2) Press View in the Signals panel.

The Signal Browser is activated and both signals are displayed in the main axes display area.
Initially, the noise signal covers up the bandlimited blnoise signal, but you can see that both
signals are displayed because the names of both signals are shown above the main axes display
area.
3) Click-and-drag in the Selection pop-up menu to select the blnoise signal.

The main axes display area is redisplayed. Now you can see the blnoise signal superimposed on
top of the noise signal. The signals are displayed in different colors in the main axes display area.
Notice that the color of the line in the Selection display changes to correspond to the color of the
signal that you’ve selected.

The signal that’s displayed in the Selection pop-up menu and in the Selection display is the
active signal. When you select Play, or use the rulers, the active signal is the one that is played or
measured.
4) To hear the active signal, select Play from the Options menu.
5) To hear the other signal, select it as in step 3 above (you can also select the signal by clicking
on it in the main axes display area). Select Play from the Options menu again.
6) You can also print the signals. To print both signals, select Print... from the File menu in the
Signal Browser.
Compare Spectra of Both Signals
You can get an idea of the frequency content of the two signals by displaying their power spectra
using the Spectrum Viewer, as described below:

1) Reactivate SPTool by selecting it from the Window menu of the Signal Browser.
2) Click on the noise[vector] signal in the Signals list of SPTool to select it.
3) Press Create in the Spectra panel. The Spectrum Viewer is activated, and a spectrum object
(spect1) corresponding to the noise signal is created in the Spectra list. The spectrum is not
computed or displayed yet.
4) Press Apply in the Spectrum Viewer to compute and display spect1. The spectrum of the noise
signal is displayed in the main axes display area. Notice that the spectrum’s signal identification
information, including its name, its type, and its sampling frequency, is displayed above the
Parameters panel, and the spectrum’s name is displayed both above the
main axes display area and in the Selection pop-up menu.
5) Reactivate SPTool by selecting it from the Window menu in the Spectrum Viewer.
6) Click on the blnoise signal in the Signals list of SPTool to select it.
7) Press Create in the Spectra panel. The Spectrum Viewer is again activated, and a spectrum
object (spect2) corresponding to the blnoise signal is created in the Spectra list. The spectrum is
not computed or displayed yet.
8) Press Apply in the Spectrum Viewer to display spect2. The spectrum of the blnoise signal is
displayed in the main axes display Area. The new spectrum’s signal identification information,
including its name, its type, and its sampling frequency, is displayed above the Parameters panel,
and the spectrum’s name is displayed both above the main axes display area and in the Selection
pop-up menu.
9) Reactivate SPTool again, as in step 5 above.
10) Shift-click on spect1 and spect2 in the Spectra list to select them both.
11) Press View in the Spectra panel to reactivate the Spectrum Viewer and display both spectra
together.
12) To select one of the spectra for measuring or editing, use the Selection pop-up menu, or click
on the spectrum in the main axes display area.
13) You can also print these spectra by using the print option.

IIR Filter Design

The most common method of IIR filter design is based on the bilinear transformation of a
prototype analog transfer function. The analog transfer function is usually one of the following
types: Butterworth, Type 1 Chebyshev, Type 2 Chebyshev, and elliptic transfer functions. The
difference between these filter types can be explained by considering the lowpass filter. The
Butterworth lowpass transfer function has a maximally-flat magnitude response, that is, and a
monotonically decreasing magnitude response with increasing frequency. TheType 1 Chebyshev
lowpass transfer function has an equiripple magnitude response in the passband and a
monotonically decreasing magnitude response with increasing frequency outside the passband.
The Type 2 Chebyshev lowpass transfer function has a monotonically decreasing magnitude
response in the passband with increasing frequency and an equiripple magnitude response in the
stopband. Finally, the elliptic lowpass transfer function has equiripple magnitude responses both
in the passband and in the stopband.

Estimation of Order of IIR Filter

The first step in the filter design process is to choose the type of filter approximation to be
employed and then to estimate the order of the transfer function from the filter specifications.
The MATLAB command for estimating the order of a Butterworth filter is

[N, Wn] = buttord(Wp, Ws, Rp, Rs),


where the input parameters are the normalized passband edge frequency Wp, the normalized
stopband edge frequency Ws, the passband ripple Rp in dB, and the minimum stopband
attenuation Rs in dB. Both Wp and Ws must be a number between 0 and 1 with the sampling
frequency assumed to be 2 Hz. The output data are the lowest order N meeting the specifications
and the normalized cutoff frequency Wn. If Rp = 3 dB, then Wn = Wp. Buttord can also be used
to estimate the order of a highpass, a bandpass, and a bandstop Butterworth filter. For a highpass
filter design,Wp > Ws. For bandpass and bandstop filter designs, Wp and Ws are two-element
vectors specifying both edge frequencies, with the lower edge frequency being the first element
of the vector. In the latter cases, Wn is also a two-element vector.
For estimating the order of a Type 1 Chebyshev filter, the MATLAB command is
[N, Wn] = cheb1ord(Wp, Ws, Rp, Rs)
and for designing a Type 2 Chebyshev filter, the MATLAB command for estimating the
order is
[N, Wn] = cheb2ord(Wp, Ws, Rp, Rs).
Finally, in the case of an elliptic filter design, the command is
[N, Wn] = ellipord(Wp, Ws, Rp, Rs).
As before, Wp and Ws are the passband and stopband edge frequencies with values between 0
and 1. Likewise, Rp and Rs are the passband ripple and the minimum stopband attenuation in
dB. N contains the estimated lowest order and Wn is the cutoff frequency. It should be noted that
for bandpass and bandstop filter designs, the actual order of the transfer function obtained using
the appropriate filter design command is 2N.

IIR Filter Design

After the filter type has been selected and its order estimated, the next step is to determine the
transfer function of the filter. To this endMATLAB provides functions for all four types of
filters. For designing Butterworth digital lowpass or bandpass filters, the command is
[num,den] = butter(N,Wn)
where the input parameters N and Wn are determined through the use of the function buttord,
and the output is the vectors num and den containing, respectively, the coefficients of the
numerator and denominator polynomials of the transfer function in ascending powers of z−1. If
Wn is a scalar, butter returns a lowpass transfer function of order N, and if Wn is a two-element
vector, it returns a bandpass transfer function of order 2N. For designing a Butterworth digital
highpass filter of order N, the command is
[num,den] = butter(N,Wn,’high’)
whereas, the command
[num,den] = butter(N,Wn,’stop’) returns the transfer function of a Butterworth bandstop filter of
order 2N provided Wn is a two-element vector. For designing a Type 1 Chebyshev digital filter,
the commands are
[num,den] = cheby1(N,Rp,Wn)
[num,den] = cheby1(N,Rp,Wn,’filtertype’)
For designing a Type 2 Chebyshev digital filter, the commands are
[num,den] = cheby2(N,Rs,Wn)
[num,den] = cheby2(N,Rs,Wn,’filtertype’)
Finally, for designing an elliptic digital filter, the commands are
[num,den] = ellip(N,Rp,Rs,Wn)
[num,den] = ellip(N,Rp,Rs,Wn,’filtertype’)

A lowpass transfer function of order N is returned in each case if Wn is a scalar, and a bandpass
transfer function of order 2N is returned if Wn is a two-element vector. In each of the above
commands, filtertype is high for designing a highpass filter with Wn being a scalar, and filtertype
is stop for designing a bandstop filter with Wn being a two-element vector.

Write a Program that illustrates the design of a Butterworth bandstop filter and
Chebyshev type 2 bandpass filter.

You might also like