Go Down

Topic: Simple histogram class (Read 1 time) previous topic - next topic

robtillaart

Nov 10, 2012, 02:07 pm Last Edit: Nov 10, 2012, 03:52 pm by robtillaart Reason: 1
Just added the  0.1 version of a histogram library to the playground. See - http://arduino.cc/playground/Main/Histogram -

The class uses an external array of floats to define the number of buckets and in which bucket new (measurement) data is to be added.

This example code shows the basic working of the buckets, note that the intervals are different in size.

Code: [Select]
//
//    FILE: hist_test.pde
//  AUTHOR: Rob Tillaart
//    DATE: 2012-11-10
//
// PUPROSE: test histogram library
//

#include "histogram.h"

float b[] = { 0, 300, 325, 350, 375, 400, 1000 };

Histogram hist(7, b);

unsigned long lastTime = 0;
const unsigned long threshold = 50;  // milliseconds, for updating display

void setup()
{
 Serial.begin(115200);
 Serial.print("\nHistogram version: ");
 Serial.println(HISTOGRAM_LIB_VERSION);
}

void loop()
{
 int x = analogRead(A0);
 hist.add(x);

 // update output
 unsigned long now = millis();
 if (now - lastTime > threshold)
 {
   lastTime = now;
   Serial.print(hist.count());
   Serial.print("\t");
   for (int i = 0; i < hist.size(); i++)
   {
     Serial.print((1.0*hist.bucket(i))/hist.count(),2);  // gives percentage per bucket
     Serial.print("\t");
   }
   Serial.println();
   if (hist.count() > 100000UL) hist.clear();
 }
}


As always all comments are welcome,

Rob
Rob Tillaart

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

PaulS

Quote
As always all comments are welcome,

I'd prefer to see the example not use delay().

robtillaart

#2
Nov 10, 2012, 03:44 pm Last Edit: Nov 10, 2012, 03:53 pm by robtillaart Reason: 1
OK, fair enough
I'll fix it :)
update -> done (playground too)
Rob Tillaart

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

robtillaart

updated the histogram lib to 0.1.2 on the playground.

There are three experimental functions: PMF, CDF and VAL.
PMF is quite similar to frequency, but uses a value as parameter. (no knowledge of buckets is needed)
CDF gives the sum of frequencies <= value.
VAL is CDF inverted.

As the Arduino typical uses a small number of buckets (<20)  these functions are quite coarse or even inaccurate (read useless).
Linear interpolation within the buckets will improve the accuracy but it is unknown to what extend. If the average value of every bucket is available the interpolation can be better than just linear interpolation. However this additional code and storage will increase the footprint and complexity of the lib..

To be continued,



Rob Tillaart

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

Go Up