Pages: [1]   Go Down
Author Topic: Simple histogram class  (Read 1123 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12487
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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()
{
  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
« 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 -
(Please do not PM for private consultancy)

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 553
Posts: 46298
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

OK, fair enough
 I'll fix it smiley
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 -
(Please do not PM for private consultancy)

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12487
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: