Chapter 14
Finite Impulse Response (FIR) Filters
Learning Objectives
Introduction to the theory behind FIR
filters:
Chapter 14, Slide 2
Properties (including aliasing).
Coefficient calculation.
Structure selection.
Implementation in Matlab, C, assembly
and linear assembly.
Dr. Naim
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
Properties of an FIR Filter
Filter coefficients:
N 1
y n bk x n k
k 0
x[n]
bk
y[n]
N
Chapter 14, Slide 4
represents the filter input,
represents the filter coefficients,
represents the filter output,
is the number of filter coefficients
(order of the filter).
Dr. Naim
Properties of an FIR Filter
Filter coefficients:
N 1
y n bk x n k
k 0
x(n)
-1
-1
-1
b2
FIR equation
z
-1
N-1
y(n)
Filter structure
Chapter 14, Slide 5
Dr. Naim
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:
y n
N 1
b n k
k
k 0
y 0 b0 0 b1 1 bk N
Chapter 14, Slide 6
Dr. Naim
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:
y n
N 1
b n k
k
k 0
y n b0 n b1 n 1 bk n N
Chapter 14, Slide 7
Dr. Naim
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:
y n
N 1
b n k
k
k 0
1 for n k
n k
0 for n k
Chapter 14, Slide 8
Dr. Naim
Properties of an FIR Filter
Filter coefficients:
N 1
y n bk x n k
k 0
Finally:
b0 h 0
b1 h1
bk h k
Chapter 14, Slide 9
Dr. Naim
Properties of an FIR Filter
Filter coefficients:
N 1
y n bk x n k
k 0
With:
Chapter 14, Slide 10
bk h k
The coefficients of a filter are the same as
the impulse response samples of the filter.
Dr. Naim
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
Chapter 14, Slide 11
n 0
Dr. Naim
Frequency Response of an FIR Filter
Since e-j2k = 1 then:
N 1
N 1
n 0
n 0
H z z e 2 h n e jn 2 h n e jn
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
Frequency Response of an FIR Filter
Frequency
response:
H e j 2 k H e j
FIR
y[n]
x[n]
y[n]
x[n]
Freq
Fs/2
Chapter 14, Slide 13
Freq
Fs/2
Dr. Naim
Frequency Response of an FIR Filter
Solution: Use an anti-aliasing filter.
x(t)
ADC
x[n]
FIR
y[n]
y[n]
x(t)
Analogue
Anti-Aliasing
Fs/2
Chapter 14, Slide 14
Freq
Fs/2
Freq
Dr. Naim
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
0
n+1
2n
2n+1
Even symmetry
Chapter 14, Slide 15
n
0
n-1
n+1
2n-1
2n
Odd symmetry
Dr. Naim
Phase Linearity of an FIR Filter
A causal FIR filter whose impulse response is symmetrical (ie h[n] = h[N-1n] for n = 0, 1, , N-1) is guaranteed to have a linear phase response.
Condition
h n h N n 1
Positive Symmetry
Chapter 14, Slide 16
Phase k
N 1
Phase Property
Filter Type
Odd Symmetry Type 1
Linear phase
Even Symmetry Type 2
Dr. Naim
Phase Linearity of an FIR Filter
Application of 90 linear phase shift:
I
Signal separation
Q
I A cos f t B sin r t
Q A sin f t B cos r t
Chapter 14, Slide 17
90
IH
delay
delay
90o
QH
IH A cos f t B sin r t
2
2
A sin f t B cos r t
Reverse
Forward
IH Q 2 B cos r t
QH I 2 B sin f t
Dr. Naim
Design Procedure
To fully design and implement a filter five steps are required:
(1)
(2)
(3)
(4)
(5)
Filter specification.
Coefficient calculation.
Structure selection.
Simulation (optional).
Implementation.
Chapter 14, Slide 18
Dr. Naim
Filter Specification - Step 1
|H(f)|
pass-band
stop-band
f c : cut-off frequency
f(norm)
f s /2
(a)
|H(f)|
(dB)
pass-band
transition band
|H(f)|
(linear)
stop-band
1 p
1
1 p
pass-band
ripple
-3
stop-band
ripple
s
f sb : stop-band frequency
f c : cut-off frequency
f pb : pass-band frequency
f s /2
f(norm)
(b)
Chapter 14, Slide 19
Dr. Naim
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
Window Method
First stage of this method is to calculate the coefficients of the ideal filter.
This is calculated as follows:
1
hd n
2
1
2
H e jn d
1 e jn d
2 f c sin n c
n c
2 fc
Chapter 14, Slide 21
for n 0
for n 0
Dr. Naim
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
Width (f(Hz))
Passband Ripple(dB)
Stopband Attenuation
(dB)
Rectangular
0.9
N
0.7416
21
Hanning
3.1
N
0.0546
44
Hamming
3.3
N
0.0194
53
Blackman
5.5
N
0.0017
74
0.0274
50
0.000275
90
Window Type
Kaiser
2.93
4.54
N
5.71
8.96
N
Using the Hamming
Window:
Chapter 14, Slide 22
3.3
3.3
8kHz 132
f 1.2 1.4 kHz
Dr. Naim
Window Method
The third stage is to calculate the set of truncated or windowed impulse
response coefficients, h[n]:
h n hd n W n
Where:
Chapter 14, Slide 23
for
N 1
N 1
n
for N odd
2
2
N
N
for N even
n
2
2
W n 0.54 0.46 cos
0.54 0.46 cos
2n
N
2n
133
for
66 n 66
Dr. Naim
Window Method
Matlab code for calculating coefficients:
close all;
clear all;
fc = 8000/44100;
N = 133;
n = -((N-1)/2):((N-1)/2);
n = n+(n==0)*eps;
% cut-off frequency
% number of taps
[h] = sin(n*2*pi*fc)./(n*pi);
[w] = 0.54 + 0.46*cos(2*pi*n/N);
d = h.*w;
% generate sequence of ideal coefficients
% generate window function
% 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)));
axis([0 2*10^4 -70 10]);
% plot transfer function
figure(2);
stem(d);
xlabel('Coefficient number');
ylabel ('Value');
title('Truncated Impulse Response');
figure(3)
freqz(d,1,512,44100);
axis([0 2*10^4 -70 10]);
Chapter 14, Slide 24
% avoiding division by zero
% plot coefficient values
% use freqz to plot magnitude and phase response
Dr. Naim
Window Method
Truncated Impulse Response
0.4
0.3
h(n)
0.2
0.1
0
Magnitude (dB)
-0.1
20
0.6
0.8
1
1.2
Frequency (Hz)
100
120
140
-20
-40
-60
0.2
0.4
0
Phase (degrees)
60
80
Coefficient number, n
1.4
1.6
1.8
2
4
x 10
-2000
-4000
-6000
Chapter 14, Slide 25
40
0.5
1
Frequency (Hz)
1.5
2
4
x 10
Dr. Naim
Realisation Structure Selection - Step 3
Direct form structure for an FIR filter:
N 1
H z bk z k
k 0
y n b0 x n b1 x n 1 .... bN 1 x n N 1
Y z H z X z
x(n)
-1
Chapter 14, Slide 26
-1
-1
N-1
y(n)
Dr. Naim
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 even:
N Odd:
N
1
2
H z bk z k z N k 1
k 0
N 1
2
H z bk z k z N k 1 b N 1 z
k 0
Chapter 14, Slide 27
N 1
2
Dr. Naim
Realisation Structure Selection - Step 3
(a) N even.
(b) N odd.
-1
-1
-1
-1
-1
b0
b1
b2
N/2-1
+
+
y(n)
(a)
x(n)
-1
-1
-1
-1
-1
-1
b0
b1
b2
+
+
b(N-1)/2
+
y(n)
(N-3)/2
+
(b)
Chapter 14, Slide 28
Dr. Naim
Realisation Structure Selection - Step 3
Direct form structure for an FIR filter:
N 1
H z bk z k
k 0
Cascade structures:
H z
N 1
k 0
bk z k b0 b1 z 1 b2 z 2 ... bN 1 z N 1
bN 1 N 1
b1 1 b2 2
b0 1 z z ...
z
b
b
b
0
0
0
b0
1 b
M
k 1
Chapter 14, Slide 29
1
2
z
b
z
k ,1
k ,2
Dr. Naim
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)
+
z -1
z -1
1,1
z -1
1,2
+
z -1
2,1
z -1
b
Chapter 14, Slide 30
M,1
y(n)
z -1
b
2,2
M,2
Dr. Naim
Implementation - Step 5
Implementation procedure in C with fixed-point:
Set up the codec (\Links\
[Link]).
).
(\Links\[Link]
Transform:
(\Links\[Link]
\Links\[Link]))
Configure timer 1 to generate an interrupt at 8000Hz (\Links\
[Link]).
).
(\Links\[Link]
Set the interrupt generator to generate an interrupt to invoke the Interrupt Service Routine (ISR) (\Links\
[Link]).
).
(\Links\[Link]
to C code.
N 1
y n bk x n k
k 0
Chapter 14, Slide 31
Dr. Naim
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\[Link]
\Links\[Link]
Chapter 14, Slide 32
Dr. Naim
Implementation - Step 5
Implementation procedure in assembly:
Same set up as fixed-point, however:
is written in assembly.
(\Links\[Link]
\Links\[Link]))
The ISR is now declared as external.
N 1
y n bk x n k
k 0
Chapter 14, Slide 33
Dr. Naim
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\[Link]
\Links\[Link]..
#pragma DATA_ALIGN (symbol, constant (bytes))
Chapter 14, Slide 34
Dr. Naim
Implementation - Step 5
b0
b1
b2
b3
+ b1*x1 + b2*x2 + b3*x3
y[n]
y0 = b0*x0
x0
x1
x2
x3
time
Circular addressing link slide.
Chapter 14, Slide 35
Dr. Naim
Implementation - Step 5
b0
b1
b2
b3
y0 = b0*x0
+ b1*x1 + b2*x2 + b3*x3
+ b1*x1 + b2*x2 + b3*x3
y[n]
y1 = b0*x4
x4
x1
x2
x3
time
Circular addressing link slide.
Chapter 14, Slide 36
Dr. Naim
Implementation - Step 5
b0
b1
b2
b3
y0 = b0*x0
+ b1*x1 + b2*x2 + b3*x3
+ b1*x1 + b2*x2 + b3*x3
+ b1*x5 + b2*x2 + b3*x3
y[n]
y1 = b0*x4
y2 = b0*x4
x4
x5
x2
x3
time
Circular addressing link slide.
Chapter 14, Slide 37
Dr. Naim
FIR Code
Code location:
Projects:
Fixed Point in C:
Floating Point in C:
Fixed Point in Assembly:
Floating Point in Assembly: \FIR_Asm_Float\
Chapter 14, Slide 38
Code\Chapter 14 - Finite Impulse Response Filters
\FIR_C_Fixed\
\FIR_C_Float\
\FIR_Asm_Fixed\
Dr. Naim
Chapter 14
Finite Impulse Response (FIR) Filters
- End -