You are on page 1of 6

Lab Handout 09:

Course Code:
Course Name:

FIR Filter Design Using MATLAB

EEE 420
Digital Signal Processing

Electrical & Electronic Engineering , Eastern Mediterranean University


Instructor: Asst. Prof. Dr. Erhan A. nce
Lab Assistant: Mr. Burin zmen
E-mail:
erhan.ince@emu.edu.tr
E-mail:
burcin.ozmen@emu.edu.tr
Ext:
2778
Ext:
2771

LINEAR-PHASE FIR FILTER


Shapes of impulse and frequency responses and locations of system function zeros of linearphase FIR filters will be discussed.
Let h n , 0 n M - 1 be the impulse response of length (or duration) M. Then the
system function is
M -1

M -1

n=0

n=0

H z = h n z -n = z - M -1 h n z M -1-n
which has M - 1 poles at the origin z = 0 (trivial poles) and M - 1 zeros located anywhere
in the z-plane. The frequency response function is
M -1

H e jw = h n e-jw ,

- p w

n=0

Type-1 Linear-Phase FIR Filters


Symmetrical impulse response and M odd. In this case 0 , M - 1 /2 is an integer,
and h n = h M - 1- n , 0 n M - 1 . Then we can show that

M -1 / 2

n=0

H e jw =

a n coswn

e -jw M -1 /2

where sequence a n is obtained from h n as


M - 1

a 0 = h

M -1
- n
2

a n = 2h
Since then, we have

the middle sample


,

1 n

M -3
2

H r w =

M -1 / 2

a n coswn
n=0

where H r w is an amplitude response function and not a magnitude response function.


function [Hr,w,a,L]=Hr_Type1(h);
% Computes Amplitude response Hr(w)of a Type-1 LP FIR filter
% ----------------------------------------------------------% [Hr,w,a,L]=Hr_Type1(h)
% Hr = Amplitude Response
% w = 500 frequencies between [0,pi] over which Hr is computed
% a = Type-1 LP filter coefficients
% L = Order of Hr
% h = Type-1 LP filter impulse response
%
M = length(h);
L= (M-1)/2;
a = [h(L+1) 2*h(L:-1:1)]; % 1x(L+1) row vector
n = [0:1:L]
% (L+1)x1 column vector
w = [0:1:500]'*pi/500;
Hr = cos(w*n)*a';

Type-2 Linear-Phase FIR Filters


Symmetrical impulse response and M even. In this case 0 , M - 1 /2 is not an
integer, and h n = h M - 1- n , 0 n M - 1 . Then we can show that

M /2

n=1

H e jw =

w n - e -jw M -1 /2
2

b n cos

where
M
- n
2

b n = 2h
Hence,

H r w =

M /2

n = 1,2,...,

b n cos
n=1

M
2

w n -
2

where H r w is an amplitude response function. Note that w = we get


H r w =

M /2

b n cos
n=1

n - 0
2

regardless of b n or h n . Hence we cannot use this type (i.e. symmetrical h n and M


even.) for highpass or bandstop filters.

Type-3 Linear-Phase FIR Filters


Antisymmetric impulse response and M odd. In this case = 2 , M - 1 /2 is an
integer, and h n = -h M - 1- n , 0 n M - 1 , and h M - 1 2 = 0 . Then we can show that
He

jw

M -1 / 2

n=1

-j 2 w M -1 /2
c n sinwn e

where
M -1
- n
2

c n = 2h
Hence,

H r w =
Note that at

w = 0 and w =

n = 1,2,...,

M -1
2

M -1 / 2

c n sinwn
n=1

we have H r w 0 , regardless of

c n or h n .

Furthermore, e j 2 = j , which means that jH r w is purely imaginary. Hence this type of


filter is not suitable for designing a lowpass or a highpass filter. However, this behavior is
suitable for approximating ideal Hilbert transformers and differentiators.
function [Hr,w,c,L]=Hr_Type3(h);
% Computes Amplitude response Hr(w) of a Type-3 LP FIR filter
% ----------------------------------------------------------% [Hr,w,c,L]=Hr_Type3(h)
% Hr = Amplitude Response
% w = frequencies between [0,pi] over which Hr is computed
% b = Type-3 LP filter coefficients
% L = Order of Hr
% h = Type-3 LP filter impulse response
%
M = length(h);
L = (M-1)/2;
c = [2*h(L+1:-1:1)];
n = [0:1:L];
w = [0:1:500]'*pi/500;
Hr = sin(w*n)*c';

Type-4 Linear-Phase FIR Filters


Antisymmetric impulse response and M even. This case is similar to Type-2. We have

M /2

n=1

H e jw =
where

w M -1 /2
2

j

1
d n sin w n - e
2

M
- n
2

d n = 2h
and

H r w =

M /2

d n sin

n = 1,2,...,

n=1

M
2

w n -
2

Note that at w = 0 and w = we have H r 0 0 and e j 2 = j . Hence, this type is also


suitable for designing digital Hilbert transformers and differentiators.
Assignments 1 2
1. MATLAB functions for Type-1 and Type-3 is given. Write a MATLAB function to
compute the amplitude response H r w and the location of the zeros of H z for Type-2
and Type-4.
2. Let
a) h n [ 4 1 1 2 5 6 5 2 1 1 4]
b) h n [4 1 1 2 5 6

6 5 2 1 1 4]

d) h n [ 4 1 1 2 5 6

6 5 2 1 1 4]

c) h n [4 1 1 2 5 0 5 2 1 1 4]

Determine the Type of the Linear-Phase FIR filters. Find the amplitude response H r w and
locations of the zeros of H z . Also plot the impulse response, amplitude response,
coefficients, and the zero locations.
WINDOW DESIGN TECHNIQUES
Summary of commonly used window function characteristics.
Window
Transition
Min. Stopband
Width w Exact
Name
Approximate
Attenuation
Values
4
1.8
Rectangular
21 dB
M
M
8
6.1
Bartlett
25 dB
M
M
8
6.2
Hanning
44 dB
M
M
8
6.6
Hamming
53 dB
M
M
12
11
Blackman
74 dB
M
M
MATLAB provides several routines to implement window functions discussed above table.

w = boxcar(n) returns the n-point rectangular window in array w.


w = triang(n) returns the n-point Bartlett (Triangular) window in array w.

w = hanning(n) returns the n-point symmetric Hanning window in a column


vector in array w .
w = hamming(n) returns the n-point symmetric Hamming window in a column
vector in array w.
w = blackman(n) returns the n-point symmetric Blackman window in a column
vector in array w.
w = kaiser(n,beta) returns the beta-valued n-point Kaiser window in array w.

Using these routines, we can use MATLAB to design FIR filters based on the window
technique, which also requires an ideal lowpass impulse response hd n as shown below.
function hd=ideal_lp(wc,M);
% Ideal LowPass filter computation
% -------------------------------% [hd] = ideal_lp(wc,M);
% hd = ideal impulse response between 0 to M-1
% wc = cutoff frequency in radians
% M = length of the ideal filter
%
alpha = (M-1)/2;
n = [0:1:(M-1)];
m = n - alpha +eps;
% add smallest number to avoi divided by zero
hd = sin(wc*m)./(pi*m);

To display the frequency domain plots of digital filters, MATLAB provides the freqz routine.
Using this routing, we can developed a modified version, called freqz_m, which returns the
magnitude response in absolute as well as dB scale, the phase response, and the group delay
response as shown below.
function [db,mag,pha,grd,w] = freqz_m(b,a)
% Modified version of freqz subroutine
% -----------------------------------% [db,mag,pha,grd,w] = freqz_m(b,a)
% db = relative magnitude in dB computed over 0 to pi radians
% mag = absolute magnitude computed over 0 to pi radians
% pha = Phase response in radians over 0 to pi radians
% grd = Group delay over 0 to pi radians
%
w = 501 frequency samples between 0 to pi radians
%
b = numerator polynomial of H(z)
(for FIR: b=h)
%
a = denominator polynomial of H(z)
(for FIR: a=[1])
%
[H,w] = freqz(b,a,1000,'whole') ;
H = (H(1:1:501))';
w = (w(1:1:501))';
mag = abs(H);
db = 20*log10((mag+eps)/max(mag));
pha = angle(H);
grd = grpdelay(b,a,w);

Assignment 3
3. Design a digital FIR lowpass filter with the following specifications:

wp = 0.2
ws = 0.3

R p = 0.25dB
As = 50dB

You might also like