I am working on a project, where I need queue, stack, and basic stats like running avg and stdev.
Has anyone written libraries for the above? I wrote my own basic stats clas but wouldn't mind scrap it all and use some time-tested and optimized codes. Thanks.
liudr:
I am working on a project, where I need queue, stack, and basic stats like running avg and stdev.
Has anyone written libraries for the above? I wrote my own basic stats clas but wouldn't mind scrap it all and use some time-tested and optimized codes. Thanks.
Thanks guys. That stats library is kind of basic. I need a running average. I will post some codes here once I tidy up mine. Now my music box is more intelligent. If it recognizes the tune you tap in, it will play the song. It is all basic stats and physics XD
Simplest form of a running average can be done by this code that builds upon the previous running average
float alpha = 0.7; // factor to play with
value = alpha * measurement + (1-alpha) * value;
This can be done in integer math too. The division by 256 is a shift-right 8, which is faster than say division by 100.
int alpha = 178;
value = (alpha * measurement + (256 - alpha) * value )/ 256; // watch the ()
If you need a more accurate running average, in concreto from the last X measurements you need an array to hold them. This array acts as a circular buffer and with every new measurement the oldest is removed. Then the running average is the sum of all elements divided by the count. The code will be something like this:
long runningAverage(int M)
{
#define LMSIZE 10
static int LM[LMSIZE]; // LastMeasurements
static byte index = 0;
static long sum = 0;
static byte count = 0;
// keep an updated sum to improve speed.
sum -= LM[index];
LM[index] = M;
sum += LM[index];
index = index % LMSIZE;
if (count < LMSIZE) count++;
return sum / count;
}
Drawback of this code is that this array to hold the values can become quite large, if you have one measurement per second and you want a running average per minute you need an array of 60; an average per hour would need an array of 3600 ints (floats or longs). That couldn't be done this way on an Arduino. However by building a 2 stage average it can be approached quite well (not for all measurements). In psuedo code:
every second: rapm = runningAverageMinute(measurement);
every minute: raph = runningAverageHour(rapm);
As a new internal static array is needed for every runningAverage, this screams to implement this as a class
Maybe this helps.
PS, I' will look if I can update the statistics lib with this code or add it as a separate class to the playground article.
Thank you robtillaart! I have made something very similar. My purpose is to detect disturbance to a box through the large STDEV on the accelerometer. Just a few measurements is needed. I'll need a real running avg for some other functions I'm thinking of.
I'm currently posting a runningAverage class in the playground - Arduino Playground - RunningAverage - based upon your need. The class uses floats but can easily be rewritten to use int or long values. I first thought to add it to the simple stat library but I like to keep the footprint of that lib as small as possible so the runningAverage with its dynamic footprint is in conflict. But feel free to merge or adapt to your own need.