Mozzi  version v2.0
sound synthesis library for Arduino
RollingAverage.h
1 /*
2  * RollingAverage.h
3  *
4  Draws on Arduino Smoothing example,
5  Created 22 April 2007
6  By David A. Mellis <dam@mellis.org>
7  modified 9 Apr 2012
8  by Tom Igoe
9  http://www.arduino.cc/en/Tutorial/Smoothing
10 
11  * This file is part of Mozzi.
12  *
13  * Copyright 2013-2024 Tim Barrass and the Mozzi Team
14  *
15  * Mozzi is licensed under the GNU Lesser General Public Licence (LGPL) Version 2.1 or later.
16  *
17  */
18 
19 #ifndef ROLLINGAVERAGE_H
20 #define ROLLINGAVERAGE_H
21 
22 #include "mozzi_utils.h" // for trailingZeros()
23 
24 
35 template <class T, int WINDOW_LENGTH>
36 class
38 
39 public:
40 
50  RollingAverage():index(0),total(0), WINDOW_LENGTH_AS_RSHIFT(trailingZerosConst(WINDOW_LENGTH))
51  {
52  // initialize all the readings to 0:
53  for (int thisReading = 0; thisReading < WINDOW_LENGTH; thisReading++)
54  readings[thisReading] = 0;
55  }
56 
57 
63  T next(T input)
64  {
65  return add(input)>>WINDOW_LENGTH_AS_RSHIFT;
66  }
67 
68 
69 protected:
70 
71  inline
72  T add(T input){
73  // out with the old
74  total -= readings[index];
75  // in with the new
76  total += input;
77  readings[index] = input;
78 
79  // advance and wrap index
80  ++index &= WINDOW_LENGTH -1;
81  return total;
82  }
83 
84 
85 private:
86  T readings[WINDOW_LENGTH]; // the readings from the analog input
87  unsigned int index; // the index of the current reading
88  long total; // the running total
89  const uint8_t WINDOW_LENGTH_AS_RSHIFT;
90 
91 };
92 
93 // no need to show the specialisations
99 template <int WINDOW_LENGTH>
100 class RollingAverage <unsigned int, WINDOW_LENGTH>
101 {
102 public:
110  RollingAverage():index(0),total(0), WINDOW_LENGTH_AS_RSHIFT(trailingZerosConst(WINDOW_LENGTH))
111  {
112  // initialize all the readings to 0:
113  for (int thisReading = 0; thisReading < WINDOW_LENGTH; thisReading++)
114  readings[thisReading] = 0;
115  }
116 
122  unsigned int next(unsigned int input)
123  {
124  // calculate the average:
125  // this unsigned cast is the only difference between the int and unsigned int specialisations
126  // it tells the shift not to sign extend in from the left
127  return (unsigned)add(input)>>WINDOW_LENGTH_AS_RSHIFT;
128  }
129 
130 protected:
131 
132 
133  inline
134  unsigned int add(unsigned int input){
135  // out with the old
136  total -= readings[index];
137  // in with the new
138  total += input;
139  readings[index] = input;
140 
141  // advance and wrap index
142  ++index &= WINDOW_LENGTH -1;
143  return total;
144  }
145 
146 
147 private:
148  unsigned int readings[WINDOW_LENGTH]; // the readings from the analog input
149  unsigned int index; // the index of the current reading
150  long total; // the running total
151  const uint8_t WINDOW_LENGTH_AS_RSHIFT;
152 
153 };
154 
155 
156 
158 template <int WINDOW_LENGTH>
159 class RollingAverage <float, WINDOW_LENGTH>
160 {
161 public:
169  RollingAverage():index(0),total(0.0)
170  {
171  // initialize all the readings to 0:
172  for (int thisReading = 0; thisReading < WINDOW_LENGTH; thisReading++)
173  readings[thisReading] = 0.0;
174  }
175 
181  float next(float input)
182  {
183  // out with the old
184  total -= readings[index];
185  // in with the new
186  total += input;
187  readings[index] = input;
188 
189  // advance and wrap index
190  ++index &= WINDOW_LENGTH -1;
191 
192  // calculate the average:
193  return total/WINDOW_LENGTH;
194  }
195 
196 private:
197  float readings[WINDOW_LENGTH]; // the readings from the analog input
198  unsigned int index; // the index of the current reading
199  float total; // the running total
200 
201 };
202 
203 
204 // no need to show the specialisations
213 #endif // #ifndef ROLLINGAVERAGE_H
RollingAverage()
Constructor.
T next(T input)
Give the average of the last WINDOW_LENGTH.
Calculates a running average over a specified number of the most recent readings.
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