Qualia  0.2
MovingAverage.h
Go to the documentation of this file.
1 /*
2  * MovingAverage.h
3  *
4  * Tool for moving averages.
5  *
6  * This file is part of Qualia https://github.com/sofian/qualia
7  *
8  * (c) 2011 Sofian Audry -- info(@)sofianaudry(.)com
9  * Inspired by code by Karsten Kutza
10  * http://www.ip-atlas.com/pub/nap/nn-src/bpn.txt
11  *
12  * This program is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>.
24  */
25 #ifndef MOVINGAVERAGE_H_
26 #define MOVINGAVERAGE_H_
27 
29  float _alpha;
30  float _value;
31 
32 public:
40  MovingAverage(float startValue, float alphaOrN) : _value(startValue) {
41  _alpha = (alphaOrN > 1 ?
42  2 / (alphaOrN - 1) :
43  alphaOrN);
44  }
45 
49  void update(float v) {
50  _value -= _alpha * (_value - v);
51  }
52 
56  float get() const { return _value; }
57 
61  void reset(float startValue) {
62  _value = startValue;
63  }
64 };
65 
66 #endif /* MOVINGAVERAGE_H_ */