Pages: [1]   Go Down
 Author Topic: Simple histogram class  (Read 624 times) 0 Members and 1 Guest are viewing this topic.
Netherlands
Offline
Tesla Member
Karma: 87
Posts: 9371
In theory there is no difference between theory and practice, however in practice there are many...
 « on: November 10, 2012, 08:07:24 am » Bigger Smaller Reset

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:
//
//    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()
{

// 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
 « Last Edit: November 10, 2012, 09:52:19 am by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
 « Reply #1 on: November 10, 2012, 09:06:43 am » Bigger Smaller Reset

Quote
As always all comments are welcome,
I'd prefer to see the example not use delay().
 Logged

Netherlands
Offline
Tesla Member
Karma: 87
Posts: 9371
In theory there is no difference between theory and practice, however in practice there are many...
 « Reply #2 on: November 10, 2012, 09:44:10 am » Bigger Smaller Reset

OK, fair enough
I'll fix it
update -> done (playground too)
 « Last Edit: November 10, 2012, 09:53:48 am by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Netherlands
Offline
Tesla Member
Karma: 87
Posts: 9371
In theory there is no difference between theory and practice, however in practice there are many...
 « Reply #3 on: December 23, 2012, 09:35:21 am » Bigger Smaller Reset

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,

 Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

 Pages: [1]   Go Up