You are on page 1of 39

Chapter 14

Finite Impulse Response (FIR) Filters


Learning Objectives
 Introduction to the theory behind FIR
filters:
 Properties (including aliasing).
 Coefficient calculation.
 Structure selection.
 Implementation in Matlab, C, assembly
and linear assembly.

Chapter 14, Slide 2 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Introduction
 Amongst all the obvious advantages that
digital filters offer, the FIR filter can
guarantee linear phase characteristics.
 Neither analogue or IIR filters can achieve
this.
 There are many commercially available
software packages for filter design.
However, without basic theoretical
knowledge of the FIR filter, it will be
difficult to use them.

Chapter 14, Slide 3 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Properties of an FIR Filter
 Filter coefficients:
N 1
yn   bk  xn  k 
k 0

x[n] represents the filter input,


bk represents the filter coefficients,
y[n] represents the filter output,
N is the number of filter coefficients
(order of the filter).

Chapter 14, Slide 4 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Properties of an FIR Filter
 Filter coefficients:
N 1
yn   bk  xn  k  FIR equation
k 0

-1 -1 -1 -1
x(n) z z z z

b0 b1 b2 bN-1
x x x x

+ + + y(n)

Filter structure
Chapter 14, Slide 5 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Properties of an FIR Filter
 Filter coefficients:
N 1
yn   bk  xn  k 
k 0

 If the signal x[n] is replaced by an impulse


[n] then:
N 1
yn   bk  n  k 
k 0

y0  b0 0  b1  1    bk   N 

Chapter 14, Slide 6 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Properties of an FIR Filter
 Filter coefficients:
N 1
yn   bk  xn  k 
k 0

 If the signal x[n] is replaced by an impulse


[n] then:
N 1
yn   bk  n  k 
k 0

yn  b0 n  b1 n  1    bk  n  N 

Chapter 14, Slide 7 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Properties of an FIR Filter
 Filter coefficients:
N 1
yn   bk  xn  k 
k 0

 If the signal x[n] is replaced by an impulse


[n] then:
N 1
yn   bk  n  k 
k 0

1 for n  k
 n  k   
0 for n  k

Chapter 14, Slide 8 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Properties of an FIR Filter
 Filter coefficients:
N 1
yn   bk  xn  k 
k 0

 Finally:
b0  h0
b1  h1

bk  hk 

Chapter 14, Slide 9 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Properties of an FIR Filter
 Filter coefficients:
N 1
yn   bk  xn  k 
k 0

With: bk  hk 

 The coefficients of a filter are the same as


the impulse response samples of the filter.

Chapter 14, Slide 10 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Frequency Response of an FIR Filter
 By taking the z-transform of h[n], H(z):
N 1
H  z    hnz  n
n 0

 Replacing z by ej in order to find the


frequency response leads to:

 
N 1
H  z  z e j  H e j   hne  jn
n 0

Chapter 14, Slide 11 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Frequency Response of an FIR Filter
 Since e-j2k = 1 then:
N 1 N 1
H z  z e 2   hne  jn  2   hne  jn
n 0 n 0

 Therefore:

   
H e j 2 k  H e j

 FIR filters have a periodic frequency


response and the period is 2.

Chapter 14, Slide 12 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Frequency Response of an FIR Filter
 Frequency
response:
   
H e j 2k  H e j

x[n] FIR y[n]


x[n]

y[n]
Freq

Freq
Fs/2 Fs/2

Chapter 14, Slide 13 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Frequency Response of an FIR Filter
 Solution: Use an anti-aliasing filter.
x[n]
x(t) ADC FIR y[n]
Analogue
Anti-Aliasing

y[n]
x(t)

Freq Freq
Fs/2 Fs/2

Chapter 14, Slide 14 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Phase Linearity of an FIR Filter
 A causal FIR filter whose impulse
response is symmetrical is guaranteed to
have a linear phase response.

h(n) h(n)
N = 2n + 2 N = 2n + 1

n n
0 1 n n+1 2n 2n+1 0 1 n-1 n n+1 2n-1 2n

Even symmetry Odd symmetry

Chapter 14, Slide 15 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Phase Linearity of an FIR Filter
 A causal FIR filter whose impulse
response is symmetrical (ie h[n] = h[N-1-n]
for n = 0, 1, …, N-1) is guaranteed to have
a linear phase response.

 N 1 
Condition Phase  k    Phase Property Filter Type
 2 

hn   hN  n  1 Odd Symmetry – Type 1


k Linear phase
Positive Symmetry Even Symmetry – Type 2

Chapter 14, Slide 16 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Phase Linearity of an FIR Filter
 Application of 90° linear phase shift:
o IH
I 90 +
Reverse
delay +
Signal
separation delay -
Forward
Q 90o +
QH

I  A cos f t  B sin r t     IH  Q  2 B cosr t


IH  A cos  f t    B sin r t  
 2  2
Q  Asin  f t  B cosr t   A sin  f t  B cosr t QH  I  2B sin  f t

Chapter 14, Slide 17 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Design Procedure
 To fully design and implement a filter five
steps are required:
(1) Filter specification.
(2) Coefficient calculation.
(3) Structure selection.
(4) Simulation (optional).
(5) Implementation.

Chapter 14, Slide 18 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Filter Specification - Step 1
|H(f)| pass-band stop-band

fc : cut-off frequency fs/2 f(norm)


(a)

|H(f)| pass-band transition band stop-band |H(f)|


(dB) (linear)
p 1   p

0 1
1   p

pass-band
-3
ripple

stop-band
ripple
s  s

fs/2 f(norm)
fsb : stop-band frequency
fc : cut-off frequency
fpb : pass-band frequency
(b)

Chapter 14, Slide 19 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Coefficient Calculation - Step 2
 There are several different methods
available, the most popular are:
 Window method.
 Frequency sampling.
 Parks-McClellan.
 We will just consider the window method.

Chapter 14, Slide 20 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Window Method
 First stage of this method is to calculate
the coefficients of the ideal filter.
 This is calculated as follows:


hd n   H  e jn d
1
2 

c
1

2  1  e jn d
 c

 2 f c sin n c 
 for n  0
 n c
 2 fc for n  0

Chapter 14, Slide 21 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Window Method
 Second stage of this method is to select a window
function based on the passband or attenuation
specifications, then determine the filter length based on
the required width of the transition band.

Normalised Transition Stopband Attenuation


Window Type Passband Ripple(dB)
Width (f(Hz)) (dB)

0.9
Rectangular 0.7416 21
N

3.1
Hanning N
0.0546 44

3.3
Hamming N 0.0194 53

5.5
Blackman N 0.0017 74

2.93
   4.54
N 0.0274 50
Kaiser 5.71
   8.96
N 0.000275 90

Using the Hamming N


3.3

3.3
 8kHz  132
Window: f 1.2  1.4kHz
Chapter 14, Slide 22 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Window Method
 The third stage is to calculate the set of
truncated or windowed impulse response
coefficients, h[n]:
N 1 N 1
 n for N  odd
hn  hd nW n for 2 2
N
 n
N for N  even
2 2

 2n 
Where: W n   0.54  0.46 cos 
 N 
for 66  n  66
 2n 
 0.54  0.46 cos 
 133 

Chapter 14, Slide 23 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Window Method
 Matlab code for calculating coefficients:
close all;
clear all;

fc = 8000/44100; % cut-off frequency


N = 133; % number of taps
n = -((N-1)/2):((N-1)/2);
n = n+(n==0)*eps; % avoiding division by zero

[h] = sin(n*2*pi*fc)./(n*pi); % generate sequence of ideal coefficients


[w] = 0.54 + 0.46*cos(2*pi*n/N); % generate window function
d = h.*w; % window the ideal coefficients

[g,f] = freqz(d,1,512,44100); % transform into frequency domain for plotting

figure(1)
plot(f,20*log10(abs(g))); % plot transfer function
axis([0 2*10^4 -70 10]);

figure(2);
stem(d); % plot coefficient values
xlabel('Coefficient number');
ylabel ('Value');
title('Truncated Impulse Response');

figure(3)
freqz(d,1,512,44100); % use freqz to plot magnitude and phase response
axis([0 2*10^4 -70 10]);

Chapter 14, Slide 24 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Window Method
Truncated Impulse Response
0.4

0.3

0.2

h(n)
0.1

-0.1
0 20 40 60 80 100 120 140
Coefficient number, n

0
Magnitude (dB)

-20

-40

-60

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2


Frequency (Hz) 4
x 10
0
Phase (degrees)

-2000

-4000

-6000
0 0.5 1 1.5 2
Frequency (Hz) 4
x 10
Chapter 14, Slide 25 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Realisation Structure Selection - Step 3
 Direct form structure for an FIR filter:
N 1
H z    bk z  k
k 0

Y z   H z   X z  yn  b0 xn  b1 xn  1  ....  bN 1 xn  N  1

-1 -1 -1
x(n) z z z

b b b b
0 1 2 N-1

+ + + y(n)

Chapter 14, Slide 26 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Realisation Structure Selection - Step 3
 Direct form structure for an FIR filter:
N 1
H z    bk z  k
k 0

 Linear phase structures:


N
1

 
2
 N even: H z    bk z  k  z N  k 1
k 0

N 1
N 1
 
2 
 N Odd: H z    bk z  k  z N  k 1  b N 1 z 2

k 0 2

Chapter 14, Slide 27 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Realisation Structure Selection - Step 3
-1 -1 -1 -1 -1

(a) N even. z z z z z

+
b0

b1

(b) N odd. +
+
b2
+

b N/2-1
+ +
y(n)
(a)

x(n) -1 -1 -1 -1 -1 -1
z z z z z z
b0
+
b1
+ +
b2
+ +
b(N-1)/2

b(N-3)/2
+ +

y(n) +

(b)

Chapter 14, Slide 28 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Realisation Structure Selection - Step 3
 Direct form structure for an FIR filter:
N 1
H z    bk z  k
k 0

 Cascade structures:
N 1
H z    bk z  k  b0  b1 z 1  b2 z  2  ...  bN 1 z   N 1
k 0

 b b b 
 b0 1  1 z 1  2 z  2  ...  N 1 z   N 1 
 b0 b0 b0 

 
M
 b0  1  bk ,1 z 1  bk , 2 z  2
k 1

Chapter 14, Slide 29 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Realisation Structure Selection - Step 3
 Direct form structure for an FIR filter:
N 1
H z    bk z  k
k 0

 Cascade structures:

x(n) b0 y(n)
+ + +

z -1 z -1 z -1
b 1,1 b 2,1 b M,1
+ + +
z -1 z -1 z -1
b 1,2 b 2,2 b M,2

Chapter 14, Slide 30 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Implementation - Step 5
 Implementation procedure in ‘C’ with
fixed-point:
 Set up the codec (\Links\CodecSetup.pdf).

 Transform: N 1
yn   bk  xn  k  to ‘C’ code.
k 0

(\Links\FIRFixed.pdf)
 Configure timer 1 to generate an interrupt at
8000Hz (\Links\TimerSetup.pdf).
 Set the interrupt generator to generate an
interrupt to invoke the Interrupt Service
Routine (ISR) (\Links\InterruptSetup.pdf).
Chapter 14, Slide 31 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Implementation - Step 5
 Implementation procedure in ‘C’ with
floating-point:
Same set up as fixed-point plus:
 Convert the input signal to floating-point
format.
 Convert the coefficients to floating-point
format.
 With floating-point multiplications there is
no need for the shift required when using
Q15 format.
 See \Links\FIRFloat.pdf

Chapter 14, Slide 32 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Implementation - Step 5
 Implementation procedure in assembly:
Same set up as fixed-point, however:
 N 1
yn   bk  xn  k 
is written in assembly.
k 0

(\Links\FIRFixedAsm.pdf)

 The ISR is now declared as external.

Chapter 14, Slide 33 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Implementation - Step 5
 Implementation procedure in assembly:
The filter implementation in assembly is
now using circular addressing and
therefore:
 The circular pointers and block size register
are selected and initialised by setting the
appropriate values of the AMR bit fields.
 The data is now aligned using:
#pragma DATA_ALIGN (symbol, constant (bytes))

 Set the initial value of the circular pointers,


see \Links\FIRFixedAsm.pdf.

Chapter 14, Slide 34 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Implementation - Step 5

b0 x0
b1 x1
b2 x2
b3 x3

y0 = b0*x0 + b1*x1 + b2*x2 + b3*x3


y[n]

time
0 1 2
Circular addressing link slide.
Chapter 14, Slide 35 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Implementation - Step 5

b0 x4
b1 x1
b2 x2
b3 x3

y0 = b0*x0 + b1*x1 + b2*x2 + b3*x3


y1 = b0*x4 + b1*x1 + b2*x2 + b3*x3
y[n]

time
0 1 2
Circular addressing link slide.
Chapter 14, Slide 36 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Implementation - Step 5

b0 x4
b1 x5
b2 x2
b3 x3

y0 = b0*x0 + b1*x1 + b2*x2 + b3*x3


y1 = b0*x4 + b1*x1 + b2*x2 + b3*x3
y2 = b0*x4 + b1*x5 + b2*x2 + b3*x3
y[n]

time
0 1 2
Circular addressing link slide.
Chapter 14, Slide 37 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
FIR Code
 Code location:
 Code\Chapter 14 - Finite Impulse Response Filters
 Projects:
 Fixed Point in C: \FIR_C_Fixed\
 Floating Point in C: \FIR_C_Float\
 Fixed Point in Assembly: \FIR_Asm_Fixed\
 Floating Point in Assembly: \FIR_Asm_Float\

Chapter 14, Slide 38 Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Chapter 14
Finite Impulse Response (FIR) Filters
- End -

You might also like