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