Skip to content

Commit 07534d7

Browse files
committed
+ version 0.3.3
+ changed float -> double to support ARM series + moved some code to .h file so compiler can optimize more + reindented
1 parent d9affe6 commit 07534d7

File tree

2 files changed

+74
-93
lines changed

2 files changed

+74
-93
lines changed

libraries/Statistic/Statistic.cpp

Lines changed: 49 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
//
22
// FILE: Statistic.cpp
3-
// AUTHOR: Rob dot Tillaart at gmail dot com
3+
// AUTHOR: Rob dot Tillaart at gmail dot com
44
// modified at 0.3 by Gil Ross at physics dot org
5-
// VERSION: see STATISTIC_LIB_VERSION in .h
5+
// VERSION: 0.3.3
66
// PURPOSE: Recursive statistical library for Arduino
77
//
88
// NOTE: 2011-01-07 Gill Ross
99
// Rob Tillaart's Statistic library uses one-pass of the data (allowing
1010
// each value to be discarded), but expands the Sum of Squares Differences to
1111
// difference the Sum of Squares and the Average Squared. This is susceptible
12-
// to bit length precision errors with the float type (only 5 or 6 digits
12+
// to bit length precision errors with the float type (only 5 or 6 digits
1313
// absolute precision) so for long runs and high ratios of
14-
// the average value to standard deviation the estimate of the
14+
// the average value to standard deviation the estimate of the
1515
// standard error (deviation) becomes the difference of two large
1616
// numbers and will tend to zero.
1717
//
1818
// For small numbers of iterations and small Average/SE th original code is
1919
// likely to work fine.
20-
// It should also be recognised that for very large samples, questions
20+
// It should also be recognised that for very large samples, questions
2121
// of stability of the sample assume greater importance than the
2222
// correctness of the asymptotic estimators.
2323
//
2424
// This recursive algorithm, which takes slightly more computation per
2525
// iteration is numerically stable.
2626
// It updates the number, mean, max, min and SumOfSquaresDiff each step to
27-
// deliver max min average, population standard error (standard deviation) and
27+
// deliver max min average, population standard error (standard deviation) and
2828
// unbiassed SE.
2929
// -------------
3030
//
@@ -34,13 +34,16 @@
3434
// 0.2.01 - 2010-10-30
3535
// added minimim, maximum, unbiased stdev,
3636
// changed counter to long -> int overflows @32K samples
37-
// 0.3 - 2011-01-07
37+
// 0.3 - 2011-01-07
3838
// branched from 0.2.01 version of Rob Tillaart's code
3939
// 0.3.1 - minor edits
4040
// 0.3.2 - 2012-11-10
41-
// minor edits
41+
// minor edits
4242
// changed count -> unsigned long allows for 2^32 samples
4343
// added variance()
44+
// 0.3.3 - 2015-03-07
45+
// float -> double to support ARM (compiles)
46+
// moved count() sum() min() max() to .h; for optimizing compiler
4447
//
4548
// Released to the public domain
4649
//
@@ -49,98 +52,73 @@
4952

5053
Statistic::Statistic()
5154
{
52-
clear();
55+
clear();
5356
}
5457

5558
// resets all counters
5659
void Statistic::clear()
57-
{
58-
_cnt = 0;
59-
_sum = 0.0;
60-
_min = 0.0;
61-
_max = 0.0;
60+
{
61+
_cnt = 0;
62+
_sum = 0.0;
63+
_min = 0.0;
64+
_max = 0.0;
6265
#ifdef STAT_USE_STDEV
63-
_ssqdif = 0.0; // not _ssq but sum of square differences
64-
// which is SUM(from i = 1 to N) of
65-
// (f(i)-_ave_N)**2
66+
_ssqdif = 0.0; // not _ssq but sum of square differences
67+
// which is SUM(from i = 1 to N) of
68+
// (f(i)-_ave_N)**2
6669
#endif
6770
}
6871

6972
// adds a new value to the data-set
70-
void Statistic::add(float f)
73+
void Statistic::add(double value)
7174
{
72-
if (_cnt == 0)
73-
{
74-
_min = f;
75-
_max = f;
76-
} else {
77-
if (f < _min) _min = f;
78-
if (f > _max) _max = f;
75+
if (_cnt == 0)
76+
{
77+
_min = value;
78+
_max = value;
79+
} else {
80+
if (value < _min) _min = value;
81+
else if (value > _max) _max = value;
7982
}
80-
_sum += f;
81-
_cnt++;
82-
83-
#ifdef STAT_USE_STDEV
84-
if (_cnt >1)
85-
{
86-
_store = (_sum / _cnt - f);
83+
_sum += value;
84+
_cnt++;
85+
86+
#ifdef STAT_USE_STDEV
87+
if (_cnt > 1)
88+
{
89+
_store = (_sum / _cnt - value);
8790
_ssqdif = _ssqdif + _cnt * _store * _store / (_cnt-1);
88-
}
91+
}
8992
#endif
9093
}
9194

92-
// returns the number of values added
93-
unsigned long Statistic::count()
94-
{
95-
return _cnt;
96-
}
97-
9895
// returns the average of the data-set added sofar
99-
float Statistic::average()
100-
{
101-
if (_cnt == 0) return NAN; // original code returned 0
102-
return _sum / _cnt;
103-
}
104-
105-
// returns the sum of the data-set (0 if no values added)
106-
float Statistic::sum()
96+
double Statistic::average()
10797
{
108-
return _sum;
98+
if (_cnt == 0) return NAN; // original code returned 0
99+
return _sum / _cnt;
109100
}
110101

111-
// returns the sum of the data-set (0 if no values added)
112-
float Statistic::minimum()
113-
{
114-
return _min;
115-
}
116-
117-
// returns the sum of the data-set (0 if no values added)
118-
float Statistic::maximum()
119-
{
120-
return _max;
121-
}
122-
123-
124102
// Population standard deviation = s = sqrt [ S ( Xi - µ )2 / N ]
125103
// http://www.suite101.com/content/how-is-standard-deviation-used-a99084
126-
#ifdef STAT_USE_STDEV
104+
#ifdef STAT_USE_STDEV
127105

128-
float Statistic::variance()
106+
double Statistic::variance()
129107
{
130-
if (_cnt == 0) return NAN; // otherwise DIV0 error
131-
return _ssqdif / _cnt;
108+
if (_cnt == 0) return NAN; // otherwise DIV0 error
109+
return _ssqdif / _cnt;
132110
}
133111

134-
float Statistic::pop_stdev()
112+
double Statistic::pop_stdev()
135113
{
136-
if (_cnt == 0) return NAN; // otherwise DIV0 error
137-
return sqrt( _ssqdif / _cnt);
114+
if (_cnt == 0) return NAN; // otherwise DIV0 error
115+
return sqrt( _ssqdif / _cnt);
138116
}
139117

140-
float Statistic::unbiased_stdev()
118+
double Statistic::unbiased_stdev()
141119
{
142-
if (_cnt < 2) return NAN; // otherwise DIV0 error
143-
return sqrt( _ssqdif / (_cnt - 1));
120+
if (_cnt < 2) return NAN; // otherwise DIV0 error
121+
return sqrt( _ssqdif / (_cnt - 1));
144122
}
145123

146124
#endif

libraries/Statistic/Statistic.h

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#ifndef Statistic_h
22
#define Statistic_h
3-
//
3+
//
44
// FILE: Statistic.h
5-
// AUTHOR: Rob dot Tillaart at gmail dot com
5+
// AUTHOR: Rob dot Tillaart at gmail dot com
66
// modified at 0.3 by Gil Ross at physics dot org
7+
// VERSION: 0.3.3
78
// PURPOSE: Recursive Statistical library for Arduino
89
// HISTORY: See Statistic.cpp
910
//
@@ -16,34 +17,36 @@
1617

1718
#include <math.h>
1819

19-
#define STATISTIC_LIB_VERSION "0.3.2"
20+
#define STATISTIC_LIB_VERSION "0.3.3"
2021

21-
class Statistic
22+
class Statistic
2223
{
23-
public:
24-
Statistic();
25-
void clear();
26-
void add(float);
27-
unsigned long count();
28-
float sum();
29-
float average();
30-
float minimum();
31-
float maximum();
24+
public:
25+
Statistic();
26+
void clear();
27+
void add(double);
28+
29+
// returns the number of values added
30+
unsigned long count() { return _cnt; }; // zero if empty
31+
double sum() { return _sum; }; // zero if empty
32+
double minimum() { return _min; }; // zero if empty
33+
double maximum() { return _max; }; // zero if empty
34+
double average();
3235

3336
#ifdef STAT_USE_STDEV
34-
float variance();
35-
float pop_stdev(); // population stdev
36-
float unbiased_stdev();
37+
double variance();
38+
double pop_stdev(); // population stdev
39+
double unbiased_stdev();
3740
#endif
3841

3942
protected:
40-
unsigned long _cnt;
41-
float _store; // store to minimise computation
42-
float _sum;
43-
float _min;
44-
float _max;
43+
unsigned long _cnt;
44+
double _store; // store to minimise computation
45+
double _sum;
46+
double _min;
47+
double _max;
4548
#ifdef STAT_USE_STDEV
46-
float _ssqdif; // sum of squares difference
49+
double _ssqdif; // sum of squares difference
4750
#endif
4851
};
4952

0 commit comments

Comments
 (0)