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