-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathmoving_average.h
More file actions
58 lines (49 loc) · 1.74 KB
/
Copy pathmoving_average.h
File metadata and controls
58 lines (49 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#ifndef SENSP_TRANSFORMS_MOVING_AVERAGE_H_
#define SENSP_TRANSFORMS_MOVING_AVERAGE_H_
#include <vector>
#include "sensesp/ui/config_item.h"
#include "transform.h"
namespace sensesp {
/**
* @brief Outputs the moving average of the last sample_size inputs.
*
* Used to smooth the output of a value (signal) that has
* frequent variations. For example, the output of a temperature sensor may vary
* from 180 to 185 several times over a short period, but you just want to see
* the average of that. MovingAverage outputs the average of the most recent
* sample_size values. It also incorporates a "scale" factor, in case you want
* to increase or decrease your final output by a fixed percentage.
*/
// y = k * 1/n * \sum_k=1^n(x_k)
class MovingAverage : public FloatTransform {
public:
/**
* @param sample_size The number of most recent values you want to average for
* your output.
*
* @param multiplier Moving average will be multiplied by multiplier before it
* is output - make it something other than 1. if you need to scale your
* output up or down by a fixed percentage.
*
* @param config_path The path used to configure this transform in the Config
* UI.
* */
MovingAverage(int sample_size, float multiplier = 1.0,
const String& config_path = "");
virtual void set(const float& input) override;
virtual bool to_json(JsonObject& root) override;
virtual bool from_json(const JsonObject& config) override;
private:
std::vector<float> buf_{};
int ptr_ = 0;
int sample_size_;
float multiplier_;
float sum_ = 0;
bool initialized_;
};
const String ConfigSchema(const MovingAverage& obj);
inline bool ConfigRequiresRestart(const MovingAverage& obj) {
return true;
}
} // namespace sensesp
#endif