Vous êtes sur la page 1sur 4

Page 1 of 4

Signal and Image Processing

2. Digital filtering
This exercise will introduce FIR and IIR digital filters (lowpass, highpass, passband, stopband),
their frequency and impulse response. Filter use for signal filtering is also demonstrated.

Assignment:
A. FIR Filters
1. Display and draw in your workbook the FIR lowpass filter frequency response for program
setting (order=100, sampling frequency Fs=1000Hz, cutoff frequency f1=60Hz, rectangular
window). Determine the approximate transition width and the ripple.
2. Change type of the window to Hann (hanning). Compare the transition width and the ripple with
the previous case
3. Change type of the window to another one from the windows offer. Compare the transition
width and the ripple with the previous cases
4. Using above described filters try to filter the harmonic signal with amplitude 1V, offset 2V,
Fsignal=100Hz, number of samples N=256. Put the graphs of the input and output signals into
your notebook and find the approximate time delay of the output voltage as compared to the
input voltage.
B. IIR Filters
5. Display and draw in your workbook the IIR Butherworth lowpass filter frequency response for
program default setting (filter order M=3, sampling frequency Fs=1000Hz, cutoff frequency
f1=60Hz). Determine the width of the transition band (for passband ripple 0.1 and stopband
ripple 0.01).
6. Set order of the filter on 10. Compare the transition width to the previous case, draw the
frequency response.
7. Use the Chebychev 1, Chebychev 2 and Elliptic filters of the order M=. Compare the transition
widths, draw the frequency responses
8. Filter input harmonic signal with amplitude 1 V, offset 2 V and frequency 100 Hz using
Butterworth filters (M=3, fs=1000 Hz, f1 = 50 Hz, f2 =150 Hz) of the type: low pass, high pass,
band stop and band pass. Sketch the original and filtered waveforms for each filter into your
notebook. Explain the cause of noise reduction and variation of filtered signal amplitude.
9. Make yourself acquainted with source code of IIR.M
10. In the time left experiment with different filters design (see instructions below).

Guide:
For demonstration of FIR filter design run FIR_AN in Matlab Command Window, script for IIR
filters is called IIR_AN.

Hints for performing the exercise


You can get the MATLAB icon using commands:
login matlab.k-338
matlab
Then start the program for fiters by the command FIR_AN.M or IIR_AN.M in the MATLAB
command window.
To be able to reproduce figures in MATLAB into your notebook, start simultaneously with MATLAB
a text editor (e.g. MS Word). Using command Edit-Copy (Alt+PrintScreen) allows you to put the
chosen figure into a box. You can get the figure using command Paste (Shift+Insert) in the text
editor. The figure can be then modified and printed.
Switching among the applications under MS Windows is performed by pressing keys Alt+Tab.
The control of the MATLAB programs is easy. You can set parameters and activate given
operation using mouse. Computation can be started using push-button Run (Start). You can also
zooming the chosen part of the figure. Zoom is canceled by double-clicking anywhere in the graph.

Signal and Image Processing

Page 2 of 4

Additional information is available under the push-button Info.

MATLAB supports several LTID (linear time-invariant discrete) system models:


tf - Transfer function - polynomial form with the coefficients b and a
[z, p, n] - Zero-pole-gain form -zeros, poles and filter order n
[A, B, C, D]- State-space form
SOS Second-order section form - cascade of the second-order sections

IIR Filter Design


You can easily create a filter of any order with a lowpass, highpass, bandpass, or bandstop
configuration using the filter design functions:
Filter Type - Design Function
Butterworth
[b,a] = butter(n,Wn,options)
[z,p,k] = butter(n,Wn,options)
Chebyshev type I
[b,a] = cheby1(n,Rp,Wn,options)
[z,p,k] = cheby1(n,Rp,Wn,options)
Chebyshev type II
[b,a] = cheby2(n,Rs,Wn,options)
[z,p,k] = cheby2(n,Rs,Wn,options)
Elliptic
[b,a] = ellip(n,Rp,Rs,Wn,options)
[z,p,k] = ellip(n,Rp,Rs,Wn,options)
Bessel (analog only)
[b,a] = besself(n,Wn,options)
[z,p,k] = besself(n,Wn,options)
By default, each of these functions returns a lowpass filter; you need only to specify the desired
cutoff frequency Wn in normalized frequency (to the Nyquist frequency, i.e. half of the sampling
frequency; that means, that for Nyquist frequency Wn is 1 ). For a highpass filter, append the
string 'high' to the functions parameter list. For a bandpass or bandstop filter, specify Wn as a twoelement vector containing the passband edge frequencies, appending the string 'stop' for the
bandstop configuration.
Examples:
1. For data sampled at 1000 Hz, design a 9th-order highpass Butterworth filter with cutoff
frequency of 300 Hz:
[b,a] = butter(9,300/500,'high')
The filters frequency response
freqz(b,a,128,1000) %128 equidistant points from 0 to 1, fs = 1000 Hz
2. Design a 10th-order bandpass Butterworth filter with a passband from 100 to 200 Hz and plot its
impulse response, or unit sample response:
n = 5; Wn = [100 200]/500;
[b,a] = butter(n,Wn);
[y,t] = impz(b,a,101);
stem(t,y)
3. For data sampled at 1000 Hz, design a ninth-order lowpass Chebyshev type II filter with
stopband attenuation 20 dB down from the passband and a cutoff frequency of 300 Hz:

Signal and Image Processing

Page 3 of 4

[b,a] = cheby2(9,20,300/500);
The frequency response of the filter:
freqz(b,a,512,1000)
4. zp2sos converts a zero-pole-gain representation of a given system to an equivalent secondorder section representation.
Example :
Find a second-order section form of a Butterworth lowpass filter:
[z,p,k] = butter(5,0.2);
sos = zp2sos(z,p,k);
Direct IIR Filter Design
Unlike the analog prototyping method, direct design methods are not constrained to the standard
lowpass, highpass, bandpass, or bandstop configurations. Rather, these functions design filters
with an arbitrary, perhaps multiband, frequency response. yulewalk designs recursive IIR digital
filters by fitting a specified frequency response. yulewalks name reflects its method for finding the
filters denominator coefficients: it finds the inverse FFT of the ideal desired power spectrum and
solves the modified Yule-Walker equations using the resulting autocorrelation function samples.

FIR Filters
Windowing Method
a) Standard Band FIR Filter Design:
b = fir1(n,Wn,options)
fir1 implements the classical method of windowed linear phase FIR digital filter design. It
resembles the IIR filter design functions in that it is formulated to design filters in standard band
configurations: lowpass, bandpass, highpass, and bandstop.
The statements:
n = 50;
Wn = 0.4;
b = fir1(n,Wn);
create row vector b containing the coefficients of the order n Hamming-windowed filter. This is a
lowpass, linear phase FIR filter with cutoff frequency Wn. Wn is a number between 0 and 1, where
1 corresponds to the Nyquist frequency, half the sampling frequency. (Unlike other methods, here
Wn corresponds to the 6 dB point.) As a window you can use in option:
bartlett, blackman, boxcar, chebwin, hamming, hanning, kaiser, triang
Syntax:
b = fir1(n,Wn)
b = fir1(n,Wn,'ftype')
b = fir1(n,Wn,window)
b = fir1(n,Wn,'ftype',window)
where ftype is high for a highpass filter with cutoff frequency Wn, stop for a bandstop filter, if Wn =
[w1 w2], the stopband is w1 < < w2.
Examples
Design a 48th-order FIR bandpass filter with passband 0.35 < w < 0.65:
b = fir1(48,[0.35 0.65]);
freqz(b,1,512)
b) Multiband FIR Filter Design:
b = fir2(n,f,m,options)
The function fir2 also designs windowed linear-phase FIR filters, but with an arbitrarily shaped
piecewise linear frequency response. This is in contrast to fir1, which only designs filters in
standard lowpass, highpass, bandpass, and bandstop configurations.
Multiband FIR Filter Design with Transition Bands

Signal and Image Processing

Page 4 of 4

a) Least square linear-phase FIR filter design.


b = firls(n,f,a, options)
returns row vector b containing the n+1 coefficients of the order n FIR filter whose frequencyamplitude characteristics approximately match those given by vectors f and a. firls is an extension
of the fir1 and fir2 functions in that it minimizes the integral of the square of the error between the
desired frequency response and the actual frequency response.
b) Parks-McClellan optimal FIR filter design.
b = remez(n,f,a, options)
returns row vector b containing the n+1 coefficients of the order n FIR filter whose frequencyamplitude characteristics match those given by vectors f and a. remez implements the ParksMcClellan algorithm, which uses the Remez exchange algorithm and Chebyshev approximation
theory to design filters with optimal fits between the desired and actual frequency responses. The
filters are optimal in the sense that they minimize the maximum error between the desired
frequency response and the actual frequency response; they are some-times called minimax
filters. Filters designed in this way exhibit an equiripple behavior in their frequency response, and
hence are also known as equiripple filters.
f is a vector of pairs of frequency points, specified in the range between 0 and 1, where 1
corresponds to half the sampling frequency (the Nyquist frequency). The frequencies must be in
increasing order. a is a vector containing the desired amplitudes at the points specified in f. f and a
must be the same length. The length must be an even number.
Syntax
b = remez(n,f,a)
b = remez(n,f,a,w)
b = remez(n,f,a,'ftype')
b = remez(n,f,a,w,'ftype')
Example
Graph the desired and actual frequency responses of a 17th-order Parks-McClellan bandpass
filter:
f = [0 0.3 0.4 0.6 0.7 1]; a = [0 0 1 1 0 0];
b = remez(17,f,a);
[h,w] = freqz(b,1,512);
plot(f,a,w/pi,abs(h))