Mozzi  version v1.1.0 sound synthesis library for Arduino
RollingStat.h
1 #ifndef ROLLINGSTAT_H
2 #define ROLLINGSTAT_H
3
4 /*
5  * RollingStat.h
6  *
7  * WARNING: this code is incomplete, it doesn't work yet
8  *
9  * Copyright 2013 Tim Barrass.
10  *
11  * This file is part of Mozzi.
12  *
14  *
15  */
16
17 #include "RollingAverage.h"
18 #include "mozzi_fixmath.h"
19
20 // adapted from RunningStat, http://www.johndcook.com/standard_deviation.html
21 /** @ingroup sensortools
22 WARNING: this class is work in progress, don't use it yet.
23 Calculates an approximation of the variance and standard deviation for a window of recent inputs.
24 @tparam T the type of numbers to use. Choose unsigned int, int , uint8_t, int8_t, or float
25 @tparam WINDOW_LENGTH how many recent input values to include in the calculations.
26 */
27 template <class T, int WINDOW_LENGTH>
29 {
30 public:
31
32  /** Constructor */
33  RollingStat() : _previous_mean(0), _mean(0), _variance(0), WINDOW_LENGTH_AS_RSHIFT(trailingZerosConst(WINDOW_LENGTH))
34  {}
35
36
37  /** Update the mean and variance given a new input value.
38  @param x the next input value
39  @note timing for unsigned int 10us, int 22us
40  */
41  void update(T x) {
42  _mean = rollingMean.next(x);
43  _variance = (((long)x - _previous_mean)*((long)x - _mean))>>WINDOW_LENGTH_AS_RSHIFT; // should really be div by (WINDOW_LENGTH-1), but exact values are not important for this application
44  _previous_mean = _mean;
45  }
46
47
48  /** Update the mean and variance given a new input value.
49  @param x the next input value
50  */
51  void update(int8_t x) {
52  _mean = rollingMean.next(x);
53  _variance = (((int)x - _previous_mean)*((int)x - _mean))>>WINDOW_LENGTH_AS_RSHIFT; // should really be div by (WINDOW_LENGTH-1), but exact values are not important for this application
54  _previous_mean = _mean;
55  }
56
57
58  /** Return the mean of the last WINDOW_LENGTH number of inputs.
59  @return mean
60  */
61  T getMean() const {
62  return _mean;
63  }
64
65
66  /** Return the approximate variance of the last WINDOW_LENGTH number of inputs.
67  @return variance
68  @note This should really be calculated using WINDOW_LENGTH-1, but sacrificing accuracy for speed
69  we use the power of two value of WINDOW_LENGTH.
70  */
71  T getVariance() const {
72  return _variance;
73  }
74
75  /** Return the approximate standard deviation of the last WINDOW_LENGTH number of inputs.
76  @return standard deviation.
77  */
79  return isqrt16(_variance);
80  }
81
82
83
84 private:
85  T _previous_mean, _mean, _variance;
86  RollingAverage <T, WINDOW_LENGTH> rollingMean;
87  const uint8_t WINDOW_LENGTH_AS_RSHIFT;
88 };
89
90 // no need to show the specialisations
91 /** @cond */
92
93 /** float specialisation of RollingStat template */
94 template <int WINDOW_LENGTH>
95 class RollingStat <float, WINDOW_LENGTH>
96 {
97 public:
98
99  /** Constructor */
100  RollingStat() : _previous_mean(0), _mean(0), _variance(0), WINDOW_LENGTH_AS_RSHIFT(trailingZerosConst(WINDOW_LENGTH))
101  {}
102
103
104  /** Update the mean and variance given a new input value.
105  @param x the next input value
106  @note timing for float: 60 to 90us
107  */
108  void update(float x) {
109  _mean = rollingMean.next(x);
110  _variance = ((x - _previous_mean)*(x - _mean))/(WINDOW_LENGTH-1);
111  _previous_mean = _mean;
112  }
113
114
115  /** Return the mean of the last WINDOW_LENGTH number of inputs.
116  @return mean
117  */
118  float getMean() const {
119  return _mean;
120  }
121
122
123  /** Return the approximate variance of the last WINDOW_LENGTH number of inputs.
124  @return variance
125  */
126  float getVariance() const {
127  return _variance;
128  }
129
130  /** Calculate and return the approximate standard deviation of the last WINDOW_LENGTH number of inputs.
131  @return standard deviation.
132  @note this is probably too slow to use!
133  */
134  float getStandardDeviation() const {
135  return sqrt(_variance);
136  }
137
138
139
140 private:
141  float _previous_mean, _mean, _variance;
142  RollingAverage <float, WINDOW_LENGTH> rollingMean;
143  const uint8_t WINDOW_LENGTH_AS_RSHIFT;
144 };
145
146 // no need to show the specialisations
147 /** @endcond */
148
149 #endif // #ifndef ROLLINGSTAT_H
T getVariance() const
Return the approximate variance of the last WINDOW_LENGTH number of inputs.
Definition: RollingStat.h:71
void update(int8_t x)
Update the mean and variance given a new input value.
Definition: RollingStat.h:51
Calculates a running average over a specified number of the most recent readings. ...
T getMean() const
Return the mean of the last WINDOW_LENGTH number of inputs.
Definition: RollingStat.h:61
RollingStat()
Constructor.
Definition: RollingStat.h:33
void update(T x)
Update the mean and variance given a new input value.
Definition: RollingStat.h:41
WARNING: this class is work in progress, don&#39;t use it yet.
Definition: RollingStat.h:28
T getStandardDeviation() const
Return the approximate standard deviation of the last WINDOW_LENGTH number of inputs.
Definition: RollingStat.h:78