Re: [foxboro] Rolling Averages

The seems like a very complicated way to solve a simple problem. Some
points:
1) The average can be calculated MUCH more efficiently by realising that
one only needs to remove the effect of the oldest value and then add the
effect of the newest value. One does not have to iterate over the entire
array at every execution. To explain this further:
    - consider an average of 5 values V(k), V(k-1), V(k-2), V(k-3),
V(k-4) where V(k) is the current value and V(k-4) is the value from 4
samples ago. 
The average we need to calculate is
   A(k) = V(k)/5 + V(k-1)/5 + V(k-2)/5 + V(k-3)/5 + V(k-4)/5
 
But remember the average calculated from the last cycle would have been
   A(k-1) = V(k-1)/5 + V(k-2)/5 + V(k-3)/5 + V(k-4)/5 + V(k-5)/5
 
Hence A(k) = A(k-1) + V(k)/5 - V(k-5)/5
 
The average calculation then involves the addition of just 3 numbers
regardless of how big the array is. Your are already keeping a copy of
the last calculated average since it's one of the blocks outputs.
 
Some might argue that numerics might mean that a bias will develop. If
this is the case, keep a copy of the sum instead of relying on the
average, and add and subtract from the sum. The average is then the sum
divided by the number of samples. If it's still a problem, do a full
average very occasionally (perhaps one every 10,000 executions depending
on the numeric issue).
 
2) Why don't you want to include 0.0 inputs? In many cases this won't be
required.
 
3) The use of the default value to determine whether or not the stored
value is real or not is an extra complication that one would often not
require. Two simple alternatives are (A) set all the values to a
constant such as zero (remember to set the average to the same constant)
(B) set all the values to the first sample read in (remember to set the
average, A(k), to the same value).
 
Regards
Adam
Lihir Gold
 
-----Original Message-----
From: foxboro-bounce@xxxxxxxxxxxxx [mailto:foxboro-bounce@xxxxxxxxxxxxx
<mailto:foxboro-bounce@xxxxxxxxxxxxx> ] On Behalf Of patrick Den Haese
Sent: Friday, 9 December 2005 1:40 AM
To: foxboro@xxxxxxxxxxxxx
Subject: Re: [foxboro] Rolling Averages
There is an example of a running average on the CSC website (HH1042) if
you don't have access here is the printout
 
 
 
_______________________________________________________________________
This mailing list is neither sponsored nor endorsed by Invensys Process
Systems (formerly The Foxboro Company). Use the info you obtain here at
your own risks. Read http://www.thecassandraproject.org/disclaimer.html
 
foxboro mailing list:             http://www.freelists.org/list/foxboro
to subscribe:         mailto:foxboro-request@xxxxxxxxxxxxx?subject=join
to unsubscribe:      mailto:foxboro-request@xxxxxxxxxxxxx?subject=leave
 

Other related posts: