19 #ifndef ROLLINGAVERAGE_H
20 #define ROLLINGAVERAGE_H
35 template <
class T,
int WINDOW_LENGTH>
53 for (
int thisReading = 0; thisReading < WINDOW_LENGTH; thisReading++)
54 readings[thisReading] = 0;
65 return add(input)>>WINDOW_LENGTH_AS_RSHIFT;
74 total -= readings[index];
77 readings[index] = input;
80 ++index &= WINDOW_LENGTH -1;
86 T readings[WINDOW_LENGTH];
89 const uint8_t WINDOW_LENGTH_AS_RSHIFT;
99 template <
int WINDOW_LENGTH>
110 RollingAverage():index(0),total(0), WINDOW_LENGTH_AS_RSHIFT(trailingZerosConst(WINDOW_LENGTH))
113 for (
int thisReading = 0; thisReading < WINDOW_LENGTH; thisReading++)
114 readings[thisReading] = 0;
122 unsigned int next(
unsigned int input)
127 return (
unsigned)add(input)>>WINDOW_LENGTH_AS_RSHIFT;
134 unsigned int add(
unsigned int input){
136 total -= readings[index];
139 readings[index] = input;
142 ++index &= WINDOW_LENGTH -1;
148 unsigned int readings[WINDOW_LENGTH];
151 const uint8_t WINDOW_LENGTH_AS_RSHIFT;
158 template <
int WINDOW_LENGTH>
169 RollingAverage():index(0),total(0.0)
172 for (
int thisReading = 0; thisReading < WINDOW_LENGTH; thisReading++)
173 readings[thisReading] = 0.0;
181 float next(
float input)
184 total -= readings[index];
187 readings[index] = input;
190 ++index &= WINDOW_LENGTH -1;
193 return total/WINDOW_LENGTH;
197 float readings[WINDOW_LENGTH];