3.14 FIR Filter Design
This section discusses filters that have impulse responses with finite duration.
3.14.1 A FIR filter does not have finite poles
Essentially, most FIR filters are non-recursive and have a transfer function that can be written as , i. e., the denominator is in Eq. (3.52). Given this denominator, all poles of FIR filters are located in or (while IIR filters do not have this restriction). For example, has a pole of order (or multiplicity) 2 at . You can check that with zplane([1 1 1],1) on Matlab/Octave34 or note it by rewriting as . Another example is the noncausal system , which has a pole of multiplicity 2 at infinity.35
3.14.2 The coefficients of a FIR coincide with its impulse response
An interesting property of FIR filters is that the coefficients of (also called “taps”) are exactly the non-zero values of the filter’s impulse response . For example, using the inverse Z-transform on , one obtains .
This property sometimes leads to confusing commands. For instance, the arguments Bz and Az to method freqz(Bz,Az) are polynomials in Z transform domain. But for FIR filters, it is possible to substitute Bz by the impulse response hn as follows:
1hn=[2, 1, 3]; % define some arbitrary FIR impulse response 2freqz(hn, 1); % find the DTFT corresponding to H(z)=B(z)/A(z)
Note in this code that for an FIR filter.
3.14.3 Algorithms for FIR filter design
There are several algorithms for both FIR and IIR. Instead of using pre-defined functions such as Butterworth’s, a FIR design is typically done with alternative methods, which seek to optimize a given criterion. The most common algorithm is the least-squares, which is implemented in Matlab/Octave via firls. Another algorithm is Parks-McClellan, implemented in Matlab’s firpm, but not available in Octave 3.2.3.
These and other optimization-based algorithms are executed according to a syntax that allows specifying an arbitrary function, not restricted to be lowpass, highpass, etc. Figure 3.47 illustrates the specification of a desired magnitude response of a differentiator, which has a gain in passband that is proportional to frequency. An arbitrary function will be specified in bands of interest. For each band , four parameters are necessary: the start and end frequencies, the desired function values (the magnitude in this context) and at the start and end frequencies. An optional parameter is the weight of each band (e. g., W1 and W2 in Figure 3.47). The larger the weight, the higher the importance given to that band in the design process.
In Matlab/Octave, the function fir2 implements another algorithm for designing a FIR with arbitrary frequency response magnitude, using a different syntax. Instead of working with bands, fir2 allows the specification of the desired magnitude at frequencies of interest.
An example of using least-squares for FIR design with firls is discussed in the sequel.
3.14.4 FIR design via least-squares
The least-squares method tries to minimize the total squared error between the desired function and the obtained approximation over the specified band. It is possible to give a different weight to each band. The larger the weight, the smaller the error in that band. A vector W with weights is an optional input parameter of firls.
For example, an ideal lowpass with cutoff rad can be specified with bands. The (normalized) frequencies could be and the amplitudes . These values are organized in vectors F=[0 0.5 0.5 1] and A=[1 1 0 0] and a -order FIR can be obtained with B=firls(100,F,A). Note that A and F have the same length, which must be (even number), and W must have length equal to if specified. The default is to weight all bands equally, which corresponds to W with all elements equal to 1.
Figure 3.48 compares the frequency responses of two filters obtained with firls. Figure 3.48(a) was obtained with Listing 3.27.
1f=[0 0.25 0.3 0.55 0.6 0.85 0.9 1]; %frequencies 2A=[1 1 0 0 1 1 0 0]; %amplitudes 3M=20; %filter order 4W=[1000 1 1 1]; %weights, first passband is prioritized 5B=firls(M,f,A,W); 6freqz(B);
The horizontal lines were superimposed to indicate the bands in which the gain should be 0 dB. Figure 3.48(b) was obtained with the same commands, but changing W to W=[1 1 1000 1]. Note that the impact of W is visible with the band that is not prioritized having larger deviations.
3.14.5 FIR design via windowing
The windowing method for FIR design is implemented in fir1. In terms of computational cost, the method is very simple: the FIR impulse response is obtained by multiplying the impulse response of an ideal filter (which has an infinite duration) by a finite-length sequence called window with non-zero samples. Recall that the coefficients of a FIR coincide with its . For the case of an ideal lowpass filter, the impulse response is given by
| (3.86) |
which is the inverse DTFT of a “rectangular pulse” in frequency-domain.
The impulse response for the ideal highpass filter can be obtained by noting that and converting this to time-domain leads to
| (3.87) |
The steps to obtain a FIR filter of order (assumed to be an even number here) via the windowing method are:
- f 1.
- Obtain the impulse response for the corresponding ideal filter: has an infinite duration but the next steps will require only of its coefficients.
- f 2.
- Choose a window: Pick a window with non-zero coefficients in the range .
- f 3.
- Get a filter with finite order
(i. e.,
coefficients): Multiply the chosen window
by the ideal filter
impulse response
to get a temporary
(3.88) - f 4.
- Make the filter causal: Delay
such that the first
coefficient is at :
(3.89)
The DTFT of , , and are , , and , respectively. Figure 3.49 illustrates the effect of multiplying a signal ( in this case) by a finite-duration window. This is a very useful approach for modeling the process of truncating an infinite-duration signal for further processing. This model allows to calculate, for instance, the spectrum of the finite-duration signal based on the convolution between the original and the window spectra. This is useful not only in FIR design, but many other situations such as segmenting a signal into frames.
In windows-based FIR design, the filter frequency response is the result of the (circular) convolution between and , as indicated in Figure 3.49. The ripples (ringing) observed in are located at the transitions of and indicate that the main lobe of dictates the transition band and the amplitude of sidelobes determine the attenuation at the stopband.
The properties of the filter are completely dependent on the adopted window type. The most common windows are named after their inventors: Hamming, Blackman, Kaiser and Hann (also called the hanning window). Among these four, the Kaiser is the only one with an extra degree of freedom provided by a parameter . The other three are completely specified by the order .
The rectangular window is useful to better understand the windowing process. The rectangular is 1 for samples and 0 otherwise. For example, a -th order lowpass filter with can be designed with a rectangular windows according to Listing 3.28.
1%% Low pass FIR design via windowing 2N=2; %the FIR order is M=2N in this case 3n=-N:N; %time indices 4wc = pi/3 %specified cutoff frequency 5h_ideal = sin(wc * n) ./ (pi * n) %ideal filter response 6h_ideal(N+1)=wc/pi %correct undetermined value 0/0 at n=0 7%% "Manually" design FIR with rectangular window 8% a rectangular window has values 1, no need to multiply 9hn = h_ideal/sum(h_ideal) %normalize to have gain 1 at DC 10%% "Automatically" design FIR using windowing via fir1 11B=fir1(2*N,wc/pi,rectwin(2*N+1)) %alternatively, use fir1 12max(abs(hn-B)) %compare hn with B obtained with fir1 13%% "Manually" design (another) FIR with Hamming window 14window_hamming = transpose(hamming(2*N+1)); %window 15hn = h_ideal .* window_hamming; %multiply in time-domain 16hn = hn/sum(hn) %normalize filter to have gain 1 at DC 17%% "Automatically" design FIR using windowing via fir1 18B=fir1(2*N,wc/pi,hamming(2*N+1)) %alternatively, use fir1 19max(abs(hn-B)) %compare hn with B obtained with fir1
There is support in Matlab/Octave for obtaining windows other than the rectangular. For example, the command w=window(@hamming,10) obtains a Hamming window with 10 non-zero samples, which can be used to obtain a FIR of order with cutoff frequency rad via the command B=fir1(9,0.5,w).
The adopted window determines the frequency response of the associated FIR designed via windowing. Section 4.3 discusses the characteristics of windows in more details.
One tricky feature of fir1 is that it generates filters with cutoff frequency that corresponds to a gain of dB (0.5 in linear scale). In contrast, the cutoff frequency of a filter obtained with butter and other IIR filter design functions corresponds to a gain of dB ( in linear scale). Another aspect is that, if the order is too small when using fir1, the gain at may not reach dB. This also happens with other filter design procedures.
3.14.6 Two important characteristics: FIRs are always stable and can have linear phase
Because practical FIR filters have all coefficients with finite values, Eq. (A.123) indicates that all FIR filters are BIBO stable. Besides, FIR filters can easily have linear phase, which is a desirable property in many applications (such as telecommunications, as will be discussed in Application 3.10). It can be proved that the linear phase is achieved if and only if there is symmetry in about some value . For example, a FIR of order has 5 non-zero samples in and exhibit symmetry about , which corresponds to , such that . The phase is linear over frequency because, for the given example:
where is a real function that provides the magnitude , such that the phase of is linear , given by the factor .
As Eq. (3.96) indicates, a symmetric FIR can always be decomposed as
where is a real function and is the group delay, to be further discussed along with Eq. (3.45).
3.14.7 Examples of linear and non-linear phase filters
When the system has linear phase it is relatively easy to calculate its group delay because it coincides with the slope of a straight line. Listing 3.29 uses the FIR from Eq. (3.96) to show an example of calculating the slope from two points k1 and k2, comparing it with the result of the grpdelay function:
1h=[1.5 -2 5 -2 1.5]; %coefficients of symmetric FIR 2N=8; %number of FFT points 3Fs=44100; %sampling frequency (Hz) 4H=fft(h,N); %calculate FFT of N points 5f=Fs/N*(0:N/2); %create abscissa in Hz, where Fs/N is the bin width 6p = unwrap(angle(H(1:N/2+1))); %calculate the phase in rad 7plot(f,p), xlabel('f (Hz)'), ylabel('Phase (rad)'), pause 8k1=2; k2=4; %choose two points of the line 9%Calculate derivative as the slope. Convert from Hz to rad/s first: 10groupDelay = -atan2(p(k2)-p(k1),2*pi*(f(k2)-f(k1))) %in seconds 11groupDelayInSamples=round(2*groupDelay*Fs)/2; %quantize with step=0.5 12grpdelay(h,1,f(1:N/2+1),Fs); %find delay for positive frequencies
Line 11 used round to avoid numerical errors. The trick of multiplying by 2 and after rounding divide by 2 allows to take into account that a symmetric FIR, unless the estimation was troubled due to the existence of zeros on the unit circle, has group delay that can be represented with a quantizer of step sample.
Figure 3.50 and Figure 3.51 were obtained by executing Listing 3.30 for the filters [0.3 -0.4 0.5 0.8 0.5 -0.4 0.3] and [0.3 -0.4 0.5 0.8 -0.2 0.1 0.5], respectively.
1function ak_plotGroupDelay(B,A) 2% function ak_plotGroupDelay(B,A) 3%Plots both the group delay and phase using plotyy 4gd = grpdelay(B,A,128); %estimate the group delay 5gd(1) = []; %avoid NaN at first sample 6[H,w] = freqz(B,A,128); %obtain DTFT at 128 points 7H(1) = []; w(1) = []; %keep the same size as gd vector 8phase = unwrap(angle(H)); %unwrap the phase for better visualization 9ax=plotyy(w,gd,w,phase); %use two distinct y axes 10ylabel(ax(1),'Group Delay (samples)'); ylabel(ax(2),'Phase (rad)'); 11xlabel('Frequency \Omega (rad)'); grid;
The filter of Figure 3.50 has a linear phase while the one in Figure 3.51 does not. It can be seen from Figure 3.51 that even causal systems can have a negative group delay for some frequencies.
3.14.8 Zeros close to the unit circle may impact the phase linearity
An often overlooked fact is that if approaches zero, the phase graph is not guaranteed to be visualized as a linear function of . For example, the first filter (from Eq. (3.96)) in the script below shows a linear phase via the freqz function, but the second does not due to a zero at approximately :
1h1=[1.5 -2 5 -2 1.5]; freqz(h1); pause %has linear phase 2h2=[-0.7 6 4 6 -0.7]; freqz(h2); %does not have linear phase 3abs(roots(h2)), angle(roots(h2));%h2 has zeros on unit circle!
The function grpdelay properly treats the zeros on the unit circle and indicates samples for both h1 and h2 filters. Because conventional filters such as lowpass and bandpass do not have zeros at the passband, the symmetry of their coefficients guarantees linear phase within the band of interest and the discussed behavior often manifest itself only in the stopband.
Another way of interpreting the problem caused by zeros on the unit circle is to note that they lead to a zero magnitude. If the magnitude of a complex number is zero or close enough to zero, its phase will not be important.
3.14.9 Four types of symmetric FIR filters
Depending on the symmetry, there are four types of linear-phase FIR filters. The filter of Eq. (3.96) is classified as a linear-phase FIR of type I. In general, a type I has an order that is even (the length of is odd) and is symmetric . Table 3.9 summarizes all types.
Type | Symmetry | Order () | (DC) | (Nyquist) |
I | Symmetric: | even | any value | any value |
II | Symmetric: | odd | any value | 0 |
III | Anti-symmetric: | even | 0 | 0 |
IV | Anti-symmetric: | odd | 0 | any value |
Recall that, because , the behavior of a FIR filter at DC can be obtained with
Hence, the anti-symmetric filters in Table 3.9 are restricted to have a zero at DC. Similarly, at the Nyquist frequency rad, , and for a type III FIR with order even:
where the first parcels in the summations correspond to even ( or ) and the second parcels to odd coefficients. This kind of relation is better visualized via examples as in Listing 3.31.
1hI=[1 2 3 2 1] %Type I FIR 2hII=[1 2 2 1] %Type II FIR 3hIII=[1 2 0 -2 -1] %Type III FIR 4hIV=[1 2 -2 -1] %Type IV FIR 5disp(['I: DC=' num2str(polyval(hI,1)) ... 6 ' Nyquist=' num2str(polyval(hI,-1))]) 7disp(['II: DC=' num2str(polyval(hII,1)) ... 8 ' Nyquist=' num2str(polyval(hII,-1))]) 9disp(['III: DC=' num2str(polyval(hIII,1)) ... 10 ' Nyquist=' num2str(polyval(hIII,-1))]) 11disp(['IV: DC=' num2str(polyval(hIV,1)) ... 12 ' Nyquist=' num2str(polyval(hIV,-1))])
Running Listing 3.31 outputs
I: DC=9 Nyquist=1 II: DC=6 Nyquist=0 III: DC=0 Nyquist=0 IV: DC=0 Nyquist=2
as expected from Table 3.9. Because the gain at DC is zero, linear-phase FIR filters of type III and IV are useful to operate as differentiator filters, for example.
Figure 3.52 shows the impulse and frequency responses for the four types of symmetric FIR filters exemplified in Listing 3.31. Note that the type II FIR in this case (hII=[1 2 2 1]) has three zeros on the unit circle (use abs(roots(hII))) and its phase has a discontinuity in spite of a constant group delay (use grpdelay(hII)), as warned in Section 3.14.8.