Pages: [1]   Go Down
Author Topic: Does arduino C++ have queue, stack, and basic stats classes?  (Read 5065 times)
0 Members and 1 Guest are viewing this topic.
Central MN, USA
Offline Offline
Tesla Member
***
Karma: 64
Posts: 6893
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 36
Posts: 5519
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 64
Posts: 6893
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-lol

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


Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12430
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Liudr,

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

Code:
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:
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:
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 smiley

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.
« Last Edit: January 30, 2011, 05:07:27 am by robtillaart » Logged

Rob Tillaart

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

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 64
Posts: 6893
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Sweden
Offline Offline
Full Member
***
Karma: 10
Posts: 237
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

See this: http://www.arduino.cc/playground/Main/GeneralCodeLibrary#DSA_code

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

Jan
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12430
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Liudr,

I'm currently posting a runningAverage class in the playground - http://arduino.cc/playground/Main/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.

« Last Edit: January 30, 2011, 01:02:17 pm by robtillaart » Logged

Rob Tillaart

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

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 64
Posts: 6893
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Pages: [1]   Go Up
Jump to: