Presentation is loading. Please wait.

Presentation is loading. Please wait.

Practical Signal Processing Concepts and Algorithms using MATLAB

Similar presentations


Presentation on theme: "Practical Signal Processing Concepts and Algorithms using MATLAB"— Presentation transcript:

1 Practical Signal Processing Concepts and Algorithms using MATLAB
FIR Filter Design

2 Section Outline Section Outline FIR design methods Windowing
Standard band filters Arbitrary response filters Multiband filters Raised cosine filters Frequency domain filtering This section continues the application of LTI system analysis to filter design. FIR filters are treated in parallel to the discussion of IIR filters in the previous section, from specification to digital design. A variety of specialized FIR filter designs are also introduced. See “FIR Filter Types,” p. 6-7. Section Outline FIR design methods Windowing Standard band filters Arbitrary response filters Multiband filters Raised cosine filters Frequency domain filtering I II III IV I II III IV

3 FIR Filter Design FIR Filter Design
Digital filters with finite-duration impulse response (all-zero, or FIR filters) have both advantages and disadvantages when compared to infinite-duration impulse response (IIR) filters. FIR filters have the following primary advantages: They can have exactly linear phase. They are always stable, even when quantized. The design methods are generally linear. They can be realized efficiently in hardware. The filter startup transients have finite duration. The primary disadvantage of FIR filters is that they often require a much higher filter order than IIR filters to achieve a given level of performance. Correspondingly, the delay of these filters is often much greater than for an equal performance IIR filter. FIR Filter Design FIR filters have the following advantages: Exactly linear phase is possible Always stable, even when quantized Design methods are generally linear Efficient hardware realizations Startup transients have finite duration FIR filters have the following disadvantages: Higher filter order than IIR filters Corresponding greater delays

4 FIR Methods FIR Methods fir1, fir2, kaiserord firls, firpm, firpmord
The Signal Processing Toolbox supports a variety of methods for the design of FIR filters. Note: Previous to Version 6.2 of the Signal Processing Toolbox, the functions firpm, cfirpm, and firpmord (for Parks-McClellan) were named remez, cremez, and remezord (for Remez), respectively. FIR Methods Filter Method Description Filter Functions Windowing Apply window to truncated inverse Fourier transform of desired filter fir1, fir2, kaiserord Multiband with Transition Bands Equiripple or least squares approach over frequency subbands firls, firpm, firpmord Constrained Least Squares Minimize squared integral error over entire frequency range subject to maximum error constraints fircls, fircls1 Arbitrary Response Arbitrary responses, including nonlinear phase and complex filters cfirpm Raised Cosine Lowpass response with smooth, sinusoidal transition firrcos Filter Method Description Filter Functions Windowing Apply window to truncated inverse Fourier transform of desired filter fir1, fir2, kaiserord Multiband with Transition Bands Equiripple or least squares approach over frequency sub-bands firls, firpm, firpmord Constrained Least Squares Minimize squared integral error over entire frequency range subject to maximum error constraints fircls, fircls1 Arbitrary Response Arbitrary responses, including nonlinear phase and complex filters cfirpm Raised Cosine Lowpass response with smooth, sinusoidal transition firrcos

5 Multiband with Transition Bands
Method Description Functions Windowing Apply window to truncated inverse Fourier transform of desired "brick wall" filter fir1, fir2, kaiserord Multiband with Transition Bands Equiripple or least squares approach over sub-bands of the frequency range firls, remez, remezord Constrained Least Squares Minimize squared integral error over entire frequency range subject to maximum error constraints fircls, fircls1 Arbitrary Response Arbitrary responses, including nonlinear phase and complex filters cremez Raised Cosine Lowpass response with smooth, sinusoidal transition firrcos

6 Impulse Response Revisited
FIR filters are described by difference equations of the form where the filtered signal y(n) is just a linear combination of current and previous values of the input signal x(n). The coefficients b are the numerator coefficients of the transfer function. The denominator of the transfer function will always be a = 1 (and so can go unspecified). The order of the filter is n = length(b)–1. If the input signal is the unit impulse x = [ ], then the corresponding impulse response y(n) = h(n) is identical to b(n): FIR filter coefficients give the impulse response Impulse Response Revisited FIR filter: Try >> b = [ ]; >> stem(b) >> impz(b,1) >> b = [b0 b1 … bM]; a = 1; no need to specify stem(b) impz(b,1) FIR filter coefficients give the impulse response stem(b) impz(b,1)

7 Linear Phase Filters Linear Phase Filters –φ(ω)/ω –dφ(ω)/dω
A filter whose impulse response is symmetric about its midpoint is called a (generalized) linear phase filter. For such filters, The DFT of the impulse response will be either pure real or pure imaginary. The magnitude of the DFT is scaled by the filter’s magnitude response (there is no amplitude distortion). The phase shift φ of a filtered signal will vary linearly with frequency ω (pure time delay with no phase distortion). The phase delay –φ(ω)/ω and group delay –dφ(ω)/dω will be equal and constant. For an order n linear phase FIR filter, the phase delay and group delay is n/2. The absence of either amplitude distortion or phase distortion preserves the waveform of signals in the passband. Linear Phase Filters Impulse response is symmetric about its midpoint: Try >> a = 1; >> b = fir1(5,0.5); >> fvtool(b,a) Look at the phase delay and the group delay. –φ(ω)/ω –dφ(ω)/dω constant phase delay constant group delay no phase distortion Except for cfirpm, all the FIR filter design functions in the Signal Processing Toolbox design linear phase filters only. Except for cfirpm, all of the FIR filter design functions in the Signal Processing Toolbox design linear phase filters only. –φ(ω)/ω –dφ(ω)/dω

8 FIR Filter Types FIR Filter Types
The symmetric impulse response of a linear phase filter can have an odd or an even number of points, and can have an odd or even symmetry about the midpoint, leading to four filter types: Depending on the filter type, certain restrictions apply: The functions fir1, fir2, firls, firpm, fircls, fircls1, and firrcos all design type I and II linear phase FIR filters by default. Both firls and firpm design type III and IV linear phase FIR filters given a 'hilbert' or 'differentiator' flag. cfirpm can design any type of linear or nonlinear phase filter. Because the frequency response of a type II filter is zero at the Nyquist frequency (“high” frequency), fir1 does not design type II highpass and bandstop filters. For odd-valued n in these cases, fir1 adds 1 to the order and returns a type I filter. FIR Filter Types odd length, odd symmetry even length, odd symmetry I II III IV I II III IV odd length, even symmetry even length, even symmetry Odd length, even symmetry Even length, even symmetry Odd length, odd symmetry Even length, odd symmetry No highpass or bandstop filters length of impulse response - 1 Filter Type Filter Order Symmetry Response H(0) Response H(1) (Nyquist) Type I Even No restriction Type II Odd H(1) = 0 Type III H(0) = 0 Type IV Filter Type Filter Order Symmetry Response H(0) Response H(1) (Nyquist) Type I Even No restriction Type II Odd H(1) = 0 Type III H(0) = 0 Type IV fir1,fir2,firls,firpm,fircls,fircls1,firrcos

9 FIR filters have the following primary advantages:
Digital filters with finite-duration impulse response (all-zero, or FIR filters) have both advantages and disadvantages compared to infinite-duration impulse response (IIR) filters. FIR filters have the following primary advantages: They can have exactly linear phase. They are always stable. The design methods are generally linear. They can be realized efficiently in hardware. The filter startup transients have finite duration. The primary disadvantage of FIR filters is that they often require a much higher filter order than IIR filters to achieve a given level of performance. Correspondingly, the delay of these filters is often much greater than for an equal performance IIR filter.

10 Example 1 Solution Example 2:
Design a 48th-order FIR bandpass filter with passband 0.35 0.65: Solution >>b = fir1(48,[ ]); >>freqz(b,1,512) Example 2: Design a lowpass filter with the following specifications using the optimal design method : >>rp = 0.01; % Passband ripple >>rs = 0.1; % Stopband ripple >>fs = 8000; % Sampling frequency >>f = [ ]; % Cutoff frequencies >>a = [1 0]; % Desired amplitudes

11 Solution >>[n,fo,ao,w] = remezord(f,a,dev,fs); >> dev=[ ] >>[n,fo,ao,w]=remezord([ ],[1 0],dev,8000); % approximate order, normalized frequency band edges, frequency band amplitudes, and weights that meet input specifications f, a, and dev.% >>b=remez(n,fo,ao,w); %use n, fo, ao and w to design the filter b which approximately meets the specifications given by remezord input parameters f, a, and dev.% >>freqz(b,1,1024,8000); >>title('Lowpass Filter Designed to Specifications');

12 Window-Based Design Window-Based Design H(f) h(n) ifft h.*w hw(n)
Windowing is a common design method for FIR filters. In this method, The ideal frequency response H(F) is sampled. The corresponding ideal impulse response h(n) is determined by the inverse Fourier transform. In general, this response cannot be implemented in a digital filter because it is infinite and noncausal. h(n) is symmetrically truncated (multipled by a finite, symmetric window) to create a linear phase finite impulse response. The approximation to the ideal filter is “best” in a mean square sense, compared to other approximations of the same length, by Parseval’s theorem. However, the abrupt truncation leads to overshoot (Gibbs phenomenon) and ripples in the spectrum. The undesirable effects of truncation are reduced or eliminated by the use of tapered windows. Windowing does not explicitly impose amplitude response constraints, such as passband ripple or stopband attenuation. It must be used iteratively to produce designs that meet such specifications. Window-Based Design H(f) h(n) Try >> edit windemo >> windemo(20) >> windemo(50) >> windemo(100) ifft h.*w hw(n) HW(f) fft H(f) h(n) ifft h.*w hw(n) HW(f) fft

13 W = gausswin(2*length(y),3); w = W(length(y)+1:end);
Windowing Functions The Signal Processing Toolbox supports a variety of windows commonly used in FIR filter design. Typing >> help window provides a list of available functions: bartlett Bartlett window barthannwin Modified Bartlett-Hanning window blackman Blackman window blackmanharris Minimum four-term Blackman-Harris window bohmanwin Bohman window chebwin Chebyshev window flattopwin Flat top window gausswin Gaussian window hamming Hamming window hann Hann window kaiser Kaiser window nuttallwin Nuttall defined minimum four-term Blackman-Harris window parsenwin Parsen (de la Valle-Poussin) window rectwin Rectangular window triang Triangular window tukeywin Tukey window Individual functions take inputs for a window length n and window parameters and return the window w in a column vector of length n. (Note: Use w' for array products with row vector impulse responses.) The window function serves as a gateway to the individual functions. >> w = gausswin(64,alpha) and >> w = both return a Gaussian window of length 64 with standard deviation equal to 1/alpha. Windowing Functions bartlett, barthannwin, blackman, blackmanharris, bohmanwin, chebwin, flattopwin, gausswin, hamming, hann, kaiser, parsenwin, rectwin, triang, tukeywin Try >> help window >> n = 15; >> w = gausswin(n,3); >> stem(-7:7,w) >> n = ... linspace(0,6*pi,100); >> y = sinc(n); >> stem(n,y) >> figure >> W = gausswin(... 2*length(y),3); >> w = ... W(length(y)+1:end); >> wy = w'.*y; >> stem(n,wy,'m') .* = y = sin(n); W = gausswin(2*length(y),3); w = W(length(y)+1:end); wy = w'.*y;

14 Truncated, Windowed Signal
Windowing and Spectra When a signal is truncated, high-frequency components are introduced that are visible in the DFT. By windowing the truncated signal in the time domain, endpoints are assigned a reduced weight. The effect on the DFT is to reduce the height of the side lobes, but increase the width of the main lobe. Truncated signal and DFT: Truncated, windowed signal and DFT: Windowing and Spectra Truncated Signal and DFT: Truncated, Windowed Signal and DFT Try >> edit windft >> windft Time domain Frequency domain wider main lobe >> windft lower side lobes Time domain Frequency domain

15 Window Visualization Tool
The transition bandwidth of a window-based FIR filter is determined by the width of the main lobe of the DFT of the window function, adjustable by changing the filter order. Passband and stopband ripples are determined by the magnitude of the side lobe of the DFT of the window function, and are usually not adjustable by changing the filter order. The actual approximation error is scaled by the amount of the passband magnitude response. Ideally, the spectrum of a window should approximate an impulse. The main lobe should be as narrow as possible and the side lobes should contain as little energy as possible. The Window Visualization Tool (WVTool) allows you to investigate the tradeoffs among different windows and filter orders. >> wvtool(windowname(n)) opens WVTool with time and frequency domain plots of the n-length window specified in windowname, which can be any window in the Signal Processing Toolbox. Several windows can be given as input arguments for comparative display. Window Visualization Tool >> wvtool(kaiser(32,1),kaiser(32,5),kaiser(32,10)) Try >> wvtool(... hamming(32), ... kaiser(32,2.5), ... flattopwin(32)) kaiser(32,1), ... kaiser(32,5), ... kaiser(32,10))

16 Window Design and Analysis Tool
The Window Design and Analysis Tool (WinTool) is used in conjunction with the Window Visualization Tool. Use WVTool for displaying and comparing existing windows created in the MATLAB workspace. Use WinTool to interactively design windows with certain specifications and export them to the MATLAB workspace. >> wintool opens WinTool with a default 64-point Hamming window. Most window types satisfy some optimality criterion. Some windows are combinations of simpler windows. For example, the Hann window is the sum of a rectangular and a cosine window, and the Bartlett window is the convolution of two rectangular windows. Other windows emphasize certain desirable features. The Hann window improves high-frequency decay (at the expense of larger peaks in the side lobes). The Hamming window minimizes side lobe peaks (at the expense of slower high-frequency decay). The Kaiser window has a parameter that can be tuned to control side lobe levels. Other windows are based on simple mathematical formulas for easy application. The Hann window is easy to use as a convolution in the frequency domain. An optimal time-limited window maximizes energy in its spectrum over a given frequency band. In the discrete domain, the Kaiser window gives the best approximation to such an optimal window. Window Design and Analysis Tool >> wintool Try >> wintool Experiment with different window designs and export to the Workspace.

17 Example: Lowpass Filter
Consider the ideal, or “brick wall,” digital lowpass filter with a cutoff frequency of ω0 rad/s. This filter has magnitude 1 at all frequencies with magnitude less than ω0, and magnitude 0 at frequencies with magnitude between ω0 and π. Its impulse response sequence h(n) is This filter is not implementable since its impulse response is infinite and noncausal. To create a finite-duration impulse response, truncate it by applying a window. Retain the central section of impulse response in the truncation to obtain a linear phase FIR filter. For example, a length 51 filter with a lowpass cutoff frequency ω0 of 0.4π rad/s is >> b = 0.4*sinc(0.4*(-25:25)); The window applied here is a simple rectangular window. By Parseval's theorem, this is the length 51 filter that best approximates the ideal lowpass filter, in the integrated least squares sense. To display the filter’s frequency response in FVTool, type >> fvtool(b,1) Ringing and ripples occur in the response, especially near the band edge. This “Gibbs effect” does not vanish as the filter length increases, but a nonrectangular window reduces its magnitude. Multiplication by a window in the time domain causes a convolution or smoothing in the frequency domain. Apply a length 51 Hamming window to the filter and display the result using FVTool: >> bw = b.*hamming(51)'; Using a Hamming window greatly reduces the ringing. This is at the expense of transition width (the windowed version takes longer to ramp from passband to stopband) and optimality (the windowed version does not minimize the integrated squared error). Example: Lowpass Filter Try >> b = ... 0.4*sinc(... 0.4*(-25:25)); >> fvtool(b,1) >> bw = ... b.*hamming(51)'; >> fvtool(bw,1) Right-click the y-axis label in FVTool and choose Magnitude squared. >> b = ... 0.4*sinc(... 0.4*(-25:25)); >> fvtool(b,1) >> bw = ... b.*hamming(51)'; >> fvtool(bw,1) Right click on the y-axis label in FVTool and choose Magnitude squared

18 Standard Band FIR Design
The Signal Processing Toolbox functions fir1 and fir2 are both based on the windowing method. Given a filter order and a description of an ideal filter, these functions return a windowed inverse Fourier transform of the ideal filter. Both use Hamming windows by default, but they accept any windowing function. fir1 resembles the IIR filter design functions in that it is formulated to design filters in standard band configurations: lowpass, bandpass, highpass, and bandstop. The commands >> n = 50; >> Wn = 0.4; >> b = fir1(n,Wn); create a 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. For a highpass filter, simply append the string 'high' to the function’s parameter list. For a bandpass or bandstop filter, specify Wn as a two-element vector containing the passband edge frequencies; append the string 'stop' for the bandstop configuration. >> b = fir1(n,Wn,window); uses the window specified in column vector window for the design. The vector window must be n+1 elements long. If you do not specify a window, fir1 applies a Hamming window. The kaiserord function estimates the filter order, cutoff frequency, and Kaiser window β parameter needed to meet a given set of specifications. Given a vector of frequency band edges, a vector of magnitudes, and a maximum allowable ripple, kaiserord returns appropriate input parameters for the fir1 function. Standard Band FIR Design >> b = fir1(n,Wn,'stop',kaiser(n+1,beta)); Try >> edit fir1demo >> fir1demo Exercise Noise Filter II (p. AIII-11)

19 Arbitrary Response FIR Filters
The fir2 function also designs windowed FIR filters, but with an arbitrarily shaped piecewise linear frequency response. (The IIR counterpart of this function is yulewalk.) >> b = fir2(n,f,m); returns row vector b containing the n+1 coefficients of an order n FIR filter. The frequency-magnitude characteristics of this filter match those given by vectors f and m. >> b = fir2(n,f,m,window); uses the window specified in column vector window for the design. The vector window must be n+1 elements long. If you do not specify a window, fir2 applies a Hamming window. The function cfirpm is used to design complex and nonlinear-phase equiripple FIR filters. It allows arbitrary frequency-domain constraints. Arbitrary Response FIR Filters >> b = fir2(n,f,m,window); Try >> edit directstop2 >> directstop2(10) >> directstop2(100) >> directstop2(500) >> edit nlinphase >> nlinphase

20 Multiband Filters Multiband Filters >> b = firls(n,f,a)
The function firls designs linear-phase FIR filters that minimize the weighted, integrated squared error between an ideal piecewise linear function and the magnitude response of the filter over a set of desired frequency bands. >> b = firls(n,f,a) returns row vector b containing the n+1 coefficients of the order n FIR filter whose frequency-amplitude characteristics approximately match those given by vectors f and a. The function fircls allows you to introduce constraints by defining upper and lower bounds for the frequency response in each band. The function fircls1 is used specifically to design lowpass and highpass linear phase FIR filters using constrained least squares. Multiband Filters >> b = firls(n,f,a) Try >> edit firlsdemo >> firlsdemo >> edit firclsdemo >> firclsdemo

21 Raised Cosine Filters Raised Cosine Filters
The sinc function, which is the impulse response of an ideal lowpass filter, forms the basis for several other interpolating functions of the form One commonly used form is the raised cosine function: R is called the rolloff factor. Like the sinc function, the raised cosine function is 1 at n = 0 and 0 at all other sampling instances n = ns. In contrast to the sinc function, the raised cosine has faster decaying oscillations on either side of the origin for R > 0. This results in improved reconstruction if samples are not acquired at exactly the sampling instants (i.e., if there is jitter). It also uses fewer past and future values in the reconstruction, as compared to the sinc function. The shape of the function’s spectrum is the “raised cosine.” The ideal raised cosine lowpass filter frequency response consists of unity gain at low frequencies, a raised cosine function in the middle, and total attenuation at high frequencies. The width of the transition band is determined by the rolloff factor. >> b = firrcos(n,F0,df,fs) >> b = firrcos(n,F0,df,fs,'bandwidth') are equivalent, and return an order n lowpass linear-phase FIR filter with a raised cosine transition band. The cutoff frequency is F0, the transition bandwidth df, and sampling frequency is fs, all in hertz. df must be small enough so that F0 ± df/2 is between 0 and fs/2. b is normalized so that the nominal passband gain is always equal to 1. >> b = firrcos(n,F0,r,fs,'rolloff') interprets the third argument, r, as the rolloff factor instead of the transition bandwidth, df. r must be in the range [0,1]. Raised Cosine Filters >> b = firrcos(n,F0,df,fs); Try >> b = firrcos(... 20,0.25,0.25,2); >> freqz(b)

22 Frequency Domain Filtering
Often, a long (perhaps continuous) stream of data must be processed by a system with only a finite length buffer for storage. The data must be processed in pieces, and the processed result constructed from the processed pieces. In the overlap-add method, an input signal x(n) is partitioned into equal length data blocks. The filter coefficients (impulse response) and each block of data are transformed to the frequency domain using the FFT, where they can be efficiently convolved using multiplication. The partial convolutions of the signal are returned to the time domain with the IFFT, where they are shifted and summed using superposition. fftfilt implements the overlap-add method for FIR filters. >> y = fftfilt(b,x) chooses the FFT length and block length for efficiency, depending on the lengths of the filter and signal. >> y = fftfilt(b,x,n) uses an FFT length of nfft = 2^nextpow2(n) and a data block length of nfft-length(b)+1 (ensures circular convolution). fftfilt incurs an “offline” startup cost when converting the coefficients b to the frequency domain. After that, the number of multiplications fftfilt performs relative to filter (which implements the filter in direct form) is ~log2(L)/N, where L is the block length and N is the filter length. (Multiplications are a good measure of performance, since they are typically expensive on hardware.) The net result is that for filters of high order, fftfilt outperforms filter. Frequency Domain Filtering finite buffer Try >> x = [ ]; >> h = [1 1 1]; >> y = conv(h,x) >> x1 = [1 2 3]; >> x2 = [4 5 6]; >> y1 = conv(h,x1) >> y2 = conv(h,x2) >> Y1 = ... [y1, zeros(1,3)]; >> Y2 = ... [zeros(1,3), y2]; >> Y = Y1 + Y2 The following script takes a few moments to run. >> edit filttimes >> filttimes long x(n) y(n) b Overlap-add method (fftfilt): Partion x(n) into data blocks FFT to the frequency domain Convolve blocks efficiently with b using multiplication IFFT back to the time domain Assemble shifted partial convolutions using superposition

23 Section Summary Section Summary FIR design methods Windowing
Standard band filters Arbitrary response filters Multiband filters Raised cosine filters Frequency domain filtering Section Summary FIR design methods Windowing Standard band filters Arbitrary response filters Multiband filters Raised cosine filters Frequency domain filtering I II III IV I II III IV


Download ppt "Practical Signal Processing Concepts and Algorithms using MATLAB"

Similar presentations


Ads by Google