(dsp.digitalfilter) - atmel digital filters with avr.pdf

(232 KB) Pobierz
AVR223: Digital Filters with AVR
AVR223: Digital Filters with AVR
Features
Implementations of Simple Digital Filters
Coefficient and Data Scaling
Fast Implementation of 2nd Order FIR Filter
Compact Implementation Of 8th Order FIR Filter
Fast Implementation of 2nd Order IIR Filter
Compact Implementation of 8th Order IIR Filter
8-bit
Microcontroller
Application
Note
Introduction
Applications involving processing of data from external analog sources (sensors)
often require some kind of digital filtering of the data prior to using the data to respond
to some external event. In applications where extremely high filter performance is
required Digital Signal Processors (DSP) are usually chosen, but in many applications
these are too expensive to use. In these cases 8- and 16-bit Microcontrollers (MCU)
comes into the picture: They are inexpensive, efficient and have all the required I/O
features and communication modules that the DSP often does not have.
The Atmel AVR microcontrollers are excellent for signal processing applications due to
their powerful architecture, strong instruction set and built-in multi-channel 10-bit Ana-
log to Digital Converter (ADC). The megaAVR ® series further have a hardware
multiplier, which is important in signal processing applications.
This document focus on the use of the AVR hardware multiplier, the use of the general
purpose registers for accumulator functionality, how to scale coefficients when imple-
menting algorithms on fixed point architectures, the actual implementation examples
and finally possible ways to optimize/modify the implementations suggested.
If the reader has a need for understanding digital filters, transfer functions, frequency
response, and other topics that are briefly mentioned here, most literature on the topic
of digital signal processing can provide this information. A literature list is enclosed
last in this document.
General Digital Filters
Two filter types are referred to when discussing digital filters, the Finite Impulse
Response (FIR) filters and Infinite Impulse Response (IIR) filters. The output of FIR fil-
ters is based on filter input only and the filter output is therefore calculated using a
non-recursive algorithm (feed-forward). The output of the IIR filter is based on both the
input and a feedback of the filter output. The IIR filter output is thus calculated using a
recursive algorithm.
The description of digital filters, covering both FIR and IIR filters, can be generalized
to the discrete differential equation seen in Equation 1.
Rev. 2527A–AVR–9/02
1
38144782.007.png 38144782.008.png
 
38144782.009.png
 
38144782.001.png 38144782.002.png
 
M
[ ]
N
[ ]
a
m
y
n
m
=
b
k
x
n
k
m
=
0
k
=
0
Equation 1
The filter coefficients related to the feedback and the feedforward part of the generalized
digital filter are respectively denoted by a m and b k . The filter input is x[n], where n refers
to the number of a given sample – similarly y[n] is the output corresponding to sample n.
The ranges of the summations M and N are referred to as the filter order or the filter
length.
The right side of the equation describes the feedforward in the filter function and the left
side the feedback in the filter function. A filter function can consist of either or both sides
of the differential equation. If only the right side of the differential equation is used, the
filter is a FIR filter (N 0 and M = 0). If both sides are used the filter is an IIR filter (N
0
0). A special case is when only the left side of the differential equation is used
(N = 0 and M
0). In that case the filter is called an “all-pole” filter. The all-pole filter is
actually also an IIR type filter since all filters that involve feedback are having an infinite
impulse response. In this document the IIR filter is however defined to be where ranges
of the two summations are equal (M = N). The all-pole IIR filter implementation will
therefore not be described separately in this context.
Often digital filters are described in the Z-domain. The Z-transform of Equation 1 is seen
from Equation 2.
M
[ ]
N
[ ] ()
M
()
N
a
y
n
m
=
b
x
n
k
Z
Y
z
a
z
m
=
X
z
b
z
k
m
k
m
k
m
=
0
k
=
0
m
=
0
k
=
0
Equation 2
The topic of Z-transforms is left for the reader to study further, recommended reading
could be [1]. For now it is sufficient to know that Z X in the Z-domain represent a delay
element and that the exponent determines the number of discrete delays generated.
The reason that the Z-transform of the differential equation is provided is that character-
istics of the digital filters are described by their transfer function, H(z), in the Z-domain.
The transfer function of a filter (or any system) describes the relation between the input
and output of the filter (or system). See Figure 1.
Figure 1. System
X
H(Z)
Y
The general form of the relation between the filter input and the filter output, the transfer
function of the filter, can be seen in Equation 3.
N
b
z
k
Y
(
z
)
k
()
H
z
=
=
k
=
0
X
(
z
)
M
a
z
m
m
m
=
0
Equation 3
The numerator describes the feedforward part of the filter function and the denominator
the feedback of the filter function.
2
AVR223
2527A–AVR–9/02
and M
38144782.003.png
AVR223
Filter Stability
From the transfer function of the filter it is possible to determine if the filter is stable. A fil-
terissaidtobestableifaboundedinputgenerates abounded output, meaning that the
output of the filter will die out when the filter input ceases. The criterion of stability is not
that the output becomes zero if the input becomes zero, but that the magnitude of the
output will decrease continuously toward zero (or becomes zero) if the input to the filter
becomes zero. Without elaborating on the topic of filter stability, it should be known that
the roots of the denominator polynomial, the poles that is, should be within the unit-circle
for the filter to be stable. Further information on the topic of filter stability can be found in
e.g. [1] and [2].
FIR Filters
FIR filters are characterized by being feed-forward filters, and there will thus only be an
output from the filter as long a data are fed into the filter input tap. The duration from the
input to the filter have ended to the output is “silent” (zero) depends on the order of the
filter, since the order of the filter determines the number of delay elements that would be
found in the filter tap delay line. An important feature of the FIR filter is that it is always
stable, since a bounded input generates a bounded output.
The FIR filter is the special case of the general digital filter, where M = 0 and N
0. The
mathematical description of the FIR filter can be seen in Equation 4.
M
[ ]
N
[ ]
a
m
y
n
m
=
b
k
x
n
k
;
for
M
=
0
and
N
0
m
=
0
k
=
[] [ ]
N
y
n
=
b
x
n
k
k
=
0
Equation 4
3
2527A–AVR–9/02
0
k
38144782.004.png
The transfer function of the FIR in the Z-domain is seen in Equation 5.
N
b
z
k
Y
(
z
)
k
N
()
H
z
=
=
k
=
0
=
b
z
k
X
(
z
)
1
k
k
=
0
Equation 5
From both Equation 4 and the FIR transfer function (Equation 5) it can be seen that the
filter output, y[n], sole rely on the input samples, x[n-k]. To make this even more obvious
one can study the illustration of the FIR filter structure, seen in Figure 2. This filter struc-
ture is referred to as “Direct Form I”.
Figure 2. FIR Filter, Direct Form I Structure
x[n]
Z -1
Z -1
Z -1
b 0
b 1
b 2
b n1
b n
y[n]
ThearrowsinthefilterstructureseeninFigure2representthedataflowinthefilter.
Boxes are functions, such as multiplications between signal and coefficients or discrete
signal delays.
The FIR filter can be used for both High Pass (HP) and Low Pass (LP) filter – this is a
matter of choosing the coefficient to match the desired frequency response. However,
the transfer function is only containing “zeros” – root of the numerator polynomial, and
since the zeros cause attenuation of the amplitude of signal filtered, the FIR is efficient
to attenuate selected frequencies. The FIR filters are good at removing undesired fre-
quency components of a signal. In Figure 3 a FIR filter magnitude response is
illustrated. From this figure it is obvious that this specific filter is attenuating the normal-
ized frequency (1) 0.5 by approximately 50 dB (by a factor 316). These filter coefficients
thus generates a notch filter.
4
AVR223
2527A–AVR–9/02
38144782.005.png
AVR223
Figure 3. Magnitude Response for FIR Filter, b K = {0.25, 0.25, 0.25, 0.25}
0
-20
-40
-60
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
Normalized Angular Frequency (x
π
rads/sample)
FIR filters are often used to smoothen signals and minimizing fluctuations in the signal
due to background noise. This can be done e.g., by using an averaging filter, where all
the coefficients equals 1/(N+1), where N is the filter order. By doing this the filter will pro-
duce the average of the last N+1 samples feed into the filter.
Note: 1. When working with discrete signals, frequencies are always normalized. When sam-
pling a signal it is not possible to get valid information about the frequencies above
half the sampling frequency, fs. Refer to Shannon's sampling theorem for more infor-
mation on this. The normalization is thus done relative to fs/2.
IIR Filters
The IIR filter is a combination of both feedforward and feedback. The IIR filter is more
efficient than the FIR filter, but have due to the feedback the disadvantage that the filter
can be unstable. Following common guidelines for designing IIR filters this is not in gen-
eral a problem however. If considering computation complexity with respect to the filter
order the IIR filter is more computational heavy than the FIR filter. An IIR filter is approx-
imately twice as complex as a FIR of the same order. The IIR filter is defined as the
general form of the digital filter, with the assumption N = M
0.
Often the scaling of the coefficients is made so that the a 0 coefficient equals 1. The filter
differential equation (Equation 1) can therefore be rearranged as follows from Equation
6. Presented in this way it is easier to understand what the filter output is generated
from.
M
[ ]
N
[ ]
a
m
y
n
m
=
b
x
n
k
;
for
a
0
=
1
m
=
0
k
=
0
[] [ ] [ ]
N
M
y
n
=
b
k
x
n
k
+
a
m
y
n
m
k
=
0
m
=
1
Equation 6
5
2527A–AVR–9/02
k
38144782.006.png
Zgłoś jeśli naruszono regulamin