# Finite Impulse Response (FIR) Filters | MATLAB Tutorial

Finite Impulse Response filters are called as non- recursive filters because they do not use the feedback. Different types of windows are used to design FIR filter. We will discuss the design of FIR filter using rectangular window in this post. Some other types of window functions are as follows:

1. Hamming Window.
2. Hanning Window.
3. Triangular (Barlett) Window.
4. Blackman Window.
5. Kaiser Window.

1. The windowing method requires minimum amount of computational effort, so window method is simple to implement.
2. For the given window, the maximum amplitude of ripple in the filter response is fixed. Thus the stopband attenuation is fixed in the given window.

1. The designing of FIR filters using windows is not flexible.
2. The frequency response of FIR filter shows the convolution of spectrum of window function and desired frequency response. Because of this, the passband and stopband edge frequencies cannot be precisely specified.
3. In many applications the expression for the desired filter response will be too complicated.

## Design steps for FIR filter:

1. Get the desired frequency response, Hd(ω).
2. Take Inverse Fourier Transform (IFT) of Hd(ω) to obtain hd(n).
3. Decide the length of FIR filter.
4. Multiply hd(n) by selecting window function to get h(n).
5. From h(n) obtain H(Z) and then realize it.

## Example:

• Design a linear phase FIR filter using rectangular window by taking 7 sample of window sequence and with ωc=0.2 π rad/sample.

Solution:

Step 1: The desired frequency response for low pass filter is given by,

Here M= 7 and ωc= 0.2π

Step 2: Taking IFT,

Step 3:

 When n= 0 1 2 3 4 5 6 h(n) 0.1009 0.1513 0.187 0.2 0.187 0.1513 0.1009

[when n=3, hd(n)= (1/ π)x ωc=0.2]

Step 4: The Transfer function H(z) of FIR low pass filter is given by,

Step 5: The magnitude response  |H(e­)|is given by |A(ω)| as,

Following table illustrates A(ω) and |H(e­)|for various values of ω,

 ω A(ω) |H(e­jω)|=| A(ω)| 0 x (π/16) 1.0788 1.0788 1 x (π/16) 1.0149 1.0149 2 x (π/16) 0.8370 0.8370 3 x (π/16) 0.5872 0.5872 4 x (π/16) 0.3219 0.3219 5 x (π/16) 0.0940 0.0940 6 x (π/16) -0.0573 0.0573 7 x (π/16) -0.1188 0.1188 8 x (π/16) -0.1028 0.1028 9 x (π/16) -0.0406 0.0406 10 x (π/16) 0.0291 0.0291 11 x (π/16) 0.0741 0.0741 12 x (π/16) 0.0780 0.0780 13 x (π/16) 0.0441 0.0441 14 x (π/16) -0.0088 0.0088 15 x (π/16) -0.0550 0.0550 16 x (π/16) -0.0732 0.0732

## MATLAB CODE:

```clear all
clc
wc=0.2*pi
N=7
hd=zeros(1,N)
a=(N-1)/2
hna=wc/pi
k=1:1:((N-1)/2)
n=k-1-((N-1)/2)
hd(k)=(sin(wc*n))./(pi*n)
hn(k)=hd(k)
hn=[hn hna]
w=0:pi/16:pi
hw1=hna*exp(-j*w*a)
hw2=0
for m=1:1:a
hw3=hn(m)*((exp(j*w*(1-m)))+(exp(-j*w*(1-m+2*a))))
hw2=hw2+hw3
end
hw=hw2+hw1
h_mag=abs(hw)
plot(w/pi,h_mag,'k')
xlabel('normalised freq')
ylabel('magnitude')
title('mag response of FIR filter')
```

## Output:

[showhide type=”post” more_text=”Show Full Output obtained from command window…” less_text=”Show less…”]
wc =    0.6283
N =     7
hd =     0     0     0     0     0     0     0
a =     3
hna =    0.2000
k =     1     2     3
n =    -3    -2    -1
hd =     0.1009    0.1514    0.1871         0         0         0         0
hn =    0.1009    0.1514    0.1871
hn =    0.1009    0.1514    0.1871    0.2000
a =     3
w =  Columns 1 through 7         0    0.1963    0.3927    0.5890    0.7854    0.9817    1.1781  Columns 8 through 14    1.3744    1.5708    1.7671    1.9635    2.1598    2.3562    2.5525  Columns 15 through 17    2.7489    2.9452    3.1416hw1 =  Columns 1 through 4   0.2000 + 0.0000i   0.1663 – 0.1111i   0.0765 – 0.1848i  -0.0390 – 0.1962i  Columns 5 through 8  -0.1414 – 0.1414i  -0.1962 – 0.0390i  -0.1848 + 0.0765i  -0.1111 + 0.1663i  Columns 9 through 12  -0.0000 + 0.2000i   0.1111 + 0.1663i   0.1848 + 0.0765i   0.1962 – 0.0390i  Columns 13 through 16   0.1414 – 0.1414i   0.0390 – 0.1962i  -0.0765 – 0.1848i  -0.1663 – 0.1111i  Column 17  -0.2000 – 0.0000ihw2 =     0hw3 =  Columns 1 through 4   0.2018 + 0.0000i   0.1395 – 0.0932i   0.0296 – 0.0714i   0.0077 + 0.0386i  Columns 5 through 8   0.1009 + 0.1009i   0.1941 + 0.0386i   0.1723 – 0.0714i   0.0623 – 0.0932i  Columns 9 through 12   0.0000 – 0.0000i   0.0623 + 0.0932i   0.1723 + 0.0714i   0.1941 – 0.0386i  Columns 13 through 16   0.1009 – 0.1009i   0.0077 – 0.0386i   0.0296 + 0.0714i   0.1395 + 0.0932i  Column 17   0.2018 + 0.0000ihw2 =  Columns 1 through 4   0.2018 + 0.0000i   0.1395 – 0.0932i   0.0296 – 0.0714i   0.0077 + 0.0386i  Columns 5 through 8   0.1009 + 0.1009i   0.1941 + 0.0386i   0.1723 – 0.0714i   0.0623 – 0.0932i  Columns 9 through 12   0.0000 – 0.0000i   0.0623 + 0.0932i   0.1723 + 0.0714i   0.1941 – 0.0386i  Columns 13 through 16   0.1009 – 0.1009i   0.0077 – 0.0386i   0.0296 + 0.0714i   0.1395 + 0.0932i  Column 17   0.2018 + 0.0000ihw3 =  Columns 1 through 4   0.3027 + 0.0000i   0.2326 – 0.1554i   0.0819 – 0.1978i  -0.0226 – 0.1136i  Columns 5 through 8  -0.0000 + 0.0000i   0.1136 + 0.0226i   0.1978 – 0.0819i   0.1554 – 0.2326i  Columns 9 through 12   0.0000 – 0.3027i  -0.1554 – 0.2326i  -0.1978 – 0.0819i  -0.1136 + 0.0226i  Columns 13 through 16  -0.0000 + 0.0000i   0.0226 – 0.1136i  -0.0819 – 0.1978i  -0.2326 – 0.1554i  Column 17  -0.3027 – 0.0000ihw2 =  Columns 1 through 4   0.5046 + 0.0000i   0.3721 – 0.2486i   0.1115 – 0.2691i  -0.0149 – 0.0750i  Columns 5 through 8   0.1009 + 0.1009i   0.3078 + 0.0612i   0.3700 – 0.1533i   0.2177 – 0.3258i  Columns 9 through 12   0.0000 – 0.3027i  -0.0931 – 0.1393i  -0.0255 – 0.0106i   0.0805 – 0.0160i  Columns 13 through 16   0.1009 – 0.1009i   0.0303 – 0.1522i  -0.0524 – 0.1264i  -0.0930 – 0.0622i  Column 17  -0.1009 – 0.0000ihw3 =  Columns 1 through 4   0.3742 + 0.0000i   0.3052 – 0.2039i   0.1323 – 0.3194i  -0.0607 – 0.3052i  Columns 5 through 8  -0.1871 – 0.1871i  -0.2039 – 0.0406i  -0.1323 + 0.0548i  -0.0406 + 0.0607i  Columns 9 through 12   0.0000 + 0.0000i  -0.0406 – 0.0607i  -0.1323 – 0.0548i  -0.2039 + 0.0406i  Columns 13 through 16  -0.1871 + 0.1871i  -0.0607 + 0.3052i   0.1323 + 0.3194i   0.3052 + 0.2039i  Column 17   0.3742 + 0.0000ihw2 =  Columns 1 through 4   0.8787 + 0.0000i   0.6772 – 0.4525i   0.2438 – 0.5885i  -0.0756 – 0.3802i  Columns 5 through 8  -0.0862 – 0.0862i   0.1039 + 0.0207i   0.2377 – 0.0985i   0.1771 – 0.2651i  Columns 9 through 12   0.0000 – 0.3027i  -0.1336 – 0.2000i  -0.1578 – 0.0654i  -0.1234 + 0.0245i  Columns 13 through 16  -0.0862 + 0.0862i  -0.0304 + 0.1529i   0.0799 + 0.1930i   0.2121 + 0.1417i  Column 17   0.2733 + 0.0000ihw =  Columns 1 through 4   1.0787 + 0.0000i   0.8435 – 0.5636i   0.3203 – 0.7733i  -0.1146 – 0.5763i  Columns 5 through 8  -0.2276 – 0.2276i  -0.0923 – 0.0184i   0.0530 – 0.0219i   0.0660 – 0.0988i  Columns 9 through 12   0.0000 – 0.1027i  -0.0225 – 0.0337i   0.0270 + 0.0112i   0.0728 – 0.0145i  Columns 13 through 16   0.0552 – 0.0552i   0.0086 – 0.0432i   0.0034 + 0.0082i   0.0458 + 0.0306i  Column 17   0.0733 + 0.0000ih_mag =  Columns 1 through 7    1.0787    1.0145    0.8370    0.5876    0.3219    0.0941    0.0573  Columns 8 through 14    0.1188    0.1027    0.0406    0.0292    0.0742    0.0781    0.0441  Columns 15 through 17    0.0089    0.0551    0.0733

[/showhide]