Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 39

Chapter 14

Finite Impulse Response (FIR) Filters


Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 2
Learning Objectives
Introduction to the theory behind FIR
filters:
Properties (including aliasing).
Coefficient calculation.
Structure selection.
Implementation in Matlab, C, assembly
and linear assembly.
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 3
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.
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 4
Properties of an FIR Filter
Filter coefficients:
| | | |

=
=
1
0
N
k
k
k n x b n y
x[n] represents the filter input,
b
k
represents the filter coefficients,
y[n] represents the filter output,
N is the number of filter coefficients
(order of the filter).
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 5
Properties of an FIR Filter
Filter coefficients:
| | | |

=
=
1
0
N
k
k
k n x b n y
z
-1
+
z
-1
z
-1
+ +
z
-1
y(n)
x(n)
x x x x
b
0
b
1
b
2
b
N-1
FIR equation
Filter structure
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 6
Properties of an FIR Filter
Filter coefficients:
| | | |

=
=
1
0
N
k
k
k n x b n y
If the signal x[n] is replaced by an impulse
o[n] then:
| | | |

=
=
1
0
N
k
k
k n b n y o
| | | | | | | | N b b b y
k
+ + + = o o o 1 0 0
1 0
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 7
Properties of an FIR Filter
Filter coefficients:
| | | |

=
=
1
0
N
k
k
k n x b n y
If the signal x[n] is replaced by an impulse
o[n] then:
| | | |

=
=
1
0
N
k
k
k n b n y o
| | | | | | | | N n b n b n b n y
k
+ + + = o o o 1
1 0
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 8
Properties of an FIR Filter
Filter coefficients:
| | | |

=
=
1
0
N
k
k
k n x b n y
If the signal x[n] is replaced by an impulse
o[n] then:
| | | |

=
=
1
0
N
k
k
k n b n y o
| |

=
=
=
k n for 0
k n for 1
k n o
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 9
Properties of an FIR Filter
Filter coefficients:
| | | |

=
=
1
0
N
k
k
k n x b n y
Finally:
| |
| |
| | k h b
h b
h b
k
=
=
=

1
0
1
0
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 10
Properties of an FIR Filter
Filter coefficients:
| | | |

=
=
1
0
N
k
k
k n x b n y
With:
| | k h b
k
=
The coefficients of a filter are the same as
the impulse response samples of the filter.
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 11
Frequency Response of an FIR Filter
By taking the z-transform of h[n], H(z):
Replacing z by e
je
in order to find the
frequency response leads to:
( ) | |

=
1
0
N
n
n
z n h z H
( ) ( ) | |

=
= =
1
0
N
n
jn j
e z
e n h e H z H
j
e e
e
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 12
Frequency Response of an FIR Filter
Since e
-j2tk
= 1 then:
Therefore:
( ) | |
( )
| |


=

=
+
=
= =
+
1
0
1
0
2
2
N
n
jn
N
n
jn
e z
e n h e n h z H
e t e
t e
( ) ( )
e t e j k j
e H e H =
+2
FIR filters have a periodic frequency
response and the period is 2t.
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 13
Frequency Response of an FIR Filter
Frequency
response:
FIR y[n] x[n]
F
s
/2 F
s
/2
Freq
Freq
x
[
n
]

y
[
n
]

( ) ( )
e t e j k j
e H e H =
+2
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 14
Frequency Response of an FIR Filter
Solution: Use an anti-aliasing filter.
FIR y[n]
x[n]
ADC
Analogue
Anti-Aliasing
x(t)
F
s
/2 F
s
/2
Freq
Freq
x
(
t
)

y
[
n
]

Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 15
Phase Linearity of an FIR Filter
A causal FIR filter whose impulse
response is symmetrical is guaranteed to
have a linear phase response.
0
n
h(n)
1 n n+1 2n+1 2n
N = 2n + 2
0
n
h(n)
1 n n+1 2n 2n-1 n-1
N = 2n + 1
Even symmetry Odd symmetry
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 16
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.
Condition Phase |
.
|

\
|
=
2
1 N
k Phase Property Filter Type
| | | | 1 = n N h n h
Positive Symmetry
e k
Linear phase
Odd Symmetry Type 1
Even Symmetry Type 2
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 17
Phase Linearity of an FIR Filter
Application of 90 linear phase shift:
Signal
separation
90
o
delay

90
o
delay

+
+
+
-
I
Q
Reverse
Forward
IH
QH
t B t A I
r f
e e sin cos + =
t B t A Q
r f
e e cos sin + =
t B t A
t B t A IH
r f
r f
e e
t
e
t
e
cos sin
2
sin
2
cos
+ =
|
.
|

\
|
+ + |
.
|

\
|
+ =
t B Q IH
r
e cos 2 = +
t B I QH
f
e sin 2 =
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 18
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.
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 19
Filter Specification - Step 1
(a)
1
f (norm)
fc : cut-of f f requency
pass-band stop-band
pass-band stop-band transition band
1
s
o
pass-band
ripple
stop-band
ripple
fpb : pass-band f requency
fsb : stop-band f requency
f (norm)
(b)
p
1 o +
s
A
p
A
0
-3
p
1 o
fs/2
fc : cut-of f f requency
fs/2
|H(f )|
(dB)
|H(f )|
(linear)
|H(f )|
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 20
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.
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 21
Window Method
First stage of this method is to calculate
the coefficients of the ideal filter.
This is calculated as follows:
( ) ( )
( )

=
=
=
=
=
}
}

0 n for
0 n for
2
sin 2
1
2
1
2
1
c
c
c c
n j
n j
d
f
n
n f
d e
d e H n h
c
c
e
e
e
t
e e
t
e
e
e
e
t
t
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 22
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.
Window Type
Normalised Transition
Width (Af(Hz))
Passband Ripple(dB)
Stopband Attenuation
(dB)
Rectangular
N
9 . 0
0.7416 21
Hanning
N
1 . 3
0.0546 44
Hamming
N
3 . 3
0.0194 53
Blackman
N
5 . 5
0.0017 74
Kaiser
54 . 4
93 . 2
= |
N
96 . 8
71 . 5
= |
N
0.0274
0.000275
50
90
( )
132 8
4 . 1 2 . 1
3 . 3 3 . 3
=

=
A
= kHz
kHz f
N
Using the Hamming
Window:
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 23
Window Method
The third stage is to calculate the set of
truncated or windowed impulse response
coefficients, h[n]:
( ) ( ) ( ) n W n h n h
d
=
even N for
odd N for
2 2
2
1
2
1
=
=
s s

s s

N
n
N
N
n
N
( )

133
2
cos 46 . 0 54 . 0

2
cos 46 . 0 54 . 0
|
.
|

\
|
+ =
|
.
|

\
|
+ =
n
N
n
n W
t
t
for
Where:
66 66 s s n
for
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 24
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]);
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 25
Window Method
0 0.5 1 1.5 2
x 10
4
-6000
-4000
-2000
0
Frequency (Hz)
P
h
a
s
e

(
d
e
g
r
e
e
s
)
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
x 10
4
-60
-40
-20
0
Frequency (Hz)
M
a
g
n
i
t
u
d
e

(
d
B
)
0 20 40 60 80 100 120 140
-0.1
0
0.1
0.2
0.3
0.4
Coefficient number, n
h
(
n
)

Truncated Impulse Response
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 26
Realisation Structure Selection - Step 3
( )

=
1
0
N
k
k
k
z b z H
( ) ( ) ( ) z X z H z Y =
( ) ( ) ( ) ( ) 1 .... 1
1 1 0
+ + + + =

N n x b n x b n x b n y
N
z
-1
z
-1
z
-1
+ + +
b
0
b
2
b
N-1
y(n)
x(n)
b
1
Direct form structure for an FIR filter:
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 27
Realisation Structure Selection - Step 3
( )

=
1
0
N
k
k
k
z b z H
Linear phase structures:

N even:


N Odd:
( ) ( )

=

+ =
1
2
0
1
N
k
k N k
k
z z b z H
( ) ( )


+ + =
2
1
0
2
1
2
1
1
N
k
N
N
k N k
k
z b z z b z H
Direct form structure for an FIR filter:
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 28
Realisation Structure Selection - Step 3
(a) N even.
(b) N odd.
+
b
0
+
+
+
+
b
1
+
b
2
+
b
N/2-1
y (n)
(a)
z
-1
z
-1
z
-1
z
-1
z
-1
+
b
0
+
+
+
+
b
1
+
b
2
+
b
(N-3)/2
y (n)
x(n)
b
(N-1)/2
+
(b)
z
-1
z
-1
z
-1
z
-1
z
-1
z
-1
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 29
Realisation Structure Selection - Step 3
( )

=
1
0
N
k
k
k
z b z H
Cascade structures:
( )
( )
( )
( )
[

+ + =
(

+ + + + =
+ + + + = =
M
k
k k
N N
N
N
N
k
k
k
z b z b b
z
b
b
z
b
b
z
b
b
b
z b z b z b b z b z H
1
2
2 ,
1
1 , 0
1
0
1 2
0
2 1
0
1
0
1
1
2
2
1
1 0
1
0
1
... 1
...
Direct form structure for an FIR filter:
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 30
Realisation Structure Selection - Step 3
( )

=
1
0
N
k
k
k
z b z H
Cascade structures:
z
-1
+
b
1,1
x(n)
z
-1
+
b
1,2
z
-1
+
b
2,1
z
-1
+
b
2,2
z
-1
+
b
M,1
z
-1
+
b
M,2
y (n)
b
0
Direct form structure for an FIR filter:
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 31
Implementation - Step 5
Implementation procedure in C with
fixed-point:
Set up the codec (\Links\CodecSetup.pdf).

Transform: to C code.

(\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).
| | | |

=
=
1
0
N
k
k
k n x b n y
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 32
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
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 33
Implementation - Step 5
Implementation procedure in assembly:
Same set up as fixed-point, however:
is written in assembly.

(\Links\FIRFixedAsm.pdf)

The ISR is now declared as external.

| | | |

=
=
1
0
N
k
k
k n x b n y
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 34
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:

Set the initial value of the circular pointers,
see \Links\FIRFixedAsm.pdf.
#pragma DATA_ALIGN (symbol, constant (bytes))
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 35
Implementation - Step 5
y0 = b0*x0 + b1*x1 + b2*x2
+ b3*x3
Circular addressing link slide.
time
y
[
n
]

0 1 2
b
0
b
1
b
2
b
3
x
0
x
1
x
2
x
3
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 36
x
4
x
1
x
2
x
3
Implementation - Step 5
y0 = b0*x0 + b1*x1 + b2*x2
+ b3*x3
y1 = b0*x4 + b1*x1
+ b3*x3
+ b2*x2
Circular addressing link slide.
time
y
[
n
]

0 1 2
b
0
b
1
b
2
b
3
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 37
x
4
x
5
x
2
x
3
Implementation - Step 5
y0 = b0*x0 + b1*x1 + b2*x2
+ b3*x3
y1 = b0*x4 + b1*x1
+ b3*x3
+ b2*x2
y2 = b0*x4 + b1*x5
+ b3*x3
+ b2*x2
Circular addressing link slide.
time
y
[
n
]

0 1 2
b
0
b
1
b
2
b
3
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004 Chapter 14, Slide 38
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
Finite Impulse Response (FIR) Filters
- End -

You might also like