Fix issue #33; Fix example programs; add keywords.txt

This commit is contained in:
RobTillaart 2017-07-27 13:13:41 +02:00
parent 5c7f310f50
commit 7c0359f1af
9 changed files with 87 additions and 46 deletions

View File

@ -1,6 +1,7 @@
//
// FILE: hist_test.pde
// AUTHOR: Rob Tillaart
// VERSION: 0.1.1
// DATE: 2012-12-23
//
// PUPROSE: test histogram frequency
@ -8,10 +9,10 @@
#include "histogram.h"
// double b[] = { 0, 100, 200, 300, 325, 350, 375, 400, 500, 600, 700, 800, 900, 1000 };
// float b[] = { 0, 100, 200, 300, 325, 350, 375, 400, 500, 600, 700, 800, 900, 1000 };
// boundaries array does not need to be equally distributed.
double b[] = {
// boundaries does not need to be equally distributed.
float b[] = {
0, 100, 200, 300, 325, 350, 375 };
Histogram hist(7, b);
@ -22,6 +23,7 @@ const uint32_t threshold = 25; // milliseconds, for updating display
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("\nHistogram version: ");
Serial.println(HISTOGRAM_LIB_VERSION);
@ -89,4 +91,4 @@ void loop()
}
}
// END OF FILE

View File

@ -1,6 +1,7 @@
//
// FILE: hist_test_cdf.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.1
// DATE: 2012-11-10
//
// PUPROSE: test histogram library
@ -8,7 +9,7 @@
#include "histogram.h"
double b[] = {
float b[] = {
0, 300, 325, 350, 375, 400, 1000 };
Histogram hist(7, b);

View File

@ -1,6 +1,7 @@
//
// FILE: hist_test_graph.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.1
// DATE: 2017-07-16
//
// PUPROSE: test histogram frequency
@ -9,7 +10,7 @@
#include "histogram.h"
// boundaries array does not need to be equally distributed.
double bounds[] = { 0, 100, 200, 300, 325, 350, 375, 400, 500, 600, 700, 800, 900, 1000 };
float bounds[] = { 0, 100, 200, 300, 325, 350, 375, 400, 500, 600, 700, 800, 900, 1000 };
Histogram hist(14, bounds);
@ -48,9 +49,11 @@ void loop()
Serial.print("\t");
int n = hist.frequency(i) * 50; // 0..50
for (int p = 0; p < n; p++);
Serial.print(n);
Serial.print("\t");
for (int p = 0; p < n; p++)
{
Serial.print(']')
Serial.print(']');
}
Serial.println();
}
@ -58,4 +61,3 @@ void loop()
}
}

View File

@ -1,6 +1,7 @@
//
// FILE: hist_test_pmf.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.1
// DATE: 2012-11-10
//
// PUPROSE: test histogram library
@ -8,7 +9,7 @@
#include "histogram.h"
double b[] = {
float b[] = {
0, 50, 100, 150, 200, 250,
300, 350, 400, 450, 500,
600, 700, 800, 900, 1000 };
@ -42,3 +43,4 @@ void loop()
delay(10);
}
// END OF FILE

View File

@ -1,6 +1,7 @@
//
// FILE: hist_test_val.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.1
// DATE: 2012-11-11
//
// PUPROSE: test histogram library
@ -8,7 +9,7 @@
#include "histogram.h"
double b[] = {
float b[] = {
0, 50, 100, 150, 200, 250,
300, 350, 400, 450, 500,
600, 700, 800, 900, 1000 };
@ -54,3 +55,4 @@ void loop()
delay(10);
}
// END OF FILE

View File

@ -1,7 +1,7 @@
//
// FILE: Histogram.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.5
// VERSION: 0.1.6
// PURPOSE: Histogram library for Arduino
// DATE: 2012-11-10
//
@ -14,13 +14,14 @@
// 0.1.3 - 2013-09-29 testing a lot & refactoring
// 0.1.4 - 2015-03-06 stricter interface
// 0.1.5 - 2017-07-16 refactor, support for > 256 buckets; prevent alloc errors
// 0.1.6 - 2017-07-27 revert double to float (issue #33)
//
// Released to the public domain
//
#include "histogram.h"
Histogram::Histogram(const int16_t len, double *bounds)
Histogram::Histogram(const int16_t len, float *bounds)
{
_bounds = bounds;
_len = len + 1;
@ -43,7 +44,7 @@ void Histogram::clear()
}
// adds a new value to the histogram - increasing
void Histogram::add(const double f)
void Histogram::add(const float f)
{
if (_len > 0)
{
@ -53,14 +54,13 @@ void Histogram::add(const double f)
}
// adds a new value to the histogram - decreasing
void Histogram::sub(const double f)
void Histogram::sub(const float f)
{
if (_len > 0)
{
_data[find(f)]--;
_cnt++;
}
}
// returns the count of a bucket
@ -71,18 +71,20 @@ int32_t Histogram::bucket(const int16_t idx)
}
// returns the relative frequency of a bucket
double Histogram::frequency(const int16_t idx)
float Histogram::frequency(const int16_t idx)
{
if (_cnt == 0 || _len == 0) return NAN;
if (idx > _len) return 0; // diff with PMF
return (1.0 * _data[idx]) / _cnt;
}
// EXPERIMENTAL
// returns the probability of the bucket of a value
double Histogram::PMF(const double val)
float Histogram::PMF(const float val)
{
if (_cnt == 0 || _len == 0) return NAN;
int16_t idx = find(val);
return (1.0 * _data[idx]) / _cnt;
}
@ -90,9 +92,10 @@ double Histogram::PMF(const double val)
// EXPERIMENTAL
// returns the cummulative probability of
// values <= value
double Histogram::CDF(const double val)
float Histogram::CDF(const float val)
{
if (_cnt == 0 || _len == 0) return NAN;
int16_t idx = find(val);
int32_t sum = 0;
for (int16_t i = 0; i <= idx; i++)
@ -105,14 +108,14 @@ double Histogram::CDF(const double val)
// EXPERIMENTAL
// returns the value of the original array for
// which the CDF is at least prob.
double Histogram::VAL(const double prob)
float Histogram::VAL(const float prob)
{
if (_cnt == 0 || _len == 0) return NAN;
double p = prob;
float p = prob;
if (p < 0.0) p = 0.0;
if (p > 1.0) p = 1.0;
double probability = p * _cnt;
float probability = p * _cnt;
int32_t sum = 0;
for (int16_t i = 0; i < _len; i++)
{
@ -123,10 +126,10 @@ double Histogram::VAL(const double prob)
}
// returns the bucket number for value val
int16_t Histogram::find(const double val)
int16_t Histogram::find(const float val)
{
if (_len <= 0) return -1;
for (int16_t i = 0; i < (_len-1); i++)
{
if (_bounds[i] >= val) return i;

View File

@ -20,36 +20,37 @@
#include "WProgram.h"
#endif
#define HISTOGRAM_LIB_VERSION "0.1.5"
#define HISTOGRAM_LIB_VERSION "0.1.6"
class Histogram
{
public:
Histogram(const int16_t len, double *bounds);
~Histogram();
Histogram(const int16_t len, float *bounds);
~Histogram();
void clear();
void add(const double val);
void sub(const double val);
void clear();
void add(const float val);
void sub(const float val);
// number of buckets
inline int16_t size() { return _len; };
// number of values added to all buckets
inline uint32_t count() { return _cnt; };
// number of values added to single bucket
int32_t bucket(const int16_t idx);
// number of buckets
inline int16_t size() { return _len; };
double frequency(const int16_t idx);
double PMF(const double val);
double CDF(const double val);
double VAL(const double prob);
int16_t find(const double f);
// number of values added to all buckets
inline uint32_t count() { return _cnt; };
// number of values added to single bucket
int32_t bucket(const int16_t idx);
float frequency(const int16_t idx);
float PMF(const float val);
float CDF(const float val);
float VAL(const float prob);
int16_t find(const float f);
protected:
double * _bounds;
int32_t * _data;
int16_t _len;
uint32_t _cnt;
float * _bounds;
int32_t * _data;
int16_t _len;
uint32_t _cnt;
};
#endif

View File

@ -0,0 +1,28 @@
#######################################
# Syntax Coloring Map For Histogram
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
Histogram KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
clear KEYWORD2
add KEYWORD2
sub KEYWORD2
size KEYWORD2
count KEYWORD2
bucket KEYWORD2
frequency KEYWORD2
PMF KEYWORD2
CDF KEYWORD2
VAL KEYWORD2
find KEYWORD2
#######################################
# Constants (LITERAL1)
#######################################
HISTOGRAM_LIB_VERSION LITERAL1

View File

@ -1,5 +1,5 @@
name=Histogram
version=0.1.5
version=0.1.6
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Library for creating histogram math.