FIR Filter Design - MATLAB & Simulink (2024)

FIR Filter Design

FIR vs. IIR Filters

Digital filters with finite-duration impulse response (all-zero,or FIR filters) have both advantages and disadvantages compared toinfinite-duration impulse response (IIR) filters.

FIR filters have the following primary advantages:

  • They can have exactly linearphase.

  • 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 requirea much higher filter order than IIR filters to achieve a given levelof performance. Correspondingly, the delay of these filters is oftenmuch greater than for an equal performance IIR filter.

FIR Filter Summary

FIR Filters

Filter Design Method

Description

Filter Functions

Windowing

Apply window to truncated inverse Fourier transform of specified "brick wall" filter

fir1, fir2, kaiserord

Multiband with Transition Bands

Equiripple or least squares approach over sub-bands ofthe frequency range

firls, firpm, firpmord

Constrained Least Squares

Minimize squared integral error over entire frequencyrange subject to maximum error constraints

fircls, fircls1

Arbitrary Response

Arbitrary responses, including nonlinear phase and complexfilters

cfirpm

Raised Cosine

Lowpass response with smooth, sinusoidal transition

rcosdesign

Linear Phase Filters

Except for cfirpm, allof the FIR filter design functions design linear phase filters only.The filter coefficients,or “taps,” of such filters obey either an even or oddsymmetry relation. Depending on this symmetry, and on whether theorder n of the filter is even or odd, a linearphase filter (stored in length n+1 vectorb)has certain inherent restrictions on its frequencyresponse.

Linear Phase Filter TypeFilter Order Symmetry of CoefficientsResponse H(f), f=0ResponseH(f), f=1 (Nyquist)

Type I

Even

even:

b(k)=b(n+2k),k=1,...,n+1

No restriction

No restriction

Type II

Odd

even:

b(k)=b(n+2k),k=1,...,n+1

No restriction

H(1)=0

Type III

Even

odd:

b(k)=b(n+2k),k=1,...,n+1

H(0)=0

H(1)=0

Type IV

Odd

odd:

b(k)=b(n+2k),k=1,...,n+1

H(0)=0

No restriction

The phase delay and groupdelay of linear phase FIR filters are equal and constant over thefrequency band. For an order n linear phase FIRfilter, the group delay is n/2, and the filteredsignal is simply delayed by n/2 time steps (andthe magnitude of its Fourier transform is scaled by the filter's magnituderesponse). This property preserves the wave shape of signals in thepassband; that is, there is no phase distortion.

The functions fir1, fir2, firls, firpm, fircls,and fircls1 all design typeI and II linear phase FIR filters by default. rcosdesign designsonly type I filters. Both firls and firpm designtype III and IV linear phase FIR filters given a 'hilbert' or 'differentiator' flag. cfirpm can design any type of linearphase filter, and nonlinear phase filters as well.

Note

Because the frequency response of a type II filter is zero atthe Nyquist frequency (“high” frequency), fir1 doesnot design type II highpass and bandstop filters. For odd-valued n inthese cases, fir1 adds 1 to the order and returnsa type I filter.

Windowing Method

Consider the ideal, or “brick wall,” digital lowpassfilter with a cutoff frequency of ω0 rad/s.This filter has magnitude1 at all frequencies withmagnitude less than ω0, and magnitude0 at frequencies with magnitude between ω0 andπ. Its impulse response sequence h(n)is

h(n)=12πππH(ω)ejωndω=12πω0ω0ejωndω=sinω0nπn

This filter is not implementable since its impulse responseis infinite and noncausal. To create a finite-duration impulse response,truncate it by applying a window. By retaining the central sectionof impulse response in this truncation, you obtain a linear phaseFIR filter. For example, a length51 filter witha lowpass cutoff frequency ω0 of0.4 π rad/s is

b = 0.4*sinc(0.4*(-25:25));

The window applied here is a simple rectangular window. By Parseval’stheorem, this is the length 51 filter that best approximates the ideallowpass filter, in the integrated least squares sense. The followingcommand displays the filter's frequency response in FVTool:

fvtool(b,1)

Note that the y-axisshown in the figure below is in Magnitude Squared. You can set thisby right-clicking on the axis label and selecting MagnitudeSquared from the menu.

FIR Filter Design- MATLAB & Simulink (1)

Ringing and ripples occur in the response, especially near theband edge. This “Gibbs effect” does not vanish as thefilter length increases, but a nonrectangular window reduces its magnitude.Multiplication by a window in the time domain causes a convolutionor smoothing in the frequency domain. Apply a length 51 Hamming windowto the filter and display the result using FVTool:

b = 0.4*sinc(0.4*(-25:25));b = b.*hamming(51)';fvtool(b,1)

Note that the y-axisshown in the figure below is in Magnitude Squared. You can set thisby right-clicking on the axis label and selecting MagnitudeSquared from the menu.

FIR Filter Design- MATLAB & Simulink (2)

Using a Hamming window greatly reduces the ringing. This improvement 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).

Standard Band FIR Filter Design: fir1

fir1 uses a least-squares approximation to compute filter coefficients and then smooths the impulse response with a window. For an overview of windows and their properties, see Windows. 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 statements

n = 50;Wn = 0.4;b = fir1(n,Wn);

create row vectorb containingthe coefficients of the ordern Hamming-windowedfilter. This is a lowpass, linear phase FIR filter with cutoff frequency Wn. Wn isa number between 0 and 1, where 1 corresponds to the Nyquist frequency,half the sampling frequency. (Unlike other methods, here Wn correspondsto the 6 dB point.) For a highpass filter, simply append 'high' tothe function's parameter list. For a bandpass or bandstop filter,specify Wn as a two-element vector containing thepassband edge frequencies. Append 'stop' for thebandstop configuration.

b=fir1(n,Wn,window) usesthe window specified in column vector window forthe design. The vector window must be n+1 elementslong. If you do not specify a window, fir1 appliesa Hamming window.

Kaiser Window Order Estimation.The kaiserord functionestimates the filter order, cutoff frequency, and Kaiser window betaparameter needed to meet a given set of specifications. Given a vectorof frequency band edges and a corresponding vector of magnitudes,as well as maximum allowable ripple, kaiserord returnsappropriate input parameters for the fir1 function.

Multiband FIR Filter Design: fir2

The fir2 function alsodesigns windowed FIR filters, but with an arbitrarily shaped piecewiselinear frequency response. This is in contrast to fir1, which only designs filters in standardlowpass, highpass, bandpass, and bandstop configurations.

The commands

n = 50;f = [0 .4 .5 1];m = [1 1 0 0];b = fir2(n,f,m);

return row vector b containing the n+1 coefficients of the order n FIR filter whose frequency-magnitude characteristics match those given by vectors f and m. f is a vector of frequency points ranging from 0 to 1, where 1 represents the Nyquist frequency. m is a vector containing the specified magnitude response at the points specified in f. (The IIR counterpart of this function is yulewalk, which also designs filters based on arbitrary piecewise linear magnitude responses. See IIR Filter Design for details.)

Multiband FIR Filter Design with Transition Bands

The firls and firpm functions provide a more general means of specifying the ideal specified filter than the fir1 and fir2 functions. These functions design Hilbert transformers, differentiators, and other filters with odd symmetric coefficients (type III and type IV linear phase). They also let you include transition or “don't care” regions in which the error is not minimized, and perform band dependent weighting of the minimization.

The firls function is an extension of the fir1 and fir2 functions in that it minimizes the integral of the square of the error between the specified frequency response and the actual frequency response.

The firpm function implements the Parks-McClellan algorithm, which uses the Remez exchange algorithm and Chebyshev approximation theory to design filters with optimal fits between the specified and actual frequency responses. The filters are optimal in the sense that they minimize the maximum error between the specified frequency response and the actual frequency response; they are sometimes called minimax filters. Filters designed in this way exhibit an equiripple behavior in their frequency response, and hence are also known as equiripple filters. The Parks-McClellan FIR filter design algorithm is perhaps the most popular and widely used FIR filter design methodology.

The syntax for firls and firpm isthe same; the only difference is their minimization schemes. The nextexample shows how filters designed with firls and firpm reflectthese different schemes.

Basic Configurations

The default mode of operation of firls and firpm is to design type I or type II linear phase filters, depending on whether the order you want is even or odd, respectively. A lowpass example with approximate amplitude1 from 0 to 0.4Hz, and approximate amplitude0 from 0.5 to 1.0Hz is

n = 20; % Filter orderf = [0 0.4 0.5 1]; % Frequency band edgesa = [1 1 0 0]; % Amplitudesb = firpm(n,f,a);

From 0.4 to 0.5 Hz, firpm performs no errorminimization; this is a transition band or “don't care”region. A transition band minimizes the error more in the bands thatyou do care about, at the expense of a slower transition rate. Inthis way, these types of filters have an inherent trade-off similarto FIR design by windowing.

To compare least squares to equiripple filter design, use firls tocreate a similar filter. Type

bb = firls(n,f,a);

and compare their frequency responses using FVTool:

fvtool(b,1,bb,1)

Note that the y-axisshown in the figure below is in Magnitude Squared. You can set thisby right-clicking on the axis label and selecting MagnitudeSquared from the menu.

FIR Filter Design- MATLAB & Simulink (3)

The filter designed with firpm exhibits equiripplebehavior. Also note that the firls filter has abetter response over most of the passband and stopband, but at theband edges (f=0.4 and f=0.5), the responseis further away from the ideal than the firpm filter.This shows that the firpm filter's maximum errorover the passband and stopband is smaller and, in fact, it is thesmallest possible for this band edge configuration and filter length.

Think of frequency bands as lines over short frequency intervals. firpm and firls use this scheme to represent any piecewise linear frequency-response function with any transition bands. firls and firpm design lowpass, highpass, bandpass, and bandstop filters; a bandpass example is

f = [0 0.3 0.4 0.7 0.8 1]; % Band edges in pairsa = [0 0 1 1 0 0]; % Bandpass filter amplitude

Technically, these f and a vectorsdefine five bands:

  • Two stopbands, from 0.0 to 0.3 and from 0.8 to 1.0

  • A passband from 0.4 to 0.7

  • Two transition bands, from 0.3 to 0.4 and from 0.7to 0.8

Example highpass and bandstop filters are

f = [0 0.7 0.8 1]; % Band edges in pairsa = [0 0 1 1]; % Highpass filter amplitudef = [0 0.3 0.4 0.5 0.8 1]; % Band edges in pairsa = [1 1 0 0 1 1]; % Bandstop filter amplitude

An example multiband bandpass filter is

f = [0 0.1 0.15 0.25 0.3 0.4 0.45 0.55 0.6 0.7 0.75 0.85 0.9 1];a = [1 1 0 0 1 1 0 0 1 1 0 0 1 1];

Another possibility is a filter that has as a transition regionthe line connecting the passband with the stopband; this can helpcontrol “runaway” magnitude response in wide transitionregions:

f = [0 0.4 0.42 0.48 0.5 1];a = [1 1 0.8 0.2 0 0]; % Passband, linear transition,  % stopband

The Weight Vector

Both firls and firpm allowyou to place more or less emphasis on minimizing the error in certainfrequency bands relative to others. To do this, specify a weight vectorfollowing the frequency and amplitude vectors. An example lowpassequiripple filter with 10 times less ripple in the stopband than thepassband is

n = 20; % Filter orderf = [0 0.4 0.5 1]; % Frequency band edgesa = [1 1 0 0]; % Amplitudesw = [1 10]; % Weight vectorb = firpm(n,f,a,w);

A legal weight vector is always half the length of the f and a vectors;there must be exactly one weight per band.

Anti-Symmetric Filters / Hilbert Transformers

When called with a trailing 'h' or 'Hilbert' option, firpm and firls designFIR filters with odd symmetry, that is, type III (for even order)or type IV (for odd order) linear phase filters. An ideal Hilberttransformer has this anti-symmetry property and an amplitude of 1across the entire frequency range. Try the following approximate Hilberttransformers and plot them using FVTool:

b = firpm(21,[0.05 1],[1 1],'h'); % Highpass Hilbertbb = firpm(20,[0.05 0.95],[1 1],'h'); % Bandpass Hilbertfvtool(b,1,bb,1)

FIR Filter Design- MATLAB & Simulink (4)

You can find the delayed Hilbert transform of a signal x bypassing it through these filters.

fs = 1000; % Sampling frequencyt = (0:1/fs:2)'; % Two second time vectorx = sin(2*pi*300*t); % 300 Hz sine wave example signalxh = filter(bb,1,x); % Hilbert transform of x

The analytic signal corresponding to x isthe complex signal that has x as its real partand the Hilbert transform of x as its imaginarypart. For this FIR method (an alternative to the hilbert function),you must delay x by half the filter order to createthe analytic signal:

xd = [zeros(10,1); x(1:length(x)-10)]; % Delay 10 samplesxa = xd + j*xh; % Analytic signal

This method does not work directly for filters of odd order,which require a noninteger delay. In this case, the hilbert function,described in Hilbert Transform, estimates the analytic signal. Alternatively,use the resample function to delay the signal bya noninteger number of samples.

Differentiators

Differentiation of a signal in the time domain is equivalentto multiplication of the signal's Fourier transform by an imaginaryramp function. That is, to differentiate a signal, pass it througha filter that has a response H(ω)=jω.Approximate the ideal differentiator (with a delay) using firpm or firls witha 'd' or 'differentiator' option:

b = firpm(21,[0 1],[0 pi],'d');

For a type III filter, the differentiation band should stopshort of the Nyquist frequency, and the amplitude vector must reflectthat change to ensure the correct slope:

bb = firpm(20,[0 0.9],[0 0.9*pi],'d');

In the 'd' mode, firpm weightsthe error by 1/ω in nonzero amplitude bands to minimize themaximum relative error. firls weightsthe error by (1/ω)2 in nonzero amplitudebands in the 'd' mode.

The following plots show the magnitude responsesfor the differentiators above.

fvtool(b,1,bb,1)legend('Odd order','Even order','Location','best')

FIR Filter Design- MATLAB & Simulink (5)

Constrained Least Squares FIR Filter Design

The Constrained Least Squares (CLS) FIR filter design functionsimplement a technique that enables you to design FIR filters withoutexplicitly defining the transition bands for the magnitude response.The ability to omit the specification of transition bands is usefulin several situations. For example, it may not be clear where a rigidlydefined transition band should appear if noise and signal informationappear together in the same frequency band. Similarly, it may makesense to omit the specification of transition bands if they appearonly to control the results of Gibbs phenomena that appear in thefilter's response. See Selesnick, Lang, and Burrus[2] for discussionof this method.

Instead of defining passbands, stopbands, and transition regions, the CLS method accepts a cutoff frequency (for the highpass, lowpass, bandpass, or bandstop cases), or passband and stopband edges (for multiband cases), for the response you specify. In this way, the CLS method defines transition regions implicitly, rather than explicitly.

The key feature of the CLS method is that it enables you to define upper and lower thresholds that contain the maximum allowable ripple in the magnitude response. Given this constraint, the technique applies the least square error minimization technique over the frequency range of the filter's response, instead of over specific bands. The error minimization includes any areas of discontinuity in the ideal, "brick wall" response. An additional benefit is that the technique enables you to specify arbitrarily small peaks resulting from the Gibbs phenomenon.

There are two toolbox functions that implement this design technique.

Description

Function

Constrained least square multiband FIR filter design

fircls

Constrained least square filter design for lowpass andhighpass linear phase filters

fircls1

For details on the calling syntax for these functions, see theirreference descriptions in the Function Reference.

Basic Lowpass and Highpass CLS Filter Design

The most basic of the CLS design functions, fircls1,uses this technique to design lowpass and highpass FIR filters. Asan example, consider designing a filter with order 61 impulse responseand cutoff frequency of 0.3 (normalized). Further, define the upperand lower bounds that constrain the design process as:

  • Maximum passband deviation from 1 (passband ripple)of 0.02.

  • Maximum stopband deviation from 0 (stopband ripple)of 0.008.

    FIR Filter Design- MATLAB & Simulink (6)

To approach this design problem using fircls1,use the following commands:

n = 61;wo = 0.3;dp = 0.02;ds = 0.008;h = fircls1(n,wo,dp,ds);fvtool(h,1)

Note that the y-axisshown below is in Magnitude Squared. You can set this by right-clickingon the axis label and selecting Magnitude Squared fromthe menu.

FIR Filter Design- MATLAB & Simulink (7)

Multiband CLS Filter Design

fircls uses the same technique to design FIR filters with a specified piecewise constant magnitude response. In this case, you can specify a vector of band edges and a corresponding vector of band amplitudes. In addition, you can specify the maximum amount of ripple for each band.

For example, assume the specifications for a filter call for:

  • From 0 to 0.3 (normalized): amplitude 0, upper bound0.005, lower bound–0.005

  • From 0.3 to 0.5: amplitude 0.5, upper bound 0.51,lower bound 0.49

  • From 0.5 to 0.7: amplitude 0, upper bound 0.03, lowerbound –0.03

  • From 0.7 to 0.9: amplitude 1, upper bound 1.02, lowerbound 0.98

  • From 0.9 to 1: amplitude 0, upper bound 0.05, lowerbound –0.05

Design a CLS filter with impulse response order 129 that meetsthese specifications:

n = 129;f = [0 0.3 0.5 0.7 0.9 1];a = [0 0.5 0 1 0];up = [0.005 0.51 0.03 1.02 0.05];lo = [-0.005 0.49 -0.03 0.98 -0.05];h = fircls(n,f,a,up,lo);fvtool(h,1)

Note that the y-axisshown below is in Magnitude Squared. You can set this by right-clickingon the axis label and selecting Magnitude Squared fromthe menu.

FIR Filter Design- MATLAB & Simulink (8)

Weighted CLS Filter Design

Weighted CLS filter design lets you design lowpass or highpassFIR filters with relative weighting of the error minimization in eachband. The fircls1 function enables you to specifythe passband and stopband edges for the least squares weighting function,as well as a constant k that specifies the ratioof the stopband to passband weighting.

For example, consider specifications that call for an FIR filterwith impulse response order of 55 and cutoff frequency of 0.3 (normalized).Also assume maximum allowable passband ripple of 0.02 and maximumallowable stopband ripple of 0.004. In addition, add weighting requirements:

  • Passband edge for the weight function of 0.28 (normalized)

  • Stopband edge for the weight function of 0.32

  • Weight error minimization 10 times as much in thestopband as in the passband

To approach this using fircls1, type

n = 55;wo = 0.3;dp = 0.02;ds = 0.004;wp = 0.28;ws = 0.32;k = 10;h = fircls1(n,wo,dp,ds,wp,ws,k);fvtool(h,1)

Note that the y-axisshown below is in Magnitude Squared. You can set this by right-clickingon the axis label and selecting Magnitude Squared fromthe menu.

FIR Filter Design- MATLAB & Simulink (9)

Arbitrary-Response Filter Design

The cfirpm filter designfunction provides a tool for designing FIR filters with arbitrarycomplex responses. It differs from the other filter design functionsin how the frequency response of the filter is specified: it acceptsthe name of a function which returns the filter response calculatedover a grid of frequencies. This capability makes cfirpm ahighly versatile and powerful technique for filter design.

This design technique may be used to produce nonlinear-phaseFIR filters, asymmetric frequency-response filters (with complex coefficients),or more symmetric filters with custom frequency responses.

The design algorithm optimizes the Chebyshev (or minimax) errorusing an extended Remez-exchange algorithm for an initial estimate.If this exchange method fails to obtain the optimal filter, the algorithmswitches to an ascent-descent algorithm that takes over to finishthe convergence to the optimal solution.

Multiband Filter Design

Consider a multiband filter with the following special frequency-domaincharacteristics.

Band

Amplitude

Optimization Weighting

[–1 –0.5]

[5 1]

1

[–0.4 +0.3]

[2 2]

10

[+0.4 +0.8]

[2 1]

5

A linear-phase multiband filter may be designed using the predefinedfrequency-response function multiband, as follows:

b = cfirpm(38, [-1 -0.5 -0.4 0.3 0.4 0.8], ... {'multiband', [5 1 2 2 2 1]}, [1 10 5]);

For the specific case of a multiband filter, we can use a shorthandfilter design notation similar to the syntax for firpm:

b = cfirpm(38,[-1 -0.5 -0.4 0.3 0.4 0.8], ... [5 1 2 2 2 1], [1 10 5]);

As with firpm, a vector of band edges ispassed to cfirpm. This vector defines the frequencybands over which optimization is performed; note that there are twotransition bands, from –0.5to–0.4and from 0.3to0.4.

In either case, the frequency response is obtained and plottedusing linear scale in FVTool:

fvtool(b,1)

Note that the range of data shown below is (-pi,pi).

FIR Filter Design- MATLAB & Simulink (10)

The filter response for this multiband filteris complex, which is expected because of the asymmetry in the frequencydomain. The impulse response, which you can select from the FVTooltoolbar, is shown below.

FIR Filter Design- MATLAB & Simulink (11)

Filter Design with Reduced Delay

Consider the design of a 62-tap lowpass filter with a half-Nyquistcutoff. If we specify a negative offset value to the lowpass filterdesign function, the group delay offset for the design is significantlyless than that obtained for a standard linear-phase design. This filterdesign may be computed as follows:

b = cfirpm(61,[0 0.5 0.55 1],{'lowpass',-16});

The resulting magnitude response is

fvtool(b,1)

The y-axis is in Magnitude Squared, which you can set by right-clicking on the axis label and selecting Magnitude Squared from the menu.

FIR Filter Design- MATLAB & Simulink (12)

The group delay of the filter reveals that the offset has been reduced from N/2 to N/2-16 (i.e., from 30.5 to 14.5). Now, however, the group delay is no longer flat in the passband region. To create this plot, click the Group Delay Response button on the toolbar.

FIR Filter Design- MATLAB & Simulink (13)

If we compare this nonlinear-phase filter to a linear-phasefilter that has exactly 14.5 samples of group delay, the resultingfilter is of order 2*14.5, or 29. Using b=cfirpm(29,[0 0.5 0.55 1],'lowpass'), the passbandand stopband ripple is much greater for the order 29 filter. Thesecomparisons can assist you in deciding which filter is more appropriatefor a specific application.

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

FIR Filter Design- MATLAB & Simulink (14)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

FIR Filter Design
- MATLAB & Simulink (2024)
Top Articles
Latest Posts
Article information

Author: Edmund Hettinger DC

Last Updated:

Views: 5496

Rating: 4.8 / 5 (78 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Edmund Hettinger DC

Birthday: 1994-08-17

Address: 2033 Gerhold Pine, Port Jocelyn, VA 12101-5654

Phone: +8524399971620

Job: Central Manufacturing Supervisor

Hobby: Jogging, Metalworking, Tai chi, Shopping, Puzzles, Rock climbing, Crocheting

Introduction: My name is Edmund Hettinger DC, I am a adventurous, colorful, gifted, determined, precious, open, colorful person who loves writing and wants to share my knowledge and understanding with you.