Go Down

Topic: Does arduino C++ have queue, stack, and basic stats classes? (Read 6 times) previous topic - next topic

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.

Here's my project:
http://arduino.cc/forum/index.php/topic,49937.0.html

Coding Badly

I think AlphaBeta put some basic data structure things on the Playground.

cr0sh


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.

Here's my project:
http://arduino.cc/forum/index.php/topic,49937.0.html


I found this - it's a start...

http://www.arduino.cc/playground/Main/Statistics
I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

liudr

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

Will upload a video soon.
http://arduino.cc/forum/index.php/topic,49937.0.html

robtillaart

#4
Jan 30, 2011, 10:21 am Last Edit: Jan 30, 2011, 11:07 am by robtillaart Reason: 1
Hi Liudr,

Simplest form of a running average can be done by this code that builds upon the previous running average

Code: [Select]
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.

Code: [Select]
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:

Code: [Select]
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.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up