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 `n`

th-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\pi \le \omega \le 0.65\pi $$ rad/sample. Visualize its magnitude and phase responses.

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

### 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)

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)

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)

### Multiband FIR Filter

Open Live Script

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

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

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

## Input Arguments

collapse all

`n`

— Filter 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 odd`n`

fora highpass or bandstop filter, then `fir1`

increments `n`

by1.

**Data Types: **`double`

`Wn`

— Frequency 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`n`

th-order multiband filter with bands 0<*ω*<`w1`

,`w1`

<*ω*<`w2`

, …,`wn`

<*ω*<1.

**Data Types: **`double`

`ftype`

— Filter 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.

`window`

— Window

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`

`scaleopt`

— Normalization 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.

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)

Contact your local office