Does arduino C++ have queue, stack, and basic stats classes?

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 think AlphaBeta put some basic data structure things on the Playground.

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

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

http://www.arduino.cc/playground/Main/Statistics

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

Hi Liudr,

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 :slight_smile:

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.

See this: Arduino Playground - GeneralCodeLibrary

There are dynamic stacks and dynamic queues. Each has a linked list version and a array version.

Jan

Hi Liudr,

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.

Thanks for the inspiration !

Rob.

Hey thank you Rob! As I replied on the other thread, I love it!

Check out this generic

Queue.h

class, can be added to your project and used for queueing different things (byte, int, char, Enums...).

For example:

#include "Queue.h":

void setup() {
  Queue<byte> queue = Queue(); 
  byte a = 255;
  byte b = 0;
  byte x;
  queue.push(a);
  queue.push(b);
  x = queue.pop(); // 255
  x = queue.pop(); // 0
}