You are here : matlabSignal Processingfir2

fir2() - Signal Processing

exampleb = fir2(n,f,m) returns
an nth-order FIR filter with frequency-magnitude
characteristics specified in the vectors f and m.
The function linearly interpolates the desired frequency response
onto a dense grid and then uses the inverse Fourier transform and
a Hamming window to obtain the filter coefficients.
exampleb = fir2(n,f,m,npt,lap) specifies npt,
the number of points in the interpolation grid, and lap,
the length of the region that fir2 inserts around
duplicate frequency points which specify steps in the frequency response.

exampleb = fir2(___,window) specifies
from previous syntaxes.
Note

Use fir1 for window-based
standard lowpass, bandpass, highpass, bandstop, and multiband configurations.

Syntax

b = fir2(n,f,m) exampleb = fir2(n,f,m,npt,lap) exampleb = fir2(___,window) example

Example

Attenuation of Low FrequenciesOpen This Example
Load the MAT-file chirp. The file contains a signal, y, sampled at a frequency
. y has most of its power above
, or half the Nyquist frequency. Add random noise to the signal.
y = y + 0.25*(rand(size(y))-0.5);
Design a 34th-order FIR highpass filter to attenuate the components of the signal below
. Specify a cutoff frequency of 0.48. Visualize the frequency response of the filter.f = [0 0.48 0.48 1];
mhi = [0 0 1 1];
bhi = fir2(34,f,mhi);

freqz(bhi,1)

Filter the chirp signal. Plot the signal before and after filtering.outhi = filter(bhi,1,y);
t = (0:length(y)-1)/Fs;

subplot(2,1,1)
plot(t,y)
title('Original Signal')
ylim([-1.2 1.2])

subplot(2,1,2)
plot(t,outhi)
title('Higpass Filtered Signal')
xlabel('Time (s)')
ylim([-1.2 1.2])

Change the filter from highpass to lowpass. Use the same order and cutoff. Filter the signal again. The result is mostly noise.mlo = [1 1 0 0];
blo = fir2(34,f,mlo);
outlo = filter(blo,1,y);

subplot(2,1,1)
plot(t,y)
title('Original Signal')
ylim([-1.2 1.2])

subplot(2,1,2)
plot(t,outlo)
title('Lowpass Filtered Signal')
xlabel('Time (s)')
ylim([-1.2 1.2])

FIR Lowpass FilterOpen This Example
Design a 30th-order lowpass filter with a normalized cutoff frequency of
rad/sample. Plot the ideal frequency response overlaid with the actual frequency response.
f = [0 0.6 0.6 1];
m = [1 1 0 0];

b1 = fir2(30,f,m);
[h1,w] = freqz(b1,1);

plot(f,m,w/pi,abs(h1))
xlabel('\omega / \pi');
lgs = {'Ideal','fir2 default'};
legend(lgs)

Redesign the filter using a 64-point interpolation grid.b2 = fir2(30,f,m,64);
h2 = freqz(b2,1);

hold on
plot(w/pi,abs(h2))
lgs{3} = 'npt = 64';
legend(lgs)

Redesign the filter using the 64-point interpolation grid and a 13-point interval around the cutoff frequency.b3 = fir2(30,f,m,64,13);
h3 = freqz(b3,1);

plot(w/pi,abs(h3))
lgs{4} = 'lap = 13';
legend(lgs)

Arbitrary Magnitude FilterOpen This Example
Design an FIR filter with the following frequency response:
A sinusoid between 0 and
A1 = 0.5+sin(2*pi*7.5*F1)/4;
A piecewise linear section between
rad/sample.F2 = [0.2 0.38 0.4 0.55 0.562 0.585 0.6 0.78];
A2 = [0.5 2.3 1 1 -0.2 -0.2 1 1];
rad/sample and the Nyquist frequency.F3 = 0.79:0.01:1;
A3 = 0.2+18*(1-F3).^2;
Design the filter using a Hamming window. Specify a filter order of 50.N = 50;

FreqVect = [F1 F2 F3];
AmplVect = [A1 A2 A3];

ham = fir2(N,FreqVect,AmplVect);
Repeat the calculation using a Kaiser window that has a shape parameter of 3.kai = fir2(N,FreqVect,AmplVect,kaiser(N+1,3));
Redesign the filter using the designfilt function. designfilt uses a rectangular window by default. Compute the zero-phase response of the filter over 1024 points.d = designfilt('arbmagfir','FilterOrder',N, ...
'Frequencies',FreqVect,'Amplitudes',AmplVect);

[zd,wd] = zerophase(d,1024);
Display the zero-phase responses of the three filters. Overlay the ideal response.zerophase(ham,1)
hold on
zerophase(kai,1)
plot(wd/pi,zd)
plot(FreqVect,AmplVect,'k:')
legend('Hamming','Kaiser','designfilt','ideal')