You are here : matlabSignal Processingfir1

# fir1() - Signal Processing

```exampleb = fir1(n,Wn) uses
a Hamming window to design an nth-order lowpass,
bandpass, or multiband FIR filter with linear phase. The filter type
depends on the number of elements of Wn.
exampleb = fir1(n,Wn,ftype) designs
a lowpass, highpass, bandpass, bandstop, or multiband filter, depending
on the value of ftype and the number of elements
of Wn.

exampleb = fir1(___,window) designs
the filter using the vector specified in window and
any of the arguments from previous syntaxes.
specifies whether or not the magnitude response of the filter is normalized.
Note

Use fir2 for windowed
filters with arbitrary frequency response.```

### Syntax

`b = fir1(n,Wn) exampleb = fir1(n,Wn,ftype) exampleb = fir1(___,window) exampleb = fir1(___,scaleopt)`

### Example

```FIR Bandpass FilterOpen This Example
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 FilterOpen This Example
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 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 FilterOpen This Example
Design a 44th-order FIR filter that attenuates normalized frequencies below
and
ord = 44;

low = 0.4;
bnd = [0.6 0.9];

bM = fir1(ord,[low bnd]);
Redesign bM so that it passes the bands it was attenuating and stops the other frequencies. Call the new filter bW. Use fvtool to display the frequency responses of the filters.bW = fir1(ord,[low bnd],'DC-1');

hfvt = fvtool(bM,1,bW,1);
legend(hfvt,'bM','bW')

Redesign bM using a Hann window. (The string 'DC-0' is optional.) Compare the magnitude responses of the Hamming and Hann designs.hM = fir1(ord,[low bnd],'DC-0',hann(ord+1));

hfvt = fvtool(bM,1,hM,1);
legend(hfvt,'Hamming','Hann')

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

hfvt = fvtool(bW,1,tW,1);
legend(hfvt,'Hamming','Tukey')```