+ fix issue #33 - revert double to float as some uproc support float in HW

+ added const where possible
+ updated average demo + test-3.4
This commit is contained in:
RobTillaart 2017-07-31 21:40:26 +02:00
parent 32d3efb77d
commit 8457e6b335
6 changed files with 299 additions and 57 deletions

View File

@ -1,11 +0,0 @@
2012-05-19
-------------
This is a simple statistic library for the Arduino, version: 0.3.1
previous versions are not available.
2013-08-17
------------
version: 0.3.2
http://arduino.cc/playground/Main/Statistics

View File

@ -2,7 +2,7 @@
// FILE: Statistic.cpp
// AUTHOR: Rob dot Tillaart at gmail dot com
// modified at 0.3 by Gil Ross at physics dot org
// VERSION: 0.3.3
// VERSION: 0.3.4
// PURPOSE: Recursive statistical library for Arduino
//
// NOTE: 2011-01-07 Gill Ross
@ -44,6 +44,10 @@
// 0.3.3 - 2015-03-07
// float -> double to support ARM (compiles)
// moved count() sum() min() max() to .h; for optimizing compiler
// 0.3.4 - 2017-07-31
// Refactored const in many places
// [reverted] double to float on request as float is 99.99% of the cases
// good enough and float(32 bit) is supported in HW for some processors.
//
// Released to the public domain
//
@ -59,9 +63,9 @@ Statistic::Statistic()
void Statistic::clear()
{
_cnt = 0;
_sum = 0.0;
_min = 0.0;
_max = 0.0;
_sum = 0;
_min = 0;
_max = 0;
#ifdef STAT_USE_STDEV
_ssqdif = 0.0; // not _ssq but sum of square differences
// which is SUM(from i = 1 to N) of
@ -70,7 +74,7 @@ void Statistic::clear()
}
// adds a new value to the data-set
void Statistic::add(double value)
void Statistic::add(const float value)
{
if (_cnt == 0)
{
@ -82,18 +86,21 @@ void Statistic::add(double value)
}
_sum += value;
_cnt++;
#ifdef STAT_USE_STDEV
if (_cnt > 1)
{
_store = (_sum / _cnt - value);
float _store = (_sum / _cnt - value);
_ssqdif = _ssqdif + _cnt * _store * _store / (_cnt-1);
// ~10% faster but limits the amount of samples to 65K as _cnt*_cnt overflows
// float _store = _sum - _cnt * value;
// _ssqdif = _ssqdif + _store * _store / (_cnt*_cnt - _cnt);
}
#endif
}
// returns the average of the data-set added sofar
double Statistic::average()
float Statistic::average() const
{
if (_cnt == 0) return NAN; // original code returned 0
return _sum / _cnt;
@ -103,19 +110,19 @@ double Statistic::average()
// http://www.suite101.com/content/how-is-standard-deviation-used-a99084
#ifdef STAT_USE_STDEV
double Statistic::variance()
float Statistic::variance() const
{
if (_cnt == 0) return NAN; // otherwise DIV0 error
return _ssqdif / _cnt;
}
double Statistic::pop_stdev()
float Statistic::pop_stdev() const
{
if (_cnt == 0) return NAN; // otherwise DIV0 error
return sqrt( _ssqdif / _cnt);
}
double Statistic::unbiased_stdev()
float Statistic::unbiased_stdev() const
{
if (_cnt < 2) return NAN; // otherwise DIV0 error
return sqrt( _ssqdif / (_cnt - 1));

View File

@ -4,7 +4,7 @@
// FILE: Statistic.h
// AUTHOR: Rob dot Tillaart at gmail dot com
// modified at 0.3 by Gil Ross at physics dot org
// VERSION: 0.3.3
// VERSION: 0.3.4
// PURPOSE: Recursive Statistical library for Arduino
// HISTORY: See Statistic.cpp
//
@ -12,41 +12,40 @@
//
// the standard deviation increases the lib (<100 bytes)
// it can be in/excluded by un/commenting next line
#define STAT_USE_STDEV
// it can be in/excluded by un/commenting next line (compile time)
#define STAT_USE_STDEV 1
#include <math.h>
#define STATISTIC_LIB_VERSION "0.3.3"
#define STATISTIC_LIB_VERSION "0.3.4"
class Statistic
{
public:
Statistic();
void clear();
void add(double);
Statistic(); // "switches on/off" stdev run time
void clear(); // "switches on/off" stdev run time
void add(const float);
// returns the number of values added
unsigned long count() { return _cnt; }; // zero if empty
double sum() { return _sum; }; // zero if empty
double minimum() { return _min; }; // zero if empty
double maximum() { return _max; }; // zero if empty
double average();
uint32_t count() const { return _cnt; }; // zero if empty
float sum() const { return _sum; }; // zero if empty
float minimum() const { return _min; }; // zero if empty
float maximum() const { return _max; }; // zero if empty
float average() const; // NAN if empty
#ifdef STAT_USE_STDEV
double variance();
double pop_stdev(); // population stdev
double unbiased_stdev();
float variance() const; // NAN if empty
float pop_stdev() const; // population stdev // NAN if empty
float unbiased_stdev() const; // NAN if empty
#endif
protected:
unsigned long _cnt;
double _store; // store to minimise computation
double _sum;
double _min;
double _max;
uint32_t _cnt;
float _sum;
float _min;
float _max;
#ifdef STAT_USE_STDEV
double _ssqdif; // sum of squares difference
float _ssqdif; // sum of squares difference
#endif
};

View File

@ -1,7 +1,7 @@
//
// FILE: Average.ino
// AUTHOR: Rob dot Tillaart at gmail dot com
// VERSION: 0.2
// VERSION: 0.3
// PURPOSE: Sample sketch for statistic library Arduino
//
@ -9,37 +9,48 @@
Statistic myStats;
uint32_t start;
uint32_t stop;
void setup(void)
{
Serial.begin(9600);
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("Demo Statistics lib ");
Serial.print(STATISTIC_LIB_VERSION);
Serial.println(STATISTIC_LIB_VERSION);
myStats.clear(); //explicitly start clean
start = millis();
}
void loop(void)
{
long rn = random(0, 9999);
myStats.add(rn/100.0 + 1);
myStats.add(rn / 100.0 + 1);
if (myStats.count() == 10000)
{
Serial.print(" Count: ");
stop = millis();
Serial.print(" Count: ");
Serial.println(myStats.count());
Serial.print(" Min: ");
Serial.println(myStats.minimum(),4);
Serial.print(" Max: ");
Serial.println(myStats.maximum(),4);
Serial.print(" Average: ");
Serial.print(" Min: ");
Serial.println(myStats.minimum(), 4);
Serial.print(" Max: ");
Serial.println(myStats.maximum(), 4);
Serial.print(" Average: ");
Serial.println(myStats.average(), 4);
// uncomment in Statistic.h file to use stdev
#ifdef STAT_USE_STDEV
#ifdef STAT_USE_STDEV
Serial.print(" variance: ");
Serial.println(myStats.variance(), 4);
Serial.print(" pop stdev: ");
Serial.println(myStats.pop_stdev(), 4);
Serial.print(" unbias stdev: ");
Serial.print(" unbias stdev: ");
Serial.println(myStats.unbiased_stdev(), 4);
#endif
#endif
Serial.print(" time(ms): ");
Serial.println(stop - start);
Serial.println("=====================================");
myStats.clear();
delay(1000);
start = millis();
}
}

View File

@ -0,0 +1,236 @@
C:\Users\Rob\Desktop\WORK\Arduino\libraries\Statistic\examples\Average\Average.ino
Demo Statistics lib 0.3.4
Count: 10000
Min: 1.0100
Max: 100.9600
Average: 50.9408
variance: 835.0057
pop stdev: 28.8965
unbias stdev: 28.8979
time(ms): 2547
=====================================
Count: 10000
Min: 1.0000
Max: 100.9800
Average: 51.4161
variance: 839.9565
pop stdev: 28.9820
unbias stdev: 28.9835
time(ms): 2548
=====================================
Count: 10000
Min: 1.0000
Max: 100.9800
Average: 50.9561
variance: 833.2212
pop stdev: 28.8656
unbias stdev: 28.8670
time(ms): 2547
=====================================
Count: 10000
Min: 1.0000
Max: 100.9700
Average: 51.4648
variance: 849.5839
pop stdev: 29.1476
unbias stdev: 29.1491
time(ms): 2549
=====================================
Count: 10000
Min: 1.0000
Max: 100.9800
Average: 50.8610
variance: 842.0071
pop stdev: 29.0174
unbias stdev: 29.0188
time(ms): 2548
=====================================
Count: 10000
Min: 1.0000
Max: 100.9800
Average: 51.2327
variance: 816.5170
pop stdev: 28.5748
unbias stdev: 28.5762
time(ms): 2548
=====================================
Count: 10000
Min: 1.0000
Max: 100.9800
Average: 50.8115
variance: 835.1608
pop stdev: 28.8991
unbias stdev: 28.9006
time(ms): 2547
=====================================
Count: 10000
Min: 1.0000
Max: 100.9800
Average: 50.7948
variance: 842.5279
pop stdev: 29.0263
unbias stdev: 29.0278
time(ms): 2548
=====================================
Count: 10000
Min: 1.0000
Max: 100.9500
Average: 51.0943
variance: 831.8248
pop stdev: 28.8414
unbias stdev: 28.8428
time(ms): 2547
=====================================
Count: 10000
Min: 1.0000
Max: 100.9700
Average: 50.8915
variance: 836.6669
pop stdev: 28.9252
unbias stdev: 28.9266
time(ms): 2546
=====================================
Count: 10000
Min: 1.0100
Max: 100.9800
Average: 50.8437
variance: 825.5516
pop stdev: 28.7324
unbias stdev: 28.7339
time(ms): 2548
=====================================
Count: 10000
Min: 1.0000
Max: 100.9800
Average: 51.3439
variance: 841.3205
pop stdev: 29.0055
unbias stdev: 29.0070
time(ms): 2547
=====================================
Count: 10000
Min: 1.0400
Max: 100.9800
Average: 50.9124
variance: 833.6501
pop stdev: 28.8730
unbias stdev: 28.8744
time(ms): 2548
=====================================
Count: 10000
Min: 1.0000
Max: 100.9500
Average: 50.7144
variance: 825.8471
pop stdev: 28.7376
unbias stdev: 28.7390
time(ms): 2545
=====================================
Count: 10000
Min: 1.0000
Max: 100.9800
Average: 50.8693
variance: 832.8941
pop stdev: 28.8599
unbias stdev: 28.8613
time(ms): 2547
=====================================
Count: 10000
Min: 1.0100
Max: 100.9800
Average: 50.4681
variance: 816.6417
pop stdev: 28.5769
unbias stdev: 28.5784
time(ms): 2546
=====================================
Count: 10000
Min: 1.0000
Max: 100.9700
Average: 50.8079
variance: 830.4100
pop stdev: 28.8168
unbias stdev: 28.8183
time(ms): 2548
=====================================
Count: 10000
Min: 1.0000
Max: 100.9800
Average: 50.9519
variance: 834.7280
pop stdev: 28.8917
unbias stdev: 28.8931
time(ms): 2548
=====================================
Count: 10000
Min: 1.0100
Max: 100.9500
Average: 51.1545
variance: 842.0332
pop stdev: 29.0178
unbias stdev: 29.0193
time(ms): 2547
=====================================
Count: 10000
Min: 1.0000
Max: 100.9800
Average: 51.2756
variance: 838.0289
pop stdev: 28.9487
unbias stdev: 28.9502
time(ms): 2548
=====================================
Count: 10000
Min: 1.0000
Max: 100.9600
Average: 51.0082
variance: 836.6192
pop stdev: 28.9244
unbias stdev: 28.9258
time(ms): 2547
=====================================
Count: 10000
Min: 1.0000
Max: 100.9500
Average: 51.4703
variance: 828.6469
pop stdev: 28.7862
unbias stdev: 28.7877
time(ms): 2548
=====================================
Count: 10000
Min: 1.0000
Max: 100.9500
Average: 51.4059
variance: 822.9982
pop stdev: 28.6879
unbias stdev: 28.6894
time(ms): 2547
=====================================
Count: 10000
Min: 1.0000
Max: 100.9800
Average: 50.9745
variance: 832.5690
pop stdev: 28.8543
unbias stdev: 28.8557
time(ms): 2547
=====================================
Count: 10000
Min: 1.0000
Max: 100.9800
Average: 50.8281
variance: 836.5443
pop stdev: 28.9231
unbias stdev: 28.9245
time(ms): 2547
=====================================
Count: 10000
Min: 1.0000
Max: 100.9800
Average: 51.0404
variance: 825.2975
pop stdev: 28.7280
unbias stdev: 28.7294
time(ms): 2548
=====================================

View File

@ -1,5 +1,5 @@
name=Statistics
version=0.3.3
version=0.3.4
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Library with basic statistical functions for Arduino.