FastLED 3.9.15
Loading...
Searching...
No Matches
weighted_moving_average_impl.h
Go to the documentation of this file.
1#pragma once
2
4#include "fl/stl/span.h"
5#include "fl/stl/noexcept.h"
6
7namespace fl {
8namespace detail {
9
10template <typename T, fl::size N = 0>
12 public:
15 : mBuf(capacity), mLastValue(T(0)) {}
16
17 T update(T input) {
18 mBuf.push_back(input);
19 return recompute();
20 }
21
23 if (values.size() == 0) return mLastValue;
24 for (fl::size i = 0; i < values.size(); ++i) {
25 mBuf.push_back(values[i]);
26 }
27 return recompute();
28 }
29
30 T value() const { return mLastValue; }
31 void reset() { mBuf.clear(); mLastValue = T(0); }
32 bool full() const { return mBuf.full(); }
33 fl::size size() const { return mBuf.size(); }
34 fl::size capacity() const { return mBuf.capacity(); }
35
36 void resize(fl::size new_capacity) {
37 mBuf = circular_buffer<T, N>(new_capacity);
38 mLastValue = T(0);
39 }
40
41 private:
43 fl::size n = mBuf.size();
44 T weighted_sum = T(0);
45 T weight_total = T(0);
46 for (fl::size i = 0; i < n; ++i) {
47 T w = T(static_cast<float>(i + 1));
48 weighted_sum = weighted_sum + mBuf[i] * w;
49 weight_total = weight_total + w;
50 }
51 mLastValue = weighted_sum / weight_total;
52 return mLastValue;
53 }
54
57};
58
59} // namespace detail
60} // namespace fl
constexpr fl::size size() const FL_NOEXCEPT
Definition span.h:458
Compile-time linker keep-alive hook for a single fl::Bus.
Definition bus_traits.h:48
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT