Mozzi  version v2.0
sound synthesis library for Arduino
RollingStat.h
1 /*
2  * RollingStat.h
3  *
4  * This file is part of Mozzi.
5  *
6  * Copyright 2013-2024 Tim Barrass and the Mozzi Team
7  *
8  * Mozzi is licensed under the GNU Lesser General Public Licence (LGPL) Version 2.1 or later.
9  *
10  * WARNING: this code is incomplete, it doesn't work yet
11  */
12 
13 #ifndef ROLLINGSTAT_H
14 #define ROLLINGSTAT_H
15 
16 #include "RollingAverage.h"
17 #include "mozzi_fixmath.h"
18 
19 // adapted from RunningStat, http://www.johndcook.com/standard_deviation.html
26 template <class T, int WINDOW_LENGTH>
28 {
29 public:
30 
32  RollingStat() : _previous_mean(0), _mean(0), _variance(0), WINDOW_LENGTH_AS_RSHIFT(trailingZerosConst(WINDOW_LENGTH))
33  {}
34 
35 
40  void update(T x) {
41  _mean = rollingMean.next(x);
42  _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
43  _previous_mean = _mean;
44  }
45 
46 
50  void update(int8_t x) {
51  _mean = rollingMean.next(x);
52  _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
53  _previous_mean = _mean;
54  }
55 
56 
60  T getMean() const {
61  return _mean;
62  }
63 
64 
70  T getVariance() const {
71  return _variance;
72  }
73 
78  return isqrt16(_variance);
79  }
80 
81 
82 
83 private:
84  T _previous_mean, _mean, _variance;
86  const uint8_t WINDOW_LENGTH_AS_RSHIFT;
87 };
88 
89 // no need to show the specialisations
93 template <int WINDOW_LENGTH>
94 class RollingStat <float, WINDOW_LENGTH>
95 {
96 public:
97 
99  RollingStat() : _previous_mean(0), _mean(0), _variance(0), WINDOW_LENGTH_AS_RSHIFT(trailingZerosConst(WINDOW_LENGTH))
100  {}
101 
102 
107  void update(float x) {
108  _mean = rollingMean.next(x);
109  _variance = ((x - _previous_mean)*(x - _mean))/(WINDOW_LENGTH-1);
110  _previous_mean = _mean;
111  }
112 
113 
117  float getMean() const {
118  return _mean;
119  }
120 
121 
125  float getVariance() const {
126  return _variance;
127  }
128 
133  float getStandardDeviation() const {
134  return sqrt(_variance);
135  }
136 
137 
138 
139 private:
140  float _previous_mean, _mean, _variance;
142  const uint8_t WINDOW_LENGTH_AS_RSHIFT;
143 };
144 
145 // no need to show the specialisations
148 #endif // #ifndef ROLLINGSTAT_H
T getStandardDeviation() const
Return the approximate standard deviation of the last WINDOW_LENGTH number of inputs.
Definition: RollingStat.h:77
T getVariance() const
Return the approximate variance of the last WINDOW_LENGTH number of inputs.
Definition: RollingStat.h:70
void update(int8_t x)
Update the mean and variance given a new input value.
Definition: RollingStat.h:50
T getMean() const
Return the mean of the last WINDOW_LENGTH number of inputs.
Definition: RollingStat.h:60
void update(T x)
Update the mean and variance given a new input value.
Definition: RollingStat.h:40
RollingStat()
Constructor.
Definition: RollingStat.h:32
Calculates a running average over a specified number of the most recent readings.
WARNING: this class is work in progress, don't use it yet.
Definition: RollingStat.h:28
constexpr uint8_t trailingZerosConst(unsigned long v)
Given a power of 2, work out the number to shift right by to do a divide by the number,...
Definition: mozzi_utils.h:73