Mozzi  version 2015-05-11-20:23
sound synthesis library for Arduino
 All Classes Functions Typedefs Groups
RollingStat.h
1 #ifndef ROLLINGSTAT_H
2 #define ROLLINGSTAT_H
3 
4 /*
5  * RollingStat.h
6  *
7  * Copyright 2013 Tim Barrass.
8  *
9  * This file is part of Mozzi.
10  *
11  * Mozzi is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
12  *
13  */
14 
15 #include "RollingAverage.h"
16 #include "mozzi_fixmath.h"
17 
18 // adapted from RunningStat, http://www.johndcook.com/standard_deviation.html
24 template <class T, int WINDOW_LENGTH>
26 {
27 public:
28 
30  RollingStat() : _previous_mean(0), _mean(0), _variance(0), WINDOW_LENGTH_AS_RSHIFT((uint8_t)trailingZeros((unsigned long)WINDOW_LENGTH))
31  {}
32 
33 
38  void update(T x) {
39  _mean = rollingMean.next(x);
40  _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
41  _previous_mean = _mean;
42  }
43 
44 
48  void update(int8_t x) {
49  _mean = rollingMean.next(x);
50  _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
51  _previous_mean = _mean;
52  }
53 
54 
58  T getMean() const {
59  return _mean;
60  }
61 
62 
68  T getVariance() const {
69  return _variance;
70  }
71 
76  return isqrt16(_variance);
77  }
78 
79 
80 
81 private:
82  T _previous_mean, _mean, _variance;
84  const uint8_t WINDOW_LENGTH_AS_RSHIFT;
85 };
86 
87 // no need to show the specialisations
91 template <int WINDOW_LENGTH>
92 class RollingStat <float, WINDOW_LENGTH>
93 {
94 public:
95 
97  RollingStat() : _previous_mean(0), _mean(0), _variance(0), WINDOW_LENGTH_AS_RSHIFT((uint8_t)trailingZeros((unsigned long)WINDOW_LENGTH))
98  {}
99 
100 
105  void update(float x) {
106  _mean = rollingMean.next(x);
107  _variance = ((x - _previous_mean)*(x - _mean))/(WINDOW_LENGTH-1);
108  _previous_mean = _mean;
109  }
110 
111 
115  float getMean() const {
116  return _mean;
117  }
118 
119 
123  float getVariance() const {
124  return _variance;
125  }
126 
131  float getStandardDeviation() const {
132  return sqrt(_variance);
133  }
134 
135 
136 
137 private:
138  float _previous_mean, _mean, _variance;
140  const uint8_t WINDOW_LENGTH_AS_RSHIFT;
141 };
142 
143 // no need to show the specialisations
146 #endif // #ifndef ROLLINGSTAT_H
147 
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.
Definition: mozzi_utils.cpp:8
T getStandardDeviation() const
Return the approximate standard deviation of the last WINDOW_LENGTH number of inputs.
Definition: RollingStat.h:75
T getMean() const
Return the mean of the last WINDOW_LENGTH number of inputs.
Definition: RollingStat.h:58
void update(int8_t x)
Update the mean and variance given a new input value.
Definition: RollingStat.h:48
Calculates a running average over a specified number of the most recent readings. ...
RollingStat()
Constructor.
Definition: RollingStat.h:30
void update(T x)
Update the mean and variance given a new input value.
Definition: RollingStat.h:38
Calculates an approximation of the variance and standard deviation for a window of recent inputs...
Definition: RollingStat.h:25
T getVariance() const
Return the approximate variance of the last WINDOW_LENGTH number of inputs.
Definition: RollingStat.h:68