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#define NORMALIZER_DEFAULT_MEAN 0.5f
31#define NORMALIZER_DEFAULT_STDDEV 0.15f
32
33// sum_i x_i^2 = stddev^2 + (sum_i x_i)^2
34constexpr float NORMALIZER_DEFAULT_MEAN2 = (NORMALIZER_DEFAULT_STDDEV*NORMALIZER_DEFAULT_STDDEV) + (NORMALIZER_DEFAULT_MEAN*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 / NORMALIZER_DEFAULT_STDDEV - FLT_MIN;
38
39#define NORMALIZER_NO_CLAMP 0
40
45class Normalizer : public MovingFilter, public MovingStats {
46public:
53
61
68
75 Normalizer(float mean, float stdDev, float timeWindow, Engine& engine = Engine::primary());
76
77 virtual ~Normalizer() {}
78
83 void targetMean(float mean) { _targetMean = mean; }
84
86 float targetMean() const { return _targetMean; }
87
92 void targetStdDev(float stdDev) { _targetStdDev = abs(stdDev); }
93
95 float targetStdDev() const { return _targetStdDev; }
96
98 virtual void infiniteTimeWindow();
99
101 virtual void timeWindow(float seconds);
102
104 virtual float timeWindow() const;
105
107 virtual bool timeWindowIsInfinite() const;
108
110 virtual void reset();
111
118 virtual float put(float value);
119
124 virtual float lowOutlierThreshold(float nStdDev=1.5f) const;
125
130 virtual float highOutlierThreshold(float nStdDev=1.5f) const;
131
133 bool isClamped() const;
134
140 void clamp(float nStdDev=NORMALIZER_DEFAULT_CLAMP_STDDEV);
141
143 void noClamp();
144
146 virtual float mapTo(float toLow, float toHigh);
147
148protected:
149 virtual void step();
150 virtual float update(float value, float sampleRate=1);
151
152 // Helper function for constructors.
153 void _init(float mean, float stdDev);
154
155 // Returns clamped value.
156 float _clamp(float value) const;
157
158 // Target normalization parameters.
159 float _targetMean;
160 float _targetStdDev;
161
162 // Clamped standard deviation (if 0 = no clamp).
163 float _clampStdDev;
164
165 // Variables used to compute current value average during a step (in case of multiple calls to put()).
166 float _currentMeanStep;
167 float _currentMean2Step;
168};
169
170}
171
172#endif
The main Plaquette static class containing all the units.
Definition PqCore.h:60
static Engine & primary()
Returns the main instance of Plaquette.
Definition PqCore.cpp:31
Definition MovingFilter.h:29
Definition MovingStats.h:30
virtual float stdDev() const
Returns the standard deviation of the samples.
Definition MovingStats.cpp:63
virtual float mean() const
Returns an exponential moving average of the samples.
Definition MovingStats.h:73
Adaptive normalizer: normalizes values on-the-run using exponential moving averages over mean and sta...
Definition Normalizer.h:45
void targetMean(float mean)
Sets target mean of normalized values.
Definition Normalizer.h:83
void noClamp()
Remove clamping.
Definition Normalizer.cpp:159
void clamp(float nStdDev=NORMALIZER_DEFAULT_CLAMP_STDDEV)
Assign clamping value.
Definition Normalizer.cpp:155
virtual void reset()
Resets the statistics.
Definition Normalizer.cpp:64
virtual float mapTo(float toLow, float toHigh)
Maps value to new range.
Definition Normalizer.cpp:182
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:143
virtual bool timeWindowIsInfinite() const
Returns true if time window is infinite.
Definition Normalizer.cpp:60
float targetMean() const
Returns target mean.
Definition Normalizer.h:86
virtual void infiniteTimeWindow()
Sets time window to infinite.
Definition Normalizer.cpp:50
virtual float put(float value)
Pushes value into the unit.
Definition Normalizer.cpp:69
bool isClamped() const
Return true iff the normalized value is clamped within reasonable range.
Definition Normalizer.cpp:151
virtual float update(float value, float sampleRate=1)
Adds a value to the statistics (returns the mean).
Definition Normalizer.cpp:127
float targetStdDev() const
Returns target standard deviation.
Definition Normalizer.h:95
void targetStdDev(float stdDev)
Sets target standard deviation of normalized values.
Definition Normalizer.h:92
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:147
virtual float timeWindow() const
Returns the time window (expressed in seconds).
Definition Normalizer.cpp:58
float seconds() const
Returns engine time in seconds.
Definition PqCore.h:348
Engine * engine() const
Returns the engine that owns this unit.
Definition PqCore.h:382