Plaquette
 
Loading...
Searching...
No Matches
MovingAverage.h
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
28#include <Arduino.h>
29#include <limits.h>
30#include <float.h>
31
32namespace pq {
33
36public:
39
42
43 virtual ~MovingAverage() {}
44
46 void infiniteTimeWindow();
47
49 void timeWindow(float seconds);
50
52 float timeWindow() const { return _smoothTime; }
53
55 bool timeWindowIsInfinite() const;
56
58 void cutoff(float hz);
59
61 float cutoff() const;
62
64 float alpha(float sampleRate) const;
65
67 void reset();
68
70 virtual float update(float v, float sampleRate=1, bool forceAlpha=false);
71
73 float get() { return _value; }
74 float constGet() const { return _value; }
75
77 unsigned int nSamples() const { return _nSamples; }
78
80 void amendUpdate(float previousValue, float newValue, float sampleRate=1, bool forceAlpha=false);
81
86 static void applyUpdate(float& runningValue, float newValue, float alpha);
87
89 static void applyAmendUpdate(float& runningValue, float previousValue, float newValue, float alpha);
90
95 static float computeUpdate(float runningValue, float newValue, float alpha);
96
98 static float alpha(float sampleRate, float timeWindow, unsigned int nSamples=UINT_MAX);
99
100protected:
101 // The smoothing window (in seconds).
102 float _smoothTime;
103
104 // The current value of the exponential moving average.
105 float _value;
106
107 // Number of samples that have been processed thus far.
108 unsigned int _nSamples;
109};
110
111} // namespace pq
112
113#endif /* MOVINGAVERAGE_H_ */
An exponential moving average class.
Definition MovingAverage.h:35
float alpha(float sampleRate) const
Returns the alpha value computed from given sample rate.
Definition MovingAverage.cpp:68
MovingAverage()
Default constructor (infinite time window).
Definition MovingAverage.cpp:34
bool timeWindowIsInfinite() const
Returns true if time window is infinite.
Definition MovingAverage.cpp:64
float get()
Returns the value of the moving average. This is undefined if isValid() == false.
Definition MovingAverage.h:73
virtual float update(float v, float sampleRate=1, bool forceAlpha=false)
Updates the moving average with new value #v# (also returns the current value).
Definition MovingAverage.cpp:76
float timeWindow() const
Returns the smoothing window (expressed in seconds).
Definition MovingAverage.h:52
static void applyAmendUpdate(float &runningValue, float previousValue, float newValue, float alpha)
Performs an amendment of latest update (needs to be called with same alpha# parameter).
Definition MovingAverage.cpp:92
static void applyUpdate(float &runningValue, float newValue, float alpha)
Applies a single update on #runningValue# with new sample #newValue# and mixing factor alpha#.
Definition MovingAverage.cpp:96
void amendUpdate(float previousValue, float newValue, float sampleRate=1, bool forceAlpha=false)
Performs an amendment of latest update (needs to be called with same sampleRate# and #forceAlpha# par...
Definition MovingAverage.cpp:88
void reset()
Resets the moving average.
Definition MovingAverage.cpp:72
static float computeUpdate(float runningValue, float newValue, float alpha)
Computes a single update on #runningValue# with new sample #newValue# and mixing factor alpha#.
Definition MovingAverage.cpp:100
unsigned int nSamples() const
Returns the number of samples processed since reset().
Definition MovingAverage.h:77
void infiniteTimeWindow()
Sets to "infinite" smoothing window.
Definition MovingAverage.cpp:44
float cutoff() const
Returns the smoothing window cutoff frequency (expressed in Hz).
Definition MovingAverage.cpp:60