15 FIR Filter Design by Windowing
15 FIR Filter Design by Windowing
This document defines five standard windowing functions used in digital filter design: Bartlett, Hanning,
Hamming, Blackman, and Kaiser. Two design examples illustrate the application of these functions. You
enter the following parameters:
Example designs for a lowpass filter using a Hamming window and a Kaiser window are shown.
References
Example 8.2 follows the discussion in Section 7.4 of Oppenheim and Schafer, Discrete-Time Signal
Processing, Prentice Hall. See also T. W. Parks and C. S. Burrus, Digital Filter Design, John Wiley & Sons
(New York, 1987).
Background
A linear-phase FIR filter can be obtained by windowing, or truncating, the impulse response of an ideal filter
with one of several available windowing functions. The window truncation in time results in a convolution of
the window transform with the filter transform in frequency.
Choosing a Window
The sampled window in time should be as short as possible, to reduce calculation time. The window should
also be narrow in frequency, so that convolution with the impulse response results in minimal distortion.
Since time and frequency are inversely and inextricably related, these two requirements are at odds.
A variety of window shapes in time can be used to minimize undesirable effects in the frequency domain.
Each optimizes some feature of the resulting frequency response: some provide narrower passbands with less
attenuation in the stopbands, some provide shallower transition regions with better attenuation in the
stopband, etc.
Phase
Since filter design is concerned with phase distortion as well as amplitude filtering, it's important to know
how windowing affects the phase of the original filter. Windows that are symmetric in time do not distort the
phase of the system.
Mathcad Implementation
This document defines some of the standard windowing functions used in FIR filter design and illustrates
their application.
Below are definitions and plots of five common windowing functions: Bartlett, Hanning, Hamming,
Blackman, and Kaiser. The first four are given as functions of two variables (the length and the term index).
The Kaiser window is also a function of the parameter b; it is plotted below for b = 5.
Rectangular window:
R (n , N) ≔ (n ≥ 0) ⋅ (n ≤ N − 1)
Hanning window:
⎛ ⎛ 2 ⋅ π ⋅ n ⎞⎞
Hn (n , N) ≔ ⎜.5 − .5 ⋅ cos ⎜――― ⋅ R (n , N)
⎝ ⎝ N − 1 ⎟⎠⎟⎠
Hamming window:
⎛ ⎛ 2 ⋅ π ⋅ n ⎞⎞
Hm (n , N) ≔ ⎜.54 − .46 ⋅ cos ⎜――― ⋅ R (n , N)
⎝ ⎝ N − 1 ⎟⎠⎟⎠
Blackman window:
⎛ ⎛2⋅π⋅n⎞ ⎛ 4 ⋅ π ⋅ n ⎞⎞
Bl (n , N) ≔ ⎜.42 − .5 ⋅ cos ⎜――― + .08 ⋅ cos ⎜⎝――― ⋅ R (n , N)
⎝ ⎝ N − 1 ⎟⎠ N − 1 ⎟⎠⎟⎠
1 1
0.9 0.9
0.8 0.8
0.7 0.7
0.6 0.6
0.5 0.5
Ba (n , N) 0.4 Hn (n , N) 0.4
0.3 0.3
0.2 0.2
0.1 0.1
0 0
0 2 4 6 8 10 12 14 16 18 20 0 2 4 6 8 10 12 14 16 18 20
n n
1 1
0.9 0.9
0.8 0.8
0.7 0.7
0.6 0.6
0.5 0.5
Hm (n , N) 0.4 Bl (n , N) 0.4
0.3 0.3
0.2 0.2
0.1 0.1
0 0
0 2 4 6 8 10 12 14 16 18 20 0 2 4 6 8 10 12 14 16 18 20
n n
1
0.9
0.8
0.7
0.6
0.5
K (n , N , 5) 0.4
0.3
0.2
0.1
0
0 2 4 6 8 10 12 14 16 18 20
n
length:
M ≔ 21 m≔0‥M−1
cutoff frequency:
ωc ≔ .5 ⋅ π
⎛ ⎛ M − 1 ⎞⎞
sin ⎜ωc ⋅ ⎜m − ――⎟⎟
⎝ ⎝ 2 ⎠⎠
r ≔ ―――――――
m ⎛ M−1⎞ −6 Add a small offset to avoid a divide
π ⋅ ⎜m − ――⎟ + 10 by zero when m = 10
⎝ 2 ⎠
⎛ M−1 M⎞
odd ≔ mod (M , 2) center ≔ if ⎜odd , ――, ―⎟
⎝ 2 2⎠
⎛ ωc ⎞
r ≔ if ⎜odd , ―, r ⎟
center ⎝ π center⎠
0.8
0.7
0.6
0.5
0.4
0.3
0.2
r 0.1
m
0
-0.1
-0.2
-0.3
-0.4
-0.5
0 2 4 6 8 10 12 14 16 18 20
m
This design example windows the impulse response of an ideal lowpass filter with a windowing function w.
This example uses a Hamming window. To see the effect of a different window, edit the function name on the
right side of the definition for w.
Mathcad calculates the vector of coefficients for the windowed impulse response using the vectorize operator,
which multiplies each element of r by the corresponding element of w.
To plot the frequency response, define range variables for the frequency:
k ≔ 0 ‥ 200
π⋅k
ω ≔ ―― h ≔ |H ⎛ω ⎞|
k 200 k | ⎝ k⎠|
1.1
1
0.9
0.8
0.7
0.6
h 0.5
k 0.4
0.3
0.2
0.1
0
0 0.3 0.6 0.9 1.2 1.5 1.8 2.1 2.4 2.7 3 3.3
ω
k
10
-10
-20
-30
-40
20 ⋅ log ⎛h ⎞ -50
⎝ k⎠
-60
-70
-80
-90
0 0.3 0.6 0.9 1.2 1.5 1.8 2.1 2.4 2.7 3 3.3
ω
k
V ≔m
m
0.51
0.425
0.34
0.255
0.17
a
0.085
-0.085
-0.17
0 2 4 6 8 10 12 14 16 18 20
V
Now, use a Kaiser window to design a filter with a given approximation error, d, and given stopband and
passband cutoff frequencies.
passband cutoff: ωp ≔ .4 ⋅ π
stopband cutoff: ωs ≔ .6 ⋅ π
First, set the cutoff frequency equal to the average of the passband and stopband cutoffs:
The following formulas calculate the necessary filter length and value of the parameter b for the Kaiser
window.
A ≔ −20 ⋅ log (δ)
⎛ ⎛ .4 ⎞⎞
β ≔ if ⎝A < 21 , 0 , if ⎝A ≤ 50 , .5842 ⋅ (A − 21) + .07886 ⋅ (A − 21) , .1102 ⋅ (A − 8.7)⎠⎠
⎛ A−8 ⎞
M ≔ ceil ⎜1 + ―――――
⎝ 2.285 ⋅ ⎛⎝ωs − ωp⎞⎠ ⎟⎠
⎛ ⎛ M − 1 ⎞⎞
sin ⎜ωc ⋅ ⎜m − ――⎟⎟
⎝ ⎝ 2 ⎠⎠
r ≔ ―――――――
m ⎛ M−1⎞
π ⋅ ⎜m − ――⎟
⎝ 2 ⎠
⎛ M−1 M⎞
odd ≔ mod (M , 2) center ≔ if ⎜odd , ――, ―⎟
⎝ 2 2⎠
⎛ ωc ⎞
r ≔ if ⎜odd , ―, r ⎟
center ⎝ π center⎠
Th filt ffi i t
The filter coefficients are
a ≔ r ⋅ K (m , M , β)
m m
1.1
0.9
0.8
0.7
0.6
h 0.5
k 0.4
0.3
0.2
0.1
0 0.3 0.6 0.9 1.2 1.5 1.8 2.1 2.4 2.7 3 3.3
ω
k
10
-10
-20
-30
-40
20 ⋅ log ⎛h ⎞ -50
⎝ k⎠
-60
-70
-80
-90
0 0.3 0.6 0.9 1.2 1.5 1.8 2.1 2.4 2.7 3 3.3
ω
k
1.005
1.004
1.003
1.002
1.002
1.001
h 1
0.999
k 0.998
0.998
0.997
0.996
1.155 1.175 1.195 1.215 1.235 1.255 1.275 1.295 1.315 1.335 1.355 1.375
ω
k
0.05
0.045
0.04
0.035
0.03
0.025
h 0.02
k 0.015
0.01
0.005
0
1.78 1.8 1.82 1.84 1.86 1.88 1.9 1.92 1.94 1.96 1.98 2
ω
k
In this example, the actual peak error is slightly larger than d. It can be calculated using the root and
derivative operators:
x ≔ ωp (guess for true error)
| ⎛ ⎛ d ⎞⎞|
|H ⎜root ⎜―― |H (x)| , x⎟⎟| − 1 = 0.00113 actual peak error
| ⎝ ⎝ dx ⎠⎠|
The actual passband and stopband cutoff frequencies corresponding to the design value of d can be calculated
using the root function, using the original design for guess values:
x ≔ ωp y ≔ ωs
The pass band and stop band frequencies found in this way are, respectively, a bit larger than 0.4p and a bit
smaller than 0.6p, which agrees with the original design values.