Mr RB
Well-Known Member
Hi, does anyone have an easy way to keep a long term running average of large numbers, signed long 32bit numbers which are limited by definition to a max size of about 2.14 billion.
The goal is to average a high number of samples that are very similar over a long time.
The samples are based on the PIC xtal freq so typical samples for a 4MHz xtal would be;
4000010
4000013
4000009 (etc)
and may even be more similar samples;
4000010
4000011
4000010 (etc)
and may be as high as a 20MHz xtal;
20000010
20000011 (etc)
As the PIC 16F has limited amounts of RAM this rules out having hundreds of 32bit variables, and an accumulator system can only be 100* samples or so without risking overflowing the signed long variable.
I have had some success with a 100* accumulator;
accum -= (accum / 100);
accum += new_sample;
But this is very poor at handling the average of such fine differences.
An ideal result would be after a long time to have the accumulator with an accurate total based on the average;
4000010.37 (where the decimal point is imaginary)
Is there a simple system that will fit within these constraints? Thanks!
The goal is to average a high number of samples that are very similar over a long time.
The samples are based on the PIC xtal freq so typical samples for a 4MHz xtal would be;
4000010
4000013
4000009 (etc)
and may even be more similar samples;
4000010
4000011
4000010 (etc)
and may be as high as a 20MHz xtal;
20000010
20000011 (etc)
As the PIC 16F has limited amounts of RAM this rules out having hundreds of 32bit variables, and an accumulator system can only be 100* samples or so without risking overflowing the signed long variable.
I have had some success with a 100* accumulator;
accum -= (accum / 100);
accum += new_sample;
But this is very poor at handling the average of such fine differences.
An ideal result would be after a long time to have the accumulator with an accurate total based on the average;
4000010.37 (where the decimal point is imaginary)
Is there a simple system that will fit within these constraints? Thanks!
Last edited: