FastLED 3.9.15
Loading...
Searching...
No Matches
triangular_filter_impl.h
Go to the documentation of this file.
1#pragma once
2
4#include "fl/log/log.h"
5#include "fl/math/math.h"
6#include "fl/stl/span.h"
7#include "fl/stl/noexcept.h"
9
10namespace fl {
11namespace detail {
12
13template <typename T, fl::size N = 0>
15 FL_STATIC_ASSERT(N == 0 || (N % 2 == 1),
16 "TriangularFilter: N must be odd for a symmetric tent shape");
17 public:
19 explicit TriangularFilterImpl(fl::size capacity)
20 : mBuf(capacity), mLastValue(T(0)) {
21 if (capacity % 2 == 0) {
22 FL_ERROR("TriangularFilter: capacity should be odd, adding 1");
24 }
25 }
26
27 T update(T input) {
28 mBuf.push_back(input);
29 return recompute();
30 }
31
33 if (values.size() == 0) return mLastValue;
34 for (fl::size i = 0; i < values.size(); ++i) {
35 mBuf.push_back(values[i]);
36 }
37 return recompute();
38 }
39
40 T value() const { return mLastValue; }
41 void reset() { mBuf.clear(); mLastValue = T(0); }
42 bool full() const { return mBuf.full(); }
43 fl::size size() const { return mBuf.size(); }
44 fl::size capacity() const { return mBuf.capacity(); }
45
46 void resize(fl::size new_capacity) {
47 if (new_capacity % 2 == 0) {
48 FL_ERROR("TriangularFilter: capacity should be odd, adding 1");
49 new_capacity += 1;
50 }
51 mBuf = circular_buffer<T, N>(new_capacity);
52 mLastValue = T(0);
53 }
54
55 private:
57 fl::size n = mBuf.size();
58 T weighted_sum = T(0);
59 T weight_total = T(0);
60 for (fl::size i = 0; i < n; ++i) {
61 fl::size w_int = fl::min(i + 1, n - i);
62 T w = T(static_cast<float>(w_int));
63 weighted_sum = weighted_sum + mBuf[i] * w;
64 weight_total = weight_total + w;
65 }
66 mLastValue = weighted_sum / weight_total;
67 return mLastValue;
68 }
69
72};
73
74} // namespace detail
75} // namespace fl
void resize(fl::size new_capacity)
T update(fl::span< const T > values)
FL_STATIC_ASSERT(N==0||(N % 2==1), "TriangularFilter: N must be odd for a symmetric tent shape")
constexpr fl::size size() const FL_NOEXCEPT
Definition span.h:458
#define FL_ERROR(X)
Definition log.h:219
Centralized logging categories for FastLED hardware interfaces and subsystems.
Compile-time linker keep-alive hook for a single fl::Bus.
Definition bus_traits.h:48
FL_DISABLE_WARNING_PUSH U constexpr common_type_t< T, U > min(T a, U b) FL_NOEXCEPT
Definition math.h:71
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT
Portable compile-time assertion wrapper.