8 const String& config_path)
10 sample_size_{sample_size},
11 multiplier_{multiplier},
14 buf_.resize(sample_size_, 0);
22 buf_.assign(sample_size_, input);
27 output_ += -multiplier_ * buf_[ptr_] / sample_size_;
28 output_ += multiplier_ * input / sample_size_;
33 ptr_ = (ptr_ + 1) % sample_size_;
39 root[
"multiplier"] = multiplier_;
40 root[
"sample_size"] = sample_size_;
45 String
const expected[] = {
"multiplier",
"sample_size"};
46 for (
auto str : expected) {
47 if (!config[str].is<JsonVariant>()) {
51 multiplier_ = config[
"multiplier"];
52 int const n_new = config[
"sample_size"];
54 if (sample_size_ != n_new) {
55 buf_.assign(sample_size_, 0);
63 return R
"({"type":"object","properties":{"sample_size":{"title":"Number of samples in average","type":"integer"},"multiplier":{"title":"Multiplier","type":"number"}}})";
virtual bool load() override
Load and populate the object from a persistent storage.
Outputs the moving average of the last sample_size inputs.
virtual void set(const float &input) override
virtual bool to_json(JsonObject &root) override
virtual bool from_json(const JsonObject &config) override
MovingAverage(int sample_size, float multiplier=1.0, const String &config_path="")
const String ConfigSchema(const SmartSwitchController &obj)