|
3 | 3 |
|
4 | 4 | #include "filter.h"
|
5 | 5 |
|
| 6 | +#include <type_traits> |
| 7 | + |
6 | 8 | // An exponential moving average filter. This uses only integer (32-bit) math.
|
7 | 9 | // This supports up to 24-bit inputs.
|
8 | 10 | //
|
|
13 | 15 | //
|
14 | 16 | // An alpha of 255 means that the filter returns the current value of the input.
|
15 | 17 | // An alpha of 0 means the filtered value changes very slowly.
|
16 |
| -template <typename OutputType> |
17 |
| -class ExponentialMovingAverageFilter : public Filter<uint32_t, OutputType> { |
18 |
| - using Filter<uint32_t, OutputType>::sensor_value_; |
| 18 | +template <typename InputType, typename OutputType> |
| 19 | +class ExponentialMovingAverageFilter : public Filter<InputType, OutputType> { |
| 20 | + using Filter<InputType, OutputType>::sensor_value_; |
19 | 21 |
|
20 | 22 | public:
|
21 |
| - ExponentialMovingAverageFilter(uint32_t (*const ReadFromSensor)(), |
22 |
| - uint8_t alpha); |
23 |
| - ExponentialMovingAverageFilter(uint32_t (*const ReadFromSensor)(), |
24 |
| - uint8_t alpha, |
25 |
| - OutputType (*Convert)(uint32_t input)); |
| 23 | + ExponentialMovingAverageFilter(InputType (*const ReadFromSensor)(), |
| 24 | + InputType alpha); |
| 25 | + ExponentialMovingAverageFilter(InputType (*const ReadFromSensor)(), |
| 26 | + InputType alpha, |
| 27 | + OutputType (*Convert)(InputType input)); |
26 | 28 |
|
27 | 29 | protected:
|
28 |
| - uint32_t DoRun() override; |
| 30 | + InputType DoRun() override; |
29 | 31 |
|
30 | 32 | private:
|
31 |
| - uint32_t average_ = 0; |
| 33 | + InputType average_ = 0; |
32 | 34 |
|
33 |
| - const uint8_t alpha_; |
| 35 | + const InputType alpha_; |
34 | 36 | };
|
35 | 37 |
|
36 |
| -template <typename OutputType> |
37 |
| -ExponentialMovingAverageFilter<OutputType>::ExponentialMovingAverageFilter( |
38 |
| - uint32_t (*const ReadFromSensor)(), uint8_t alpha) |
39 |
| - : Filter<uint32_t, OutputType>(ReadFromSensor), alpha_(alpha) {} |
40 |
| - |
41 |
| -template <typename OutputType> |
42 |
| -ExponentialMovingAverageFilter<OutputType>::ExponentialMovingAverageFilter( |
43 |
| - uint32_t (*const ReadFromSensor)(), uint8_t alpha, |
44 |
| - OutputType (*Convert)(uint32_t input)) |
45 |
| - : Filter<uint32_t, OutputType>(ReadFromSensor, Convert), alpha_(alpha) {} |
46 |
| - |
47 |
| -template <typename OutputType> |
48 |
| -uint32_t ExponentialMovingAverageFilter<OutputType>::DoRun() { |
49 |
| - uint32_t old_average = average_; |
50 |
| - average_ = (sensor_value_ * (alpha_ + 1) + (average_ * (255 - alpha_))) / 256; |
51 |
| - if (old_average == average_ && sensor_value_ != average_) { |
52 |
| - if (sensor_value_ > average_) { |
53 |
| - average_++; |
54 |
| - } else if (sensor_value_ < average_) { |
55 |
| - average_--; |
| 38 | +template <typename InputType, typename OutputType> |
| 39 | +ExponentialMovingAverageFilter<InputType, OutputType>:: |
| 40 | + ExponentialMovingAverageFilter(InputType (*const ReadFromSensor)(), |
| 41 | + InputType alpha) |
| 42 | + : Filter<InputType, OutputType>(ReadFromSensor), alpha_(alpha) {} |
| 43 | + |
| 44 | +template <typename InputType, typename OutputType> |
| 45 | +ExponentialMovingAverageFilter<InputType, OutputType>:: |
| 46 | + ExponentialMovingAverageFilter(InputType (*const ReadFromSensor)(), |
| 47 | + InputType alpha, |
| 48 | + OutputType (*Convert)(InputType input)) |
| 49 | + : Filter<InputType, OutputType>(ReadFromSensor, Convert), alpha_(alpha) {} |
| 50 | + |
| 51 | +template <typename InputType, typename OutputType> |
| 52 | +InputType ExponentialMovingAverageFilter<InputType, OutputType>::DoRun() { |
| 53 | + InputType old_average = average_; |
| 54 | + if (std::is_integral<InputType>::value) { |
| 55 | + average_ = |
| 56 | + (sensor_value_ * (alpha_ + 1) + (average_ * (255 - alpha_))) / 256; |
| 57 | + if (old_average == average_ && sensor_value_ != average_) { |
| 58 | + if (sensor_value_ > average_) { |
| 59 | + average_++; |
| 60 | + } else if (sensor_value_ < average_) { |
| 61 | + average_--; |
| 62 | + } |
56 | 63 | }
|
| 64 | + } else { |
| 65 | + average_ = (sensor_value_ * (alpha_) + (average_ * (1.0 - alpha_))); |
57 | 66 | }
|
58 | 67 | return average_;
|
59 | 68 | }
|
|
0 commit comments