where : ibrtses embedded
averaging data streams
The mathematical average is defined to be the sum over a set divided by the number of members. This concept is poorly suited for continous applications. Alternatively, summing over the last N members proves too time- and memory consuming. True alternatives are the lowpass filter and exponential averaging, both described and compared here.
exponential average
Exponential average is a quick way to average datastreams in realtime.
Assume a data stream (coming from the left) being weighted with N being between 0 and 1,
being added with the stored value weighted with the reminder 1-N.
For sufficiently small values of N, the output to the right shows characteristics of an average value. Each input value decays in the storage with an exponential.
the implementation
The floating point implementation looks like
output:=input*N+output*(1-N);
As can be seen each input value requires two multiplications plus an addition.
Since most controllers do not have the time nor the power for floating point operations, the exponential average is mostly implemented for integer numbers.
be N of {1/32, 1/64, 1/128, 1/256, 1/512} being shifts to the right by 5,6,7,8,9
output:=input * 1/32 + output -( output * 1/32)
or rather
output:=input shr 5 + output - output shr 5
As can be seen the multiplication has been replaced by a shift operation and the operation thus became very fast. The loss of precision is usually countered with an additional byte or two to the right, meaning the whole exponential average is done with eg 256 times the original value and truncated before further use, as
added example of lossless implementation
Have a storage location Y (16 or 32bit), then
Y := Y - (Y shr N)
Y := Y + Input
Output := Y shr N
with N being the number of shift rights 5,6,7,8,9,..
low pass filtering
A common way to remove noise from signals is low pass filtering. Here adapted from electronics, the schematic looks like
The fraction being added to the storage C per unit of time is (input-output)/RC, and the new output thus is
output:=output+(input-output)/RC
a quick rearrangement to
output:=input/RC+output*(1-1/RC)
shows that the electrical lowpass and the exponential average are in fact identical for N equal 1/RC. Thus the above implementation details apply.
Questions ?
Suggestions?
Feedback ?
sponsored links
embedded
home
last updated: 18.Aug.2010 or perhaps later
Copyright (99,2010) Ing.Büro R.Tschaggelar