Plaquette
 
Loading...
Searching...
No Matches
MovingFilter.h
1/*
2 * MovingFilter.h
3 *
4 * (c) 2022 Sofian Audry :: info(@)sofianaudry(.)com
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef MOVING_FILTER_H_
21#define MOVING_FILTER_H_
22
23#include "PqCore.h"
24#include "TimeWindowable.h"
25#include "pq_moving_average.h"
26
27namespace pq {
28
29// Max value of _nValuesStep.
30#define MOVING_FILTER_N_VALUES_STEP_MAX 63
31constexpr float MOVING_FILTER_N_VALUES_STEP_MAX_MINUS_ONE = MOVING_FILTER_N_VALUES_STEP_MAX - 1;
32
33// Precomputed proportion to add one value to _nValuesStep when it has reached MOVING_FILTER_N_VALUES_STEP_MAX.
34constexpr float MOVING_FILTER_VALUES_STEP_ADD_ONE_PROPORTION = MOVING_FILTER_N_VALUES_STEP_MAX / (MOVING_FILTER_N_VALUES_STEP_MAX + 1.0f);
35
36// N. standard deviations considered in full range.
37#define MOVING_FILTER_N_STDDEV_RANGE 5.0f
38
40protected:
46
53
54 virtual ~MovingFilter() {}
55
56public:
58 virtual void reset();
59
61 virtual void reset(float estimatedMeanValue);
62
64 virtual void reset(float estimatedMinValue, float estimatedMaxValue);
65
70 virtual void resumeCalibrating();
71
76 virtual void pauseCalibrating();
77
79 virtual void toggleCalibrating();
80
82 virtual bool isCalibrating() const;
83
85 unsigned int nSamples() const { return _nSamples; }
86
88 virtual bool isPreInitialized() const { return _isPreInitialized; }
89
91 virtual float filter(float value) = 0;
92
93protected:
94 virtual void begin();
95
96 // Returns the instantaneous moving average alpha for this filter.
97 virtual float alpha() const {
98 return movingAverageAlpha(sampleRate(), timeWindow(), nSamples(), isPreInitialized());
99 }
100
101 // Start/stop calibration flag.
102 bool _isCalibrating : 1;
103 bool _isPreInitialized : 1;
104 uint8_t _nValuesStep : 6;
105
106 // Number of samples that have been processed thus far.
107 unsigned int _nSamples;
108};
109
110}
111
112#endif
An analog analog source that contains a value constrained to a finite range (typically in [0,...
Definition PqCore.h:482
The main Plaquette static class containing all the units.
Definition PqCore.h:63
static Engine & primary()
Returns the main instance of Plaquette.
Definition PqCore.cpp:30
Definition MovingFilter.h:39
virtual float filter(float value)=0
Returns the filtered value (without calibrating).
virtual void resumeCalibrating()
Switches to calibration mode (default).
Definition MovingFilter.cpp:58
virtual bool isCalibrating() const
Returns true iff the moving filter is in calibration mode.
Definition MovingFilter.cpp:70
virtual void toggleCalibrating()
Toggles calibration mode.
Definition MovingFilter.cpp:66
virtual void reset()
Resets the filter.
Definition MovingFilter.cpp:40
virtual void pauseCalibrating()
Switches to non-calibration mode: calls to put(value) will return filtered value without updating the...
Definition MovingFilter.cpp:62
virtual bool isPreInitialized() const
Returns true if the moving filter has been initialized with a starting range at reset.
Definition MovingFilter.h:88
unsigned int nSamples() const
Returns the number of samples that have been processed thus far.
Definition MovingFilter.h:85
Abstract class for time-based objects.
Definition TimeWindowable.h:28
virtual float timeWindow() const
Returns the time window (expressed in seconds).
Definition TimeWindowable.h:45
Engine * engine() const
Returns the engine that owns this unit.
Definition PqCore.h:420
float sampleRate() const
Returns engine sample rate.
Definition PqCore.h:398