Free Books

Window Method for FIR Filter Design

The window method for digital filter design is fast, convenient, and robust, but generally suboptimal. It is easily understood in terms of the convolution theorem for Fourier transforms, making it instructive to study after the Fourier theorems and windows for spectrum analysis. It can be effectively combined with the frequency sampling method, as we will see in §4.6 below.

The window method consists of simply ``windowing'' a theoretically ideal filter impulse response $ h(n)$ by some suitably chosen window function $ w(n)$ , yielding

$\displaystyle h_w(n) = w(n)\cdot h(n), \quad n\in{\cal Z}.$ (5.8)

For example, as derived in Eq.$ \,$ (4.3), the impulse response of the ideal lowpass filter is the well known sinc function

$\displaystyle h(n) \eqsp B\cdot$sinc$\displaystyle (B n) \isdefs B\frac{\sin(\pi B n)}{\pi B n}, \quad n\in{\bf Z},$ (5.9)

where $ B=2f_c$ is the total normalized bandwidth of the lowpass filter in Hz (counting both negative and positive frequencies), and $ f_c$ denotes the cut-off frequency in Hz. As noted earlier, we cannot implement this filter in practice because it is noncausal and infinitely long.

Since $ h(n) = B\,$sinc$ (BnT)$ decays away from time 0 as $ 1/n$ , we would expect to be able to truncate it to the interval $ [-N,N]$ , for some sufficiently large $ N$ , and obtain a pretty good FIR filter which approximates the ideal filter. This would be an example of using the window method with the rectangular window. We saw in §4.3 that such a choice is optimal in the least-squares sense, but it designs relatively poor audio filters. Choosing other windows corresponds to tapering the ideal impulse response to zero instead of truncating it. Tapering better preserves the shape of the desired frequency response, as we will see. By choosing the window carefully, we can manage various trade-offs so as to maximize the filter-design quality in a given application.

Window functions are always time limited. This means there is always a finite integer $ N_w$ such that $ w(n)=0$ for all $ \vert n\vert>N_w$ . The final windowed impulse response $ h_w(n) =
w(n)\cdot h(n)$ is thus always time-limited, as needed for practical implementation. The window method always designs a finite-impulse-response (FIR) digital filter (as opposed to an infinite-impulse-response (IIR) digital filter).

By the dual of the convolution theorem, pointwise multiplication in the time domain corresponds to convolution in the frequency domain. Thus, the designed filter $ h_w$ has a frequency response given by

$\displaystyle H_w(f) = \left(W\ast H\right)(f), \quad f\in\left[\,-1/2,\,1/2\,\right)$ (5.10)

where $ H=\hbox{\sc DTFT}(h)$ is the ideal frequency response and $ W=\hbox{\sc DTFT}(w)$ is the window transform. For the ideal lowpass filter, $ H$ is a rectangular window in the frequency domain. The frequency response $ H_w(f)$ is thus obtained by convolving the rectangular window with the window transform $ W(f)$ . This implies several points which can be immediately seen in terms of this convolution operation:

  • The pass-band gain is primarily the area under the main lobe of the window transform, provided the main lobe ``fits'' inside the pass-band (i.e., the total lowpass bandwidth $ B\isdeftext 2f_c$ is greater than or equal to the main-lobe width of $ W$ ).

  • The stop-band gain is given by an integral over a portion of the side lobes of the window transform. Since side-lobes oscillate about zero, a finite integral over them is normally much smaller than the side-lobes themselves, due to adjacent side-lobe cancellation under the integral.

  • The best stop-band performance occurs when the cut-off frequency is set so that the stop-band side-lobe integral traverses a whole number of side lobes.

  • The transition bandwidth is equal to the bandwidth of the main lobe of the window transform, again provided that the main lobe ``fits'' inside the pass-band.

  • For very small lowpass bandwidths $ B$ , $ H(\omega)$ approaches an impulse in the frequency domain. Since the impulse is the identity operator under convolution, the resulting lowpass filter $ H_w(\omega)$ approaches the window transform $ W(\omega)$ for small $ B$ . In particular, the stop-band gain approaches the window side-lobe level, and the transition width approaches half the main-lobe width. Thus, for good results, the lowpass cut-off frequency should be set no lower than half the window's main-lobe width.

Matlab Support for the Window Method

Octave and the Matlab Signal Processing Toolbox have two functions implementing the window method for FIR digital filter design:

  • fir1 designs lowpass, highpass, bandpass, and multi-bandpass filters.

  • fir2 takes an arbitrary magnitude frequency response specification.

The default window type is Hamming, but any window can be passed in as an argument. In addition, there is a function kaiserord for estimating the parameters of a Kaiser window which will achieve the desired filter specifications.

Bandpass Filter Design Example

The matlab code below designs a bandpass filter which passes frequencies between 4 kHz and 6 kHz, allowing transition bands from 3-4 kHz and 6-8 kHz (i.e., the stop-bands are 0-3 kHz and 8-10 kHz, when the sampling rate is 20 kHz). The desired stop-band attenuation is 80 dB, and the pass-band ripple is required to be no greater than 0.1 dB. For these specifications, the function kaiserord returns a beta value of $ \beta=7.85726$ and a window length of $ M=101$ . These values are passed to the function kaiser which computes the window function itself. The ideal bandpass-filter impulse response is computed in fir1, and the supplied Kaiser window is applied to shorten it to length $ M=101$ .

fs = 20000;                 % sampling rate
F = [3000 4000 6000 8000];  % band limits
A = [0 1 0];                % band type: 0='stop', 1='pass'
dev = [0.0001 10^(0.1/20)-1 0.0001]; % ripple/attenuation spec
[M,Wn,beta,typ] = kaiserord(F,A,dev,fs);  % window parameters
b = fir1(M,Wn,typ,kaiser(M+1,beta),'noscale'); % filter design

Note the conciseness of the matlab code thanks to the use of kaiserord and fir1 from Octave or the Matlab Signal Processing Toolbox.

Figure 4.6 shows the magnitude frequency response $ \vert B(\omega/2\pi)\vert$ of the resulting FIR filter $ b$ . Note that the upper pass-band edge has been moved to 6500 Hz instead of 6000 Hz, and the stop-band begins at 7500 Hz instead of 8000 Hz as requested. While this may look like a bug at first, it's actually a perfectly fine solution. As discussed earlier (§4.5), all transition-widths in filters designed by the window method must equal the window-transform's main-lobe width. Therefore, the only way to achieve specs when there are multiple transition regions specified is to set the main-lobe width to the minimum transition width. For the others, it makes sense to center the transition within the requested transition region.

Figure 4.6: Amplitude response of the FIR bandpass filter designed by the window method.

Under the Hood of kaiserord

Without kaiserord, we would need to implement Kaiser's formula [115,67] for estimating the Kaiser-window $ \beta $ required to achieve the given filter specs:

$\displaystyle \beta = \left\{\begin{array}{ll} 0.1102(A-8.7), & A > 50 \\ [5pt] 0.5842(A-21)^{0.4} + 0.07886(A-21), & 21< A < 50 \\ [5pt] 0, & A < 21, \\ \end{array} \right. \protect$ (5.11)

where $ A$ is the desired stop-band attenuation in dB (typical values in audio work are $ A=60$ to $ 90$ ). Note that this estimate for $ \beta $ becomes too small when the filter pass-band width approaches zero. In the limit of a zero-width pass-band, the frequency response becomes that of the Kaiser window transform itself. A non-zero pass-band width acts as a ``moving average'' lowpass filter on the side-lobes of the window transform, which brings them down in level. The kaiserord estimate assumes some of this side-lobe smoothing is present.

A similar function from [198] for window design (as opposed to filter design5.7) is

$\displaystyle \beta = \left\{\begin{array}{ll} 0, & A<13.26 \\ [5pt] 0.76609(A-13.26)^{0.4} + 0.09834(A-13.26), & 13.26< A < 60 \\ [5pt] 0.12438*(A+6.3), & 60<A<120, \\ \end{array} \right. \protect$ (5.12)

where now $ A$ is the desired side-lobe attenuation in dB (as opposed to stop-band attenuation). A plot showing Kaiser window side-lobe level for various values of $ \beta $ is given in Fig.3.28.

Similarly, the filter order $ M$ is estimated from stop-band attenuation $ A$ and desired transition width $ \Delta\omega$ using the empirical formula

$\displaystyle M = \frac{A-8}{2.285 \cdot \Delta\omega}$ (5.13)

where $ \Delta\omega$ is in radians between 0 and $ \pi$ .

Without the function fir1, we would have to manually implement the window method of filter design by (1) constructing the impulse response of the ideal bandpass filter $ h(n)$ (a cosine modulated sinc function), (2) computing the Kaiser window $ w(n)$ using the estimated length and $ \beta $ from above, then finally (3) windowing the ideal impulse response with the Kaiser window to obtain the FIR filter coefficients $ h_w(n) = w(n)h(n)$ . A manual design of this nature will be illustrated in the Hilbert transform example of §4.6.

Comparison to the Optimal Chebyshev FIR Bandpass Filter

To provide some perspective on the results, let's compare the window method to the optimal Chebyshev FIR filter4.10) for the same length and design specifications above.

The following Matlab code illustrates two different bandpass filter designs. The first (different transition bands) illustrates a problem we'll look at. The second (equal transition bands, commented out), avoids the problem.

M = 101;
normF = [0 0.3 0.4 0.6 0.8 1.0];  % transition bands different
%normF = [0 0.3 0.4 0.6 0.7 1.0]; % transition bands the same
amp = [0 0 1 1 0 0];              % desired amplitude in each band

[b2,err2] = firpm(M-1,normF,amp); % optimal filter of length M

Figure 4.7 shows the frequency response of the Chebyshev FIR filter designed by firpm, to be compared with the window-method FIR filter in Fig.4.6. Note that the upper transition band ``blows up''. This is a well known failure mode in FIR filter design using the Remez exchange algorithm [176,224]. It can be eliminated by narrowing the transition band, as shown in Fig.4.8. There is no error penalty in the transition region, so it is necessary that each one be ``sufficiently narrow'' to avoid this phenomenon.

Remember the rule of thumb that the narrowest transition-band possible for a length $ L$ FIR filter is on the order of $ 4\pi/L$ , because that's the width of the main-lobe of a length $ L$ rectangular window (measured between zero-crossings) (§3.1.2). Therefore, this value is quite exact for the transition-widths of FIR bandpass filters designed by the window method using the rectangular window (when the main-lobe fits entirely within the adjacent pass-band and stop-band). For a Hamming window, the window-method transition width would instead be $ 8\pi/L$ . Thus, we might expect an optimal Chebyshev design to provide transition widths in the vicinity of $ 8\pi/L$ , but probably not too close to $ 4\pi/L$ or below In the example above, where the sampling rate was $ 20$ kHz, and the filter length was $ L=101$ , we expect to be able to achieve transition bands circa $ (20,000/(2\pi))\cdot (8\pi/101) = 792$ Hz, but not so low as $ (20,000/(2\pi))\cdot (4\pi/101) = 396$ Hz. As we found above, $ 2000$ Hz was under-constrained, while $ 1000$ Hz was ok, being near the ``Hamming transition width.''

Figure 4.7: Amplitude response of the optimal Chebyshev FIR bandpass filter designed by the Remez exchange method.

Figure 4.8: Amplitude response of the optimal Chebyshev FIR bandpass filter as in Fig.4.7 with the upper transition band narrowed from 2 kHz down to 1 kHz in width.

Next Section:
Hilbert Transform Design Example
Previous Section:
Frequency Sampling Method for FIR Filter Design