Code Module: Precise Running Average

Jeremy Leach

Senior Member
This module enables precise calculation of a running average (arithmetic mean) of Word values. Designed for 28X1 but could be adapted.

- Does not need to keep a record of all samples.
- The EXACT average is held as a Whole and Remainder part.
The Remainder is the remainder of the division by the SampleCount.
- There is NO loss of accuracy as new averages are calculated.
- The sample count can be up to 65535.

See my other post on modules for 28X1.



Well, I'm being very thick this morning and obv J has put effort into this. I don't question the Maths.
But, I didn't follow it at all.
How do you get your first value of Avremainder?

Where are all the subroutines referenced?
Where are the Symbols referenced? e.g. XFER_W0

I'm certain this is a very useful method but can't see how to apply it.
Good luck Newbies :)

Jeremy Leach

Senior Member
Come on Dippy, keep up ;)

This is a module to be used in my code module method, see other post in snippets called "28X1: Method for writing modular code"

So the XFER symbols are part of the main module framework that is given in this other post. Honestly the module idea isn't too difficult, and the beauty of it is that you can write separate modules, like this running av one, and test them separately.

Plus, in the title of this module it mentions that it refers to another module 'WordAWordBDivWordC' , so you need this loaded too.

Ok, I agree you need to get to grips with the basics first...


Oh, too many late night films...:eek:

I think you'll have to write this up into a single downloadable doc Jer.

On the Forum it ends up in pieces with a time-related function between them.


Senior Member
It's awfully memory consuming. You might want to highlight the core feature of you efforts so that others get the drift. Which I believe is the 32 bit working register, often called an accumulator.


Jeremy Leach

Senior Member
No, this thread is about a running av code module - nothing to do with 32 bit maths ;) The key feature of this running average is that it doesn't lose ANY accuracy as time progresses. That's because it keeps an exact calculation of the average (whole and remainder) after each sample is taken.

The problem with running averages over a long period of time is that either a) you need to store all the samples and do a big calculation on all of them, or b) you need to update the average as you go and here the accuracy of the average can drift.

Admittedly this module is pretty specialised. But if, say, you wanted an accurate running average of a load of temperature readings then this could help. I actually used this to keep a very accurate record of average elapsed times.


Senior Member
A bit more hardware... but how about as another option a wee EEPROM to store the little blighters (tons of them), in case you want them for something else later?


New member
Only a decade and a half later....
This could be useful for something I'm thinking about:
I want to sample an accelerometer signal as fast as a picaxe can go (ideally >100Hz), compute a running average of as many samples as possible (ideally over a period of a few minutes), then output the running average on the DAC for another device to read every 5 or 10 minutes. The end game is to trend vibration levels on a machine to see if they change over time and to be able to tell if the machine is running.