Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
cs_Variance.h
Go to the documentation of this file.
1
8#include <math.h>
9
23private:
24 // running values
25 uint32_t num_recorded_values = 0;
26 float M2 = 0.0f;
27 float mean = 0.0f;
28
29 // these values are just estimated to be reasonable.
30 // reduceCount is called if M2 or num_recorded_values surpass
31 // these thresholds.
32 static const constexpr float float_precision_threshold = 10e9f;
33 static const constexpr int count_precision_threshold = 10e6;
34
35public:
39 void addValue(float new_measurement) {
42 }
43
44 float diff_with_old_mean = new_measurement - mean;
45
47 mean += diff_with_old_mean / num_recorded_values;
48
49 float diff_with_new_mean = new_measurement - mean;
50
51 M2 += diff_with_old_mean * diff_with_new_mean;
52 }
53
54 int getCount() const { return num_recorded_values; }
55
56 float getMean() const { return mean; }
57
58 float getVariance() const { return M2 / (num_recorded_values - 1); }
59
60 float getStandardDeviation() const { return sqrt(getVariance()); }
61
62 bool isNumericPrecisionLow() const {
64 }
65
77 void reduceCount() {
78 M2 /= (2 + 2 / (num_recorded_values - 2));
80 }
81
82 void reset() {
84 M2 = 0.0f;
85 mean = 0.0f;
86 }
87};
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: Nov 29,...
Definition: cs_Variance.h:22
static const constexpr int count_precision_threshold
Definition: cs_Variance.h:33
bool isNumericPrecisionLow() const
Definition: cs_Variance.h:62
uint32_t num_recorded_values
Definition: cs_Variance.h:25
int getCount() const
Definition: cs_Variance.h:54
float getStandardDeviation() const
Definition: cs_Variance.h:60
float getVariance() const
Definition: cs_Variance.h:58
float M2
Definition: cs_Variance.h:26
float mean
Definition: cs_Variance.h:27
void addValue(float new_measurement)
update the aggregated data with a new measurement.
Definition: cs_Variance.h:39
void reset()
Definition: cs_Variance.h:82
void reduceCount()
Reduce |M2| and num_recorded_values to prevent overflow.
Definition: cs_Variance.h:77
float getMean() const
Definition: cs_Variance.h:56
static const constexpr float float_precision_threshold
Definition: cs_Variance.h:32