Plaquette
 
Loading...
Searching...
No Matches
Normalizer.h
1/*
2 * Normalizer.h
3 *
4 * (c) 2015 Sofian Audry :: info(@)sofianaudry(.)com
5 * (c) 2015 Thomas O Fredericks :: tof(@)t-o-f(.)info
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#ifndef NORMALIZER_H_
22#define NORMALIZER_H_
23
24#include "PqCore.h"
25#include "MovingFilter.h"
26#include "MovingStats.h"
27
28namespace pq {
29
30// Default mean and standard deviation.
31constexpr float NORMALIZER_DEFAULT_MEAN = 0.5f;
32constexpr float NORMALIZER_DEFAULT_STDDEV = 1.0f / MOVING_FILTER_N_STDDEV_RANGE;
33// sum_i x_i^2 = stddev^2 + (sum_i x_i)^2
34constexpr float NORMALIZER_DEFAULT_MEAN2 = sq(NORMALIZER_DEFAULT_STDDEV) + sq(NORMALIZER_DEFAULT_MEAN);
35
36// This is so that the basic normalizer will stay within range [0, 1].
37constexpr float NORMALIZER_DEFAULT_CLAMP_STDDEV = 0.5f * MOVING_FILTER_N_STDDEV_RANGE - FLT_MIN;
38
39// Label for no clamping.
40#define NORMALIZER_NO_CLAMP 0
41
46class Normalizer : public MovingFilter, public MovingStats {
47public:
54
62
69
76 Normalizer(float mean, float stdDev, float timeWindow, Engine& engine = Engine::primary());
77
78 virtual ~Normalizer() {}
79
84 void targetMean(float mean) { _targetMean = mean; }
85
87 float targetMean() const { return _targetMean; }
88
93 void targetStdDev(float stdDev) { _targetStdDev = abs(stdDev); }
94
96 float targetStdDev() const { return _targetStdDev; }
97
98
100 virtual void reset();
101
103 virtual void reset(float estimatedMeanValue);
104
106 virtual void reset(float estimatedMinValue, float estimatedMaxValue);
107
114 virtual float put(float value);
115
117 virtual float filter(float value);
118
123 virtual float lowOutlierThreshold(float nStdDev=1.5f) const;
124
129 virtual float highOutlierThreshold(float nStdDev=1.5f) const;
130
132 bool isClamped() const;
133
139 void clamp(float nStdDev=NORMALIZER_DEFAULT_CLAMP_STDDEV);
140
142 void noClamp();
143
145 virtual float mapTo(float toLow, float toHigh);
146
147protected:
148 virtual void step();
149
150 // Helper function for constructors.
151 void _init(float mean, float stdDev);
152
153 // Returns clamped value.
154 float _clamp(float value) const;
155
156 // Target normalization parameters.
157 float _targetMean;
158 float _targetStdDev;
159
160 // Clamped standard deviation (if 0 = no clamp).
161 float _clampStdDev;
162
163 // Variables used to compute current value average during a step (in case of multiple calls to put()).
164 float _currentMeanStep;
165 float _currentMean2Step;
166};
167
168}
169
170#endif
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
Definition MovingStats.h:30
virtual float stdDev() const
Returns the standard deviation of the samples.
Definition MovingStats.cpp:55
virtual float mean() const
Returns an exponential moving average of the samples.
Definition MovingStats.h:53
Adaptive normalizer: normalizes values on-the-run using exponential moving averages over mean and sta...
Definition Normalizer.h:46
void targetMean(float mean)
Sets target mean of normalized values.
Definition Normalizer.h:84
void noClamp()
Remove clamping.
Definition Normalizer.cpp:164
void clamp(float nStdDev=NORMALIZER_DEFAULT_CLAMP_STDDEV)
Assign clamping value.
Definition Normalizer.cpp:160
virtual void reset()
Resets the statistics.
Definition Normalizer.cpp:47
virtual float mapTo(float toLow, float toHigh)
Maps value to new range.
Definition Normalizer.cpp:185
virtual float lowOutlierThreshold(float nStdDev=1.5f) const
Returns value above which value is considered to be a low outler (below average).
Definition Normalizer.cpp:148
float targetMean() const
Returns target mean.
Definition Normalizer.h:87
virtual float put(float value)
Pushes value into the unit.
Definition Normalizer.cpp:71
bool isClamped() const
Return true iff the normalized value is clamped within reasonable range.
Definition Normalizer.cpp:156
virtual float filter(float value)
Returns the filtered value (without calibrating).
Definition Normalizer.cpp:116
float targetStdDev() const
Returns target standard deviation.
Definition Normalizer.h:96
void targetStdDev(float stdDev)
Sets target standard deviation of normalized values.
Definition Normalizer.h:93
virtual float highOutlierThreshold(float nStdDev=1.5f) const
Returns value above which value is considered to be a high outler (above average).
Definition Normalizer.cpp:152
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