15 #include "RollingAverage.h"
16 #include "mozzi_fixmath.h"
24 template <
class T,
int WINDOW_LENGTH>
30 RollingStat() : _previous_mean(0), _mean(0), _variance(0), WINDOW_LENGTH_AS_RSHIFT((uint8_t)
trailingZeros((unsigned long)WINDOW_LENGTH))
39 _mean = rollingMean.next(x);
40 _variance = (((long)x - _previous_mean)*((long)x - _mean))>>WINDOW_LENGTH_AS_RSHIFT;
41 _previous_mean = _mean;
49 _mean = rollingMean.next(x);
50 _variance = (((int)x - _previous_mean)*((int)x - _mean))>>WINDOW_LENGTH_AS_RSHIFT;
51 _previous_mean = _mean;
76 return isqrt16(_variance);
82 T _previous_mean, _mean, _variance;
84 const uint8_t WINDOW_LENGTH_AS_RSHIFT;
91 template <
int WINDOW_LENGTH>
97 RollingStat() : _previous_mean(0), _mean(0), _variance(0), WINDOW_LENGTH_AS_RSHIFT((uint8_t)
trailingZeros((unsigned long)WINDOW_LENGTH))
106 _mean = rollingMean.next(x);
107 _variance = ((x - _previous_mean)*(x - _mean))/(WINDOW_LENGTH-1);
108 _previous_mean = _mean;
132 return sqrt(_variance);
138 float _previous_mean, _mean, _variance;
140 const uint8_t WINDOW_LENGTH_AS_RSHIFT;
146 #endif // #ifndef ROLLINGSTAT_H
long trailingZeros(const unsigned long v)
Given a power of 2, work out the number to shift right by to do a divide by the number, or shift left to multiply.
T getStandardDeviation() const
Return the approximate standard deviation of the last WINDOW_LENGTH number of inputs.
T getMean() const
Return the mean of the last WINDOW_LENGTH number of inputs.
void update(int8_t x)
Update the mean and variance given a new input value.
Calculates a running average over a specified number of the most recent readings. ...
RollingStat()
Constructor.
void update(T x)
Update the mean and variance given a new input value.
Calculates an approximation of the variance and standard deviation for a window of recent inputs...
T getVariance() const
Return the approximate variance of the last WINDOW_LENGTH number of inputs.