fir1 - Window-based FIR filter design (2024)

Window-based FIR filter design

collapse all in page

Syntax

b = fir1(n,Wn)

b = fir1(n,Wn,ftype)

b = fir1(___,window)

b = fir1(___,scaleopt)

Description

b = fir1(n,Wn) usesa Hamming window to design an nth-order lowpass,bandpass, or multiband FIR filter with linear phase. The filter typedepends on the number of elements of Wn.

example

b = fir1(n,Wn,ftype) designsa lowpass, highpass, bandpass, bandstop, or multiband filter, dependingon the value of ftype and the number of elementsof Wn.

example

b = fir1(___,window) designsthe filter using the vector specified in window andany of the arguments from previous syntaxes.

example

b = fir1(___,scaleopt) additionallyspecifies whether or not the magnitude response of the filter is normalized.

Note:Use fir2 forwindowed filters with arbitrary frequency response.

Examples

collapse all

FIR Bandpass Filter

Open Live Script

Design a 48th-order FIR bandpass filter with passband 0.35πω0.65π rad/sample. Visualize its magnitude and phase responses.

b = fir1(48,[0.35 0.65]);freqz(b,1,512)

fir1 - Window-based FIR filter design (1)

FIR Highpass Filter

Open Live Script

Load 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 chirpt = (0:length(y)-1)/Fs;bhi = fir1(34,0.48,"high",chebwin(35,30));freqz(bhi,1)

fir1 - Window-based FIR filter design (2)

Filter the signal. Display the original and highpass-filtered signals. Use the same y-axis scale for both plots.

outhi = filter(bhi,1,y);tiledlayout("flow")nexttileplot(t,y)title("Original Signal")ys = ylim;nexttileplot(t,outhi)title("Highpass Filtered Signal")xlabel("Time (s)")ylim(ys)

fir1 - Window-based FIR filter design (3)

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);tiledlayout("flow")nexttileplot(t,y)title("Original Signal")ys = ylim;nexttileplot(t,outlo)title("Lowpass Filtered Signal")xlabel("Time (s)")ylim(ys)

fir1 - Window-based FIR filter design (4)

Multiband FIR Filter

Open Live Script

Design a 46th-order FIR filter that attenuates normalized frequencies below 0.4π rad/sample and between 0.6π and 0.9π 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);

Redesign 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

fir1 - Window-based FIR filter design (5)

Redesign 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

fir1 - Window-based FIR filter design (6)

Redesign 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

fir1 - Window-based FIR filter design (7)

Input Arguments

collapse all

nFilter order
integer scalar

Filter order, specified as an integer scalar.

For highpass and bandstop configurations, fir1 alwaysuses an even filter order. The order must be even because odd-ordersymmetric FIR filters must have zero gain at the Nyquist frequency.If you specify an oddn fora highpass or bandstop filter, then fir1 increments n by1.

Data Types: double

WnFrequency constraints
scalar | two-element vector | multi-element vector

Frequency constraints, specified as a scalar, a two-elementvector, or a multi-element vector. All elements of Wn mustbe strictly greater than 0 and strictly smaller than 1, where 1 correspondsto the Nyquist frequency: 0<Wn<1. The Nyquist frequency is halfthe sample rate or πrad/sample.

  • If Wn is a scalar, then fir1 designsa lowpass or highpass filter with cutoff frequency Wn.The cutoff frequency is the frequency at which the normalized gainof the filter is –6dB.

  • If Wn is the two-element vector [w1w2], where w1<w2, then fir1 designsa bandpass or bandstop filter with lower cutoff frequency w1 andhigher cutoff frequency w2.

  • If Wn is the multi-element vector [w1w2...wn],where w1<w2<…<wn, then fir1 returnsan nth-order multiband filter with bands 0<ω<w1, w1<ω<w2, …, wn<ω<1.

Data Types: double

ftypeFilter type
'low' | 'bandpass' | 'high' | 'stop' | 'DC-0' | 'DC-1'

Filter type, specified as one of the following:

  • 'low' specifies a lowpass filterwith cutoff frequency Wn. 'low' isthe default for scalar Wn.

  • 'high' specifies a highpass filterwith cutoff frequency Wn.

  • 'bandpass' specifies a bandpassfilter if Wn is a two-element vector. 'bandpass' isthe default when Wn has two elements.

  • 'stop' specifies a bandstop filterif Wn is a two-element vector.

  • 'DC-0' specifies that the firstband of a multiband filter is a stopband. 'DC-0' isthe default when Wn has more than two elements.

  • 'DC-1' specifies that the firstband of a multiband filter is a passband.

windowWindow
vector

Window, specified as a vector. The window vector must have n+1 elements. If you do not specify window,then fir1 uses a Hamming window. For a list ofavailable windows, see Windows.

fir1 does not automatically increase thelength of window if you attempt to design a highpassor bandstop filter of odd order.

Example: kaiser(n+1,0.5) specifies a Kaiserwindow with shape parameter 0.5 to use with a filter of order n.

Example: hamming(n+1) is equivalent to leavingthe window unspecified.

Data Types: double

scaleoptNormalization option
'scale' (default) | 'noscale'

Normalization option, specified as either 'scale' or 'noscale'.

  • 'scale' normalizes the coefficientsso that the magnitude response of the filter at the center of thepassband is 1 (0dB).

  • 'noscale' does not normalize thecoefficients.

Output Arguments

collapse all

b — Filter coefficients
row vector

Filter coefficients, returned as a row vector of length n+1. The coefficients are sorted in descendingpowers of the Z-transform variable z:

B(z)=b(1)+b(2)z+…+b(n+1)z–n.

Algorithms

fir1 uses a least-squares approximation tocompute the filter coefficients and then smooths the impulse responsewith window.

References

[1] Digital Signal Processing Committee of theIEEE Acoustics, Speech, and Signal Processing Society, eds. Programsfor Digital Signal Processing. New York: IEEE Press, 1979,Algorithm 5.2.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced before R2006a

See Also

Apps

  • Filter Analyzer | Filter Designer

Functions

  • cfirpm | designfilt | filter | fir2 | fircls | fircls1 | firls | firpm | freqz | hamming | kaiserord

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.

fir1 - Window-based FIR filter design (8)

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

fir1 - Window-based FIR filter design (2024)

FAQs

How you will design an FIR filter using window method? ›

To design an FIR filter by windowing, you first decide on an ideal frequency response and calculate the impulse response of the ideal frequency response. You then truncate the impulse response to produce a finite number of coefficients.

What is the difference between fir1 and fir2? ›

The fir2 function also designs windowed FIR filters, but with an arbitrarily shaped piecewise linear frequency response. This is in contrast to fir1 , which only designs filters in standard lowpass, highpass, bandpass, and bandstop configurations.

Why is Kaiser window most used for designing FIR filters? ›

It is a one-parameter family of window functions used in finite impulse response filter design and spectral analysis. The Kaiser window approximates the DPSS window which maximizes the energy concentration in the main lobe but which is difficult to compute.

What does fir1 do in Matlab? ›

If Wn is a scalar, then fir1 designs 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.

What are the requirements for FIR filter design? ›

(i) A desired or ideal response is chosen, usually in the frequency domain. (ii) An allowed class of filters is chosen (e.g. the length N for a FIR filters). (iii) A measure of the quality of approximation is chosen. (iv) A method or algorithm is selected to find the best filter transfer function.

What are the disadvantages of a FIR filter? ›

Some of the advantages of FIR filter are linear phase, both recursive and non recursive, stable and round off noise can be made smaller. Some of the disadvantages of FIR filters are large amount of processing is required and non integral delay may lead to problems.

Which is better, an IIR or FIR filter? ›

Advantages and Disadvantages of FIR and IIR Filters

The advantage of IIR filters over FIR filters is that IIR filters usually require fewer coefficients to execute similar filtering operations, that IIR filters work faster, and require less memory space. The disadvantage of IIR filters is the nonlinear phase response.

Why is the FIR filter always stable? ›

In contrast, FIR filters are always stable because the FIR filters do not have poles. You can determine if pole-zero pairs are close enough to cancel out each other effectively. Try deleting close pairs and then check the resulting frequency response.

What is the difference between firefighter 1 and 2 training? ›

After earning a Firefighter I certification, individuals can pursue a Firefighter II certification. This advanced certification program provides additional training in preparedness, structural fire control and rescue operations.

Why is Kaiser window better than other windows? ›

If we talk about the advantages of Kaiser window then in Kaiser window the passband and stopband ripple size are not much affected by the change in the window length in comparison to the other windows. It is affected by the coefficients roll off that is also known as the shape factor.

What is the Kaiser window technique? ›

The Kaiser window is an approximation to the prolate spheroidal window, for which the ratio of the mainlobe energy to the sidelobe energy is maximized. For a Kaiser window of a particular length, the parameter β controls the relative sidelobe attenuation.

What are the different types of FIR filter windows? ›

—The paper presents FIR filter design by different window functions such as, Gaussian window, Kaiser window, Hamming window, and Blackman window. In the design procedure of FIR filter, we determine its side love peak by the help of different window function.

Why are FIR filters called all zero filters? ›

In signal processing, a finite impulse response (FIR) filter is a filter whose impulse response (or response to any finite length input) is of finite duration, because it settles to zero in finite time.

What is an ideal filter in MATLAB? ›

Ideal filters are noncausal, and the ends of the filter amplitude are flat in the frequency domain. The data in ts must have zero mean. tsout = idealfilter( tsin , interval , filtertype , ind ) optionally specifies the row or column indices of tsin to apply the filter to.

How to calculate coefficients of FIR filter? ›

The key theorem of FIR filter design is that the coefficients h(n) of the FIR filter are simply the quantized values of the impulse response of the frequency transfer function H(f). Conversely, the impulse response is the Fourier Transform of H(f).

Why are windows used in filter design? ›

Due to its simplicity and efficiency, the window method is most commonly used method for designing filters. The sampling frequency method is easy to use, but filters designed this way have small attenuation in the stop band.

How do you design a filter? ›

One common method for designing FIR filters is the Parks-McClellan filter design algorithm, based on the Remez exchange algorithm. Here the user specifies a desired frequency response, a weighting function for errors from this response, and a filter order N.

Why is a rectangular window mostly preferred in FIR filter design? ›

since the sinc function extends to infinity in both side, it results in an infinite numbers of taps of the FIR filters. Then such filters are not practical and the number of taps must be truncated. This is accomplished by using windows. The simplest window is the rectangular window.

What is the window technique in digital signal processing? ›

Windowing is a technique used to shape the time portion of your measurement data, to minimize edge effects that result in spectral leakage in the FFT spectrum. By using Window Functions correctly, the spectral resolution of your frequency-domain result will increase.

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Kieth Sipes

Last Updated:

Views: 6580

Rating: 4.7 / 5 (67 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Kieth Sipes

Birthday: 2001-04-14

Address: Suite 492 62479 Champlin Loop, South Catrice, MS 57271

Phone: +9663362133320

Job: District Sales Analyst

Hobby: Digital arts, Dance, Ghost hunting, Worldbuilding, Kayaking, Table tennis, 3D printing

Introduction: My name is Kieth Sipes, I am a zany, rich, courageous, powerful, faithful, jolly, excited person who loves writing and wants to share my knowledge and understanding with you.