Simple histogram class

Just added the 0.1 version of a histogram library to the playground. See - Arduino Playground - 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

As always all comments are welcome,

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

OK, fair enough
I'll fix it :slight_smile:
update -> done (playground too)

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,

Updated Histogram class to version to 0.1.5 (in fact skipped 0.1.4 )

Code can be found - Arduino/libraries/Histogram at master · RobTillaart/Arduino · GitHub

main changes:

  • support for more than 256 buckets
  • added error checking on allocated data
  • added a "graphical" test

side effect is that the changes caused the lib to grow 224 bytes compared to 0.1.3
so possibly a smaller unsafer version of the lib (SmallHisto?) is still appreciated ???

as always comments and remarks are welcome.

Regards,
Rob