Least-squares linear-phase FIR filter design
Filter with Transition Band
Design an FIR lowpass filter of order 255 with a transition region between and . Use
fvtool to display the magnitude and phase responses of the filter.
b = firls(255,[0 0.25 0.3 1],[1 1 0 0]); fvtool(b,1,'OverlayedAnalysis','phase')
Design of a Differentiator
An ideal differentiator has a frequency response given by . Design a differentiator of order 30 that attenuates frequencies above . Include a factor of in the amplitude because the frequencies are normalized by . Display the zero-phase response of the filter.
b = firls(30,[0 0.9],[0 0.9*pi],'differentiator'); fvtool(b,1,'MagnitudeDisplay','zero-phase')
Filter with Piecewise Linear Passbands
Design a 24th-order antisymmetric filter with piecewise linear passbands.
F = [0 0.3 0.4 0.6 0.7 0.9]; A = [0 1.0 0.0 0.0 0.5 0.5]; b = firls(24,F,A,'hilbert');
Plot the desired and actual frequency responses.
[H,f] = freqz(b,1,512,2); plot(f,abs(H)) hold on for i = 1:2:6, plot([F(i) F(i+1)],[A(i) A(i+1)],'r--') end legend('firls design','Ideal') grid on xlabel('Normalized Frequency (\times\pi rad/sample)') ylabel('Magnitude')
Lowpass Filter Design with Weighted Fit
Design an FIR lowpass filter. The passband ranges from DC to rad/sample. The stopband ranges from rad/sample to the Nyquist frequency. Produce three different designs, changing the weights of the bands in the least-squares fit.
In the first design, make the stopband weight higher than the passband weight by a factor of 100. Use this specification when it is critical that the magnitude response in the stopband is flat and close to 0. The passband ripple is about 100 times higher than the stopband ripple.
bhi = firls(18,[0 0.45 0.55 1],[1 1 0 0],[1 100]);
In the second design, reverse the weights so that the passband weight is 100 times the stopband weight. Use this specification when it is critical that the magnitude response in the passband is flat and close to 1. The stopband ripple is about 100 times higher than the passband ripple.
blo = firls(18,[0 0.45 0.55 1],[1 1 0 0],[100 1]);
In the third design, give the same weight to both bands. The result is a filter with similar ripple in the passband and the stopband.
b = firls(18,[0 0.45 0.55 1],[1 1 0 0],[1 1]);
Visualize the magnitude responses of the three filters.
hfvt = fvtool(bhi,1,blo,1,b,1,'MagnitudeDisplay','Zero-phase'); legend(hfvt,'bhi: w = [1 100]','blo: w = [100 1]','b: w = [1 1]')
n — Filter order
real positive scalar
Filter order, specified as a real positive scalar.
f — Normalized frequency points
Normalized frequency points, specified as a real-valued vector. The argument must be in the range [0, 1] , where 1 corresponds to the Nyquist frequency. The number of elements in the vector is always a multiple of 2. The frequencies must be in nondecreasing order.
a — Desired amplitude
Desired amplitudes at the points specified in
f, specified as a
a must be the same length. The
length must be an even number.
The desired amplitude at frequencies between pairs of points (f(k), f(k+1)) for k odd is the line segment connecting the points (f(k), a(k)) and (f(k+1), a(k+1)).
The desired amplitude at frequencies between pairs of points (f(k), f(k+1)) for k even is unspecified. The areas between such points are transition regions or regions that are not important for a particular application.
ftype — Filter type
Filter type for linear-phase filters with odd symmetry (type III and type IV),
specified as either
'hilbert'— The output coefficients in
bobey the relation b(k) = –b(n + 2 – k), k = 1, ..., n + 1. This class of filters includes the Hilbert transformer, which has a desired amplitude of 1 across the entire band.
'differentiator'— For nonzero amplitude bands, the filter weighs the error by a factor of 1/f2 so that the error at low frequencies is much smaller than at high frequencies. For FIR differentiators, which have an amplitude characteristic proportional to frequency, these filters minimize the maximum relative error (the maximum of the ratio of the error to the desired amplitude).
b — Filter coefficients
Filter coefficients, returned as a row vector of length
n + 1.
The coefficients are in increasing order.
Filter Length and Transition Width Incompatibility
If you design a filter such that the product of the filter length and the transition width is large, you might get this warning message:
Matrix is close to singular or badly scaled. The following example illustrates this limitation.
b = firls(100,[0 0.15 0.85 1],[1 1 0 0]);
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 3.756863e-19.
In this case, the filter coefficients
b might not represent the desired filter. You can check the filter by looking at its frequency response.
firls designs a linear-phase FIR filter that minimizes 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.
Reference  describes the theoretical
firls. The function solves a system of linear equations
involving an inner product matrix of roughly the size
n\2 using the
These are type I (
n is odd) and type II (
n is even)
linear-phase filters. Vectors
a specify the
frequency-amplitude characteristics of the filter:
fis a vector of pairs of frequency points, specified in the range 0 to 1, where 1 corresponds to the Nyquist frequency. The frequencies must be in increasing order. Duplicate frequency points are allowed.
ais a vector containing the desired amplitudes at the points specified in
The desired amplitude function at frequencies between pairs of points (f(k), f(k+1)) for k odd is the line segment connecting the points (f(k), a(k)) and (f(k+1), a(k+1)).
The desired amplitude function at frequencies between pairs of points (f(k), f(k+1)) for k even is unspecified. These are transition ("don’t care") regions.
aare the same length. This length must be an even number.
This figure illustrates the relationship between the
a vectors in defining a desired amplitude response.
This function designs type I, II, III, and IV linear-phase filters. Type I and II are the
default filters when n is even and odd, respectively, while the
'differentiator' flags produce type III (n is even) and IV (n is odd)
filters. The various filter types have different symmetries and constraints on their frequency
responses (see  for details).
|Linear Phase Filter Type||Filter Order||Symmetry of Coefficients||Response H(f), f = 0||Response H(f), f = 1 (Nyquist)|
|H(1) = 0|
H(0) = 0
H(1) = 0
H(0) = 0
 Oppenheim, Alan V., Ronald W. Schafer, and John R. Buck. Discrete-Time Signal Processing. Upper Saddle River, NJ: Prentice Hall, 1999.
 Parks, Thomas W., and C. Sidney Burrus. Digital Filter Design. Hoboken, NJ: John Wiley & Sons, 1987, pp. 54–83.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Introduced before R2006a