Window-based FIR filter design
FIR Bandpass Filter
Design a 48th-order FIR bandpass filter with passband rad/sample. Visualize its magnitude and phase responses.
b = fir1(48,[0.35 0.65]); freqz(b,1,512)
FIR Highpass Filter
chirp.mat. The file contains a signal,
y, that has most of its power above
Fs/4, or half the Nyquist frequency. The sample rate is 8192 Hz.
Design a 34th-order FIR highpass filter to attenuate the components of the signal below
Fs/4. Use a cutoff frequency of 0.48 and a Chebyshev window with 30 dB of ripple.
load chirp t = (0:length(y)-1)/Fs; bhi = fir1(34,0.48,'high',chebwin(35,30)); freqz(bhi,1)
Filter the signal. Display the original and highpass-filtered signals. Use the same y-axis scale for both plots.
outhi = filter(bhi,1,y); subplot(2,1,1) plot(t,y) title('Original Signal') ys = ylim; subplot(2,1,2) plot(t,outhi) title('Highpass Filtered Signal') xlabel('Time (s)') ylim(ys)
Design a lowpass filter with the same specifications. Filter the signal and compare the result to the original. Use the same y-axis scale for both plots.
blo = fir1(34,0.48,chebwin(35,30)); outlo = filter(blo,1,y); subplot(2,1,1) plot(t,y) title('Original Signal') ys = ylim; subplot(2,1,2) plot(t,outlo) title('Lowpass Filtered Signal') xlabel('Time (s)') ylim(ys)
Multiband FIR Filter
Design a 46th-order FIR filter that attenuates normalized frequencies below rad/sample and between and rad/sample. Call it
bM. Compute its frequency response.
ord = 46; low = 0.4; bnd = [0.6 0.9]; bM = fir1(ord,[low bnd]); [hbM,f] = freqz(bM,1);
bM so that it passes the bands it was attenuating and stops the other frequencies. Call the new filter
bW. Display the frequency responses of the filters.
bW = fir1(ord,[low bnd],"DC-1"); [hbW,~] = freqz(bW,1); plot(f/pi,mag2db(abs(hbM)),f/pi,mag2db(abs(hbW))) legend("bM","bW",Location="best") ylim([-75 5]) grid
bM using a Hann window. (The
"DC-0" is optional.) Compare the magnitude responses of the Hamming and Hann designs.
hM = fir1(ord,[low bnd],'DC-0',hann(ord+1)); hhM = freqz(hM,1); plot(f/pi,mag2db(abs(hbM)),f/pi,mag2db(abs(hhM))) legend("Hamming","Hann",Location="northwest") ylim([-75 5]) grid
bW using a Tukey window. Compare the magnitude responses of the Hamming and Tukey designs.
tW = fir1(ord,[low bnd],'DC-1',tukeywin(ord+1)); htW = freqz(tW,1); plot(f/pi,mag2db(abs(hbW)),f/pi,mag2db(abs(htW))) legend("Hamming","Tukey",Location="best") ylim([-75 5]) grid
n — Filter order
Filter order, specified as an integer scalar.
For highpass and bandstop configurations,
uses an even filter order. The order must be even because odd-order
symmetric FIR filters must have zero gain at the Nyquist frequency.
If you specify an odd
a highpass or bandstop filter, then
n by 1.
Wn — Frequency constraints
scalar | two-element vector | multi-element vector
Frequency constraints, specified as a scalar, a two-element
vector, or a multi-element vector. All elements of
be strictly greater than 0 and strictly smaller than 1, where 1 corresponds
to the Nyquist frequency: 0 <
Wn < 1. The Nyquist frequency is half
the sample rate or π rad/sample.
Wnis a scalar, then
fir1designs a lowpass or highpass filter with cutoff frequency
Wn. The cutoff frequency is the frequency at which the normalized gain of the filter is –6 dB.
Wnis the two-element vector
[w1 w2], where
fir1designs a bandpass or bandstop filter with lower cutoff frequency
w1and higher cutoff frequency
Wnis the multi-element vector
[w1 w2 ... wn], where
w2< … <
nth-order multiband filter with bands 0 < ω <
w1< ω <
wn< ω < 1.
ftype — Filter type
Filter type, specified as one of the following:
'low'specifies a lowpass filter with cutoff frequency
'low'is the default for scalar
'high'specifies a highpass filter with cutoff frequency
'bandpass'specifies a bandpass filter if
Wnis a two-element vector.
'bandpass'is the default when
Wnhas two elements.
'stop'specifies a bandstop filter if
Wnis a two-element vector.
'DC-0'specifies that the first band of a multiband filter is a stopband.
'DC-0'is the default when
Wnhas more than two elements.
'DC-1'specifies that the first band of a multiband filter is a passband.
window — Window
fir1 does not automatically increase the
window if you attempt to design a highpass
or bandstop filter of odd order.
kaiser(n+1,0.5) specifies a Kaiser
window with shape parameter 0.5 to use with a filter of order
hamming(n+1) is equivalent to leaving
the window unspecified.
scaleopt — Normalization option
'scale' (default) |
Normalization option, specified as either
'scale'normalizes the coefficients so that the magnitude response of the filter at the center of the passband is 1 (0 dB).
'noscale'does not normalize the coefficients.
fir1 uses a least-squares approximation to
compute the filter coefficients and then smooths the impulse response
 Digital Signal Processing Committee of the IEEE Acoustics, Speech, and Signal Processing Society, eds. Programs for Digital Signal Processing. New York: IEEE Press, 1979, Algorithm 5.2.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Introduced before R2006a