Sei sulla pagina 1di 6

Department of Biomedical Engineering

BME 3112 Biomedical Signal Processing Laboratory


Experiment No: 04
Instruction for Students: Please make you report ready (Hand written) before coming
to the lab as instructed in 1st class.

Name of the Experiment: FIR filter design by window method using MATLAB
Objective:

FIR filter design using MATLAB

The MATLAB Signal Processing Toolbox contains an excellent set of programs and
functions for the design and analysis of different types of FIR digital filters. The programs
and commands are readily accessible via high level commands and make the Toolbox a
valuable tool for gaining an insight into the design and analysis of FIR filters without getting
bogged down with extensive programming.
In this section, we will illustrate how to use some of the MATLAB functions and programs
to design linear phase FIR filters. In particular, we will illustrate how to calculate the
coefficients of linear phase FIR filters using window, optimal (Parks—McClellan) and
frequency sampling methods and MATLAB to complement the use of C language programs
discussed in the previous section.

Window method
The steps involved in the calculation of coefficients of standard, frequency selective, linear
phase FIR filters using the window method may be summarized as follows (see the main text
for details):
1. Specify the desired frequency response.
2. Select a window function and estimate the number of filter coefficients, N.
3. Obtain the ideal impulse response, hD(n) (truncated to N values).
4. Obtain N coefficients of the window function, w(n).
5. Obtain the FIR filter coefficients by applying the window, h(n) = h D(n) x
w(n)

For standard, frequency selective linear phase window-based FIR filter design (lowpass,
highpass, bandpass and bandstop filters), the key high-level command in the Toolbox is the fir1
command. The syntax for the basic firl command is:

b = fir1 (N-1, Fc)

The basic command computes and returns N-point impulse response coefficients of an FIR filter
with a cutoff frequency FC. The command returns the N-point coefficients in the vector b,
arranged in ascending negative powers of z:

b(z) = b(0) + b(1)z -1 + b(2)z -2 + . . . . + b(N-1)z –(N-1)


The parameter, N- 1, in the command specifies the order of the filter, (normally one less
than the number of FIR filter coefficients). The cutoff frequency, FC, is normalized with
respect to the Nyquist frequency (i.e., half the sampling frequency) and lies between 0
and 1 (where 1 corresponds to the Nyquist frequency).
By default, the basic firl command applies a Hamming window and assumes a lowpass filter
(or a bandpass filter if FC specifies more than one cutoff frequency). The basic command can
be extended by specifying the type of filter and/or the window function. The syntax in these
cases is:

b = fir1(N-1,Fc, ‘filter-type’)
b = fir1(N-1,Fc, window)
b = fir1 (N-1 ,Fc, ‘filter-type’, window)

For a highpass filter, the word ‘high’ specifies the filter type, and for bandstop the word 'stop'
is used. For both bandpass and bandstop filters, the variable FC is a vector that specifies the cutoff
frequencies. For highpass and bandstop filters, the filter length must be an odd integer (even
integers are unsuitable for highpass and bandstop filters because they lead to a zero magnitude
response at the Nyquist frequency as described previously).

MATLAB supports the use of a variety of window functions including Hamming, Hanning,
boxcar (rectangular), Kaiser and Chebyshev windows. The syntax for generating window
coefficients is:
w = boxcar(N)
w = blackman(N)
w = hamming(N)
w = hanning(N)
w = kaiser(N, beta)

In practice, the window command is often embedded into the firl command (see
examples later).
It should be pointed out that there may be differences in the results obtained in using
MATLAB to design window-based FIR filters compared to other programs because of
differences in implementation. For example, in MATLAB, after windowing the impulse
response coefficients may be scaled to give a magnitude-frequency response of unity in the
middle of the passband. The word ‘noscale’ may be added to override this, e.g.: b = firl (N-1, Fc,
‘noscale’). Further, MATLAB implementation of most window functions may be slightly
different from previous implementation and this may lead to slight differences in results. The
designer should be aware of such differences and make an appropriate allowance to
compensate for them, if necessary.
Example 7B.1
Determine the coefficients of a linear phase, FIR lowpass filter with a passband and stopband
edge frequency of 1 kHz and 4.3 kHz, respectively. Use the Hamming window and assume a
sampling frequency of 10 kHz.
From Table 7.3, the approximate relationship between the transition width, ∆𝑓; and filter
length, N, for a Hamming window-based filter is:
3.3
𝑁 ≈
∆𝑓
Now, ∆𝑓 is 0.33 (from (4.3 - 1)/10) and so the filter length N = 10. Following the approach
in the main text, the actual cutoff frequency (allowing for the smearing effect) is taken to lie
half-way between the specified passband and the stopband edge frequencies, i.e. 2.65 kHz.
In MATLAB, the cutoff frequency should be normalized to half the sampling frequency.
Thus, fC (normalized) = 2.65/5 = 0.53.
The MATLAB commands are given in Program 7B.1. The values of the truncated ideal
impulse response, the window and the windowed FIR coefficients are given in Table 7B.1

Program 7B.1 MATLAB m-file for computing FIR filter coefficients for
Example 7B.1.

fc = 0.53; % Cutoff frequency (normalized to Fs/2)


N = 10; % Filter length (number of taps)
hd = fir1(N - % Truncated ideal impulse response
1,fc,boxcar(N)); % Calculate Hamming window coefficients
wn = hamming(N); % Obtain windowed coefficients
hn = fir1 (N - 1 ,fc,wn);

Table 7B.1 Filter parameters for Example 7B.1.

Truncated ideal impulse Window Windowed


n
response, hD(n) coeffs, w(n) filter coeffs, h(n)
0 0.0641 0.0800 0.0053
1 -0.0388 0.1876 -0.0075
2 -0.1052 0.4601 -0.0496
3 0.1235 0.7700 0.0974
4 0.4564 0.9723 0.4544
5 0.4564 0.9723 0.4544
6 0.1235 0.7700 0.0974
7 -0.1052 0.4601 -0.0496
8 -0.0388 0.1876 -0.0075
9 0.0641 0.0800 0.0053
Example 7B.2 Illustrating FIR coefficient calculation using the Kaiser window Determine the
coefficients and plot the magnitude-frequency response of a bandpass FIR filter,
using the Kaiser window and MATLAB, that meets the following specifications:

passband 150-250 Hz
transition width 50 Hz
passband ripple 0.1 dB
stopband attenuation60 dB
sampling frequency1 kHz

Solution:
This problem is identical to Example 7.4 (Show the calculation here ) in the main text.
Here, we will use MATLAB to solve the problem.
Example 7.4 gives the filter length, N = 73, and the ripple parameter,β= 5.65. The
MATLAB program is listed in Program 7B.2. The filter coefficients and the magnitude
spectrum are shown in Table 7B.2 and Figure 7B.1.
It should be noted that in this example, we have included the window type in the fir1
command, instead of computing the window coefficients separately.
Program 7B.2 MATLAB m-file for calculating FIR filter coefficients for Example
7B.2.

FS=1000; % Sampling frequency


FN=FS/2; % Nyquist frequency
N=73; % Filter length
beta=5.65; % Kaiser window Ripple parameter
fc1=125/FN; % Normalized cut off frequencies
fc2=275/FN;
FC=[fc1 fc2];
% Band edge frequency vector
hn=fir1 (N-1, FC,
% Obtain windowed filter coeffs
kaiser(N, beta)); [H,
f]=freqz(hn, 1, 512, FS); % Compute frequency response
mag=20*log10(abs(H));
plot (f, mag), grid on
xlabel ('Frequency (Hz)')
ylabel (Magnitude Response
(dB)')

Table 7B.2
Filter coefficients for Example 7B.2.
n h(n)
0 -0.0001
1 -0.0004
2 -0.0001
3 -0.0001
4 -0.0007
5 0.0005
6 0.0023
7 0.0008
8 -0.0017
9 -0.0005
10 -0.0005
11 -0.0044
12 -0.0022
13 0.0069
14 0.0066
15 -0.0016
16 0.0000
17 0.0022
18 -0.0117
19 -0.0164
20 0.0069
21 0.0189
22 0.0029
23 0.0044
24 0.0188
25 -0.0125
26 -0.0520
27 -0.0165
28 0.0333
29 0.0104
30 0.0094
31 0.0856
32 0.0453
33 -0.1665
34 -0.2066
35 0.0891
36 0.2998

Lab Exercise: Changing the window types redo the above 2 example and show your result
and update in the report.

Potrebbero piacerti anche