diff --git a/include/etl/debounce.h b/include/etl/debounce.h index 14d96ee9..5fc461be 100644 --- a/include/etl/debounce.h +++ b/include/etl/debounce.h @@ -55,13 +55,13 @@ namespace etl void add_sample(bool sample) { // Changed from last time? - if (sample != bool((flags & SAMPLE) != 0)) + if (sample != bool((flags & Sample) != 0)) { count = 0; - flags = (flags & ~SAMPLE) | (sample ? SAMPLE : 0); + flags = (flags & ~Sample) | (sample ? Sample : 0); } - flags &= ~CHANGE; + flags &= ~Change; } //************************************************************************* @@ -70,7 +70,7 @@ namespace etl //************************************************************************* bool has_changed() const { - return (flags & CHANGE) != 0; + return (flags & Change) != 0; } //************************************************************************* @@ -79,7 +79,7 @@ namespace etl //************************************************************************* bool is_set() const { - return ((flags & STATE) > OFF); + return ((flags & State) > Off); } //************************************************************************* @@ -88,7 +88,7 @@ namespace etl //************************************************************************* bool is_held() const { - return (flags & STATE) > ON; + return (flags & State) > On; } //************************************************************************* @@ -97,27 +97,27 @@ namespace etl //************************************************************************* bool is_repeating() const { - return ((flags & STATE) == REPEATING); + return ((flags & State) == Repeating); } + protected: + enum states { - OFF = 0, - ON = 1, - HELD = 2, - REPEATING = 3, - STATE = 0x03U, - SAMPLE = 4, - CHANGE = 8 + Off = 0, + On = 1, + Held = 2, + Repeating = 3, + State = 0x03U, + Sample = 4, + Change = 8 }; - protected: - //************************************************************************* /// Constructor. //************************************************************************* debounce_base(bool initial_state) - : flags(initial_state ? ON : OFF), + : flags(initial_state ? On : Off), count(0) { } @@ -134,21 +134,21 @@ namespace etl //************************************************************************* void get_next(bool sample, bool condition_set, bool condition_clear, const uint_least8_t state_table[][2]) { - int index1 = ((flags & STATE) * 2) + (sample ? 1 : 0); + int index1 = ((flags & State) * 2) + (sample ? 1 : 0); int index2 = (sample ? (condition_set ? 0 : 1) : (condition_clear ? 0 : 1)); flags_t next = flags; - next &= ~STATE; + next &= ~State; next |= state_table[index1][index2]; if (next != flags) { - next |= CHANGE; + next |= Change; } else { - next &= ~CHANGE; + next &= ~Change; } flags = next; @@ -184,10 +184,10 @@ namespace etl { static ETL_CONSTANT uint_least8_t state_table[4][2] = { - /* OFF 0 */{ debounce_base::OFF, debounce_base::OFF }, - /* OFF 1 */{ debounce_base::ON, debounce_base::OFF }, - /* ON 0 */{ debounce_base::OFF, debounce_base::ON }, - /* ON 1 */{ debounce_base::ON, debounce_base::ON }, + /* Off 0 */{ debounce_base::Off, debounce_base::Off }, + /* Off 1 */{ debounce_base::On, debounce_base::Off }, + /* On 0 */{ debounce_base::Off, debounce_base::On }, + /* On 1 */{ debounce_base::On, debounce_base::On }, }; get_next(sample, condition_set, condition_clear, state_table); @@ -206,15 +206,15 @@ namespace etl bool valid = (count == valid_count); - switch (flags & STATE) + switch (flags & State) { - case OFF: + case Off: { set_state(sample, valid, valid); break; } - case ON: + case On: { set_state(sample, valid, valid); break; @@ -227,12 +227,12 @@ namespace etl } } - if (flags & CHANGE) + if (flags & Change) { count = 0; } - return (flags & CHANGE); + return (flags & Change); } }; @@ -262,12 +262,12 @@ namespace etl { static ETL_CONSTANT uint_least8_t state_table[6][2] = { - /* OFF 0 */{ debounce_base::OFF, debounce_base::OFF }, - /* OFF 1 */{ debounce_base::ON, debounce_base::OFF }, - /* ON 0 */{ debounce_base::OFF, debounce_base::ON }, - /* ON 1 */{ debounce_base::HELD, debounce_base::ON }, - /* HELD 0 */{ debounce_base::OFF, debounce_base::HELD }, - /* HELD 1 */{ debounce_base::HELD, debounce_base::HELD } + /* Off 0 */{ debounce_base::Off, debounce_base::Off }, + /* Off 1 */{ debounce_base::On, debounce_base::Off }, + /* On 0 */{ debounce_base::Off, debounce_base::On }, + /* On 1 */{ debounce_base::Held, debounce_base::On }, + /* Held 0 */{ debounce_base::Off, debounce_base::Held }, + /* Held 1 */{ debounce_base::Held, debounce_base::Held } }; get_next(sample, condition_set, condition_clear, state_table); @@ -287,21 +287,21 @@ namespace etl bool valid = (count == valid_count); bool hold = (count == hold_count); - switch (flags & STATE) + switch (flags & State) { - case OFF: + case Off: { set_state(sample, valid, valid); break; } - case ON: + case On: { set_state(sample, hold, valid); break; } - case HELD: + case Held: { set_state(sample, hold, valid); break; @@ -314,12 +314,12 @@ namespace etl } } - if (flags & CHANGE) + if (flags & Change) { count = 0; } - return (flags & CHANGE); + return (flags & Change); } }; @@ -349,14 +349,14 @@ namespace etl { static ETL_CONSTANT uint_least8_t state_table[8][2] = { - /* OFF 0 */{ debounce_base::OFF, debounce_base::OFF }, - /* OFF 1 */{ debounce_base::ON, debounce_base::OFF }, - /* ON 0 */{ debounce_base::OFF, debounce_base::ON }, - /* ON 1 */{ debounce_base::HELD, debounce_base::ON }, - /* HELD 0 */{ debounce_base::OFF, debounce_base::HELD }, - /* HELD 1 */{ debounce_base::REPEATING, debounce_base::HELD }, - /* REPEATING 0 */{ debounce_base::OFF, debounce_base::REPEATING }, - /* REPEATING 1 */{ debounce_base::REPEATING, debounce_base::REPEATING } + /* Off 0 */{ debounce_base::Off, debounce_base::Off }, + /* Off 1 */{ debounce_base::On, debounce_base::Off }, + /* On 0 */{ debounce_base::Off, debounce_base::On }, + /* On 1 */{ debounce_base::Held, debounce_base::On }, + /* Held 0 */{ debounce_base::Off, debounce_base::Held }, + /* Held 1 */{ debounce_base::Repeating, debounce_base::Held }, + /* Repeating 0 */{ debounce_base::Off, debounce_base::Repeating }, + /* Repeating 1 */{ debounce_base::Repeating, debounce_base::Repeating } }; get_next(sample, condition_set, condition_clear, state_table); @@ -377,34 +377,34 @@ namespace etl bool hold = (count == hold_count); bool repeat = (count == repeat_count); - switch (flags & STATE) + switch (flags & State) { - case OFF: + case Off: { set_state(sample, valid, valid); break; } - case ON: + case On: { set_state(sample, hold, valid); break; } - case HELD: + case Held: { set_state(sample, repeat, valid); break; } - case REPEATING: + case Repeating: { set_state(sample, repeat, valid); if (sample && repeat) { count = 0; - flags |= CHANGE; + flags |= Change; } break; } @@ -416,12 +416,12 @@ namespace etl } } - if (flags & CHANGE) + if (flags & Change) { count = 0; } - return (flags & CHANGE); + return (flags & Change); } }; } diff --git a/include/etl/cumulative_moving_average.h b/include/etl/pseudo_moving_average.h similarity index 88% rename from include/etl/cumulative_moving_average.h rename to include/etl/pseudo_moving_average.h index 087bc2fe..73c3d7c5 100644 --- a/include/etl/cumulative_moving_average.h +++ b/include/etl/pseudo_moving_average.h @@ -36,7 +36,7 @@ SOFTWARE. namespace etl { - namespace private_cumulative_moving_average + namespace private_pseudo_moving_average { //*************************************************** /// add_insert_iterator @@ -95,7 +95,7 @@ namespace etl const size_t SCALING = 1U, const bool IsIntegral = etl::is_integral::value, const bool IsFloat = etl::is_floating_point::value> - class cumulative_moving_average; + class pseudo_moving_average; //*************************************************************************** /// Cumulative Moving Average @@ -105,11 +105,11 @@ namespace etl /// \tparam SCALING The scaling factor applied to samples. Default = 1. //*************************************************************************** template - class cumulative_moving_average + class pseudo_moving_average { private: - typedef cumulative_moving_average this_t; + typedef pseudo_moving_average this_t; typedef typename etl::conditional::value, int32_t, uint32_t>::type scale_t; typedef typename etl::conditional::value, int32_t, uint32_t>::type sample_t; @@ -120,7 +120,7 @@ namespace etl public: typedef T value_type; - typedef private_cumulative_moving_average::add_insert_iterator add_insert_iterator; + typedef private_pseudo_moving_average::add_insert_iterator add_insert_iterator; static ETL_CONSTANT size_t SAMPLE_SIZE = SAMPLE_SIZE_; ///< The number of samples averaged over. static ETL_CONSTANT size_t SCALING = SCALING_; ///< The sample scaling factor. @@ -129,7 +129,7 @@ namespace etl /// Constructor /// \param initial_value The initial value for the average. //************************************************************************* - cumulative_moving_average(const T initial_value) + pseudo_moving_average(const T initial_value) : average(initial_value * SCALE) { } @@ -155,7 +155,7 @@ namespace etl } //************************************************************************* - /// Gets the current cumulative average. + /// Gets the current pseudo moving average. /// \return The current average. //************************************************************************* T value() const @@ -174,7 +174,7 @@ namespace etl private: - T average; ///< The current cumulative average. + T average; ///< The current pseudo moving average. }; //*************************************************************************** @@ -184,9 +184,9 @@ namespace etl /// \tparam SCALING The scaling factor applied to samples. Default = 1. //*************************************************************************** template - class cumulative_moving_average + class pseudo_moving_average { - typedef cumulative_moving_average this_t; + typedef pseudo_moving_average this_t; typedef typename etl::conditional::value, int32_t, uint32_t>::type scale_t; typedef typename etl::conditional::value, int32_t, uint32_t>::type sample_t; @@ -196,7 +196,7 @@ namespace etl public: typedef T value_type; - typedef private_cumulative_moving_average::add_insert_iterator add_insert_iterator; + typedef private_pseudo_moving_average::add_insert_iterator add_insert_iterator; static ETL_CONSTANT size_t SCALING = SCALING_; ///< The sample scaling factor. @@ -204,7 +204,7 @@ namespace etl /// Constructor /// \param initial_value The initial value for the average. //************************************************************************* - cumulative_moving_average(const T initial_value, const size_t sample_size) + pseudo_moving_average(const T initial_value, const size_t sample_size) : average(initial_value * SCALE) , samples(sample_t(sample_size)) { @@ -240,7 +240,7 @@ namespace etl } //************************************************************************* - /// Gets the current cumulative average. + /// Gets the current pseudo moving average. /// \return The current average. //************************************************************************* T value() const @@ -259,7 +259,7 @@ namespace etl private: - T average; ///< The current cumulative average. + T average; ///< The current pseudo moving average. sample_t samples; ///< The number of samples to average over. }; @@ -270,14 +270,14 @@ namespace etl /// \tparam SAMPLE_SIZE The number of samples to average over. //*************************************************************************** template - class cumulative_moving_average + class pseudo_moving_average { - typedef cumulative_moving_average this_t; + typedef pseudo_moving_average this_t; public: typedef T value_type; - typedef private_cumulative_moving_average::add_insert_iterator add_insert_iterator; + typedef private_pseudo_moving_average::add_insert_iterator add_insert_iterator; static ETL_CONSTANT size_t SAMPLE_SIZE = SAMPLE_SIZE_; @@ -285,7 +285,7 @@ namespace etl /// Constructor /// \param initial_value The initial value for the average. //************************************************************************* - cumulative_moving_average(const T initial_value) + pseudo_moving_average(const T initial_value) : reciprocal_samples_plus_1(T(1.0) / T(SAMPLE_SIZE_ + 1U)) , average(initial_value) { @@ -310,7 +310,7 @@ namespace etl } //************************************************************************* - /// Gets the current cumulative average. + /// Gets the current pseudo moving average. /// \return The current average. //************************************************************************* T value() const @@ -330,7 +330,7 @@ namespace etl private: const T reciprocal_samples_plus_1; ///< Reciprocal of one greater than the sample size. - T average; ///< The current cumulative average. + T average; ///< The current pseudo moving average. }; //*************************************************************************** @@ -340,20 +340,20 @@ namespace etl /// \tparam SAMPLE_SIZE The number of samples to average over. //*************************************************************************** template - class cumulative_moving_average + class pseudo_moving_average { - typedef cumulative_moving_average this_t; + typedef pseudo_moving_average this_t; public: typedef T value_type; - typedef private_cumulative_moving_average::add_insert_iterator add_insert_iterator; + typedef private_pseudo_moving_average::add_insert_iterator add_insert_iterator; //************************************************************************* /// Constructor /// \param initial_value The initial value for the average. //************************************************************************* - cumulative_moving_average(const T initial_value, const size_t sample_size) + pseudo_moving_average(const T initial_value, const size_t sample_size) : reciprocal_samples_plus_1(T(1.0) / T(sample_size + 1U)) , average(initial_value) { @@ -387,7 +387,7 @@ namespace etl } //************************************************************************* - /// Gets the current cumulative average. + /// Gets the current pseudo moving average. /// \return The current average. //************************************************************************* T value() const @@ -407,7 +407,7 @@ namespace etl private: T reciprocal_samples_plus_1; ///< Reciprocal of one greater than the sample size. - T average; ///< The current cumulative average. + T average; ///< The current pseudo moving average. }; } diff --git a/support/Release notes.txt b/support/Release notes.txt index 746e82fd..73d21210 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -13,6 +13,8 @@ Refactors etl::vector and etl::deque resize() to take const reference parameter Renamed ETL_ALWAYS_ASSERT to ETL_ASSERT_FAIL Removed duplicate void_t definition Removed duplicate etl::declvar definition +Renamed cumulative_moving_average to pseudo_windowed_moving_average to more accurately reflect its algorithm. +Changed etl::debounce internal state names to avoid clashes with Arduino macros. =============================================================================== 20.28.0 diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 296e7237..dde57b83 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -118,7 +118,6 @@ set(TEST_SOURCE_FILES test_crc8_maxim.cpp test_crc8_rohc.cpp test_crc8_wcdma.cpp - test_cumulative_moving_average.cpp test_cyclic_value.cpp test_debounce.cpp test_delegate.cpp @@ -200,6 +199,7 @@ set(TEST_SOURCE_FILES test_pool.cpp test_pool_external_buffer.cpp test_priority_queue.cpp + test_pseudo_moving_average.cpp test_quantize.cpp test_queue.cpp test_queue_lockable.cpp diff --git a/test/sanity-check/cumulative_moving_average.h.t.cpp b/test/sanity-check/cumulative_moving_average.h.t.cpp index 995f3731..1904ce74 100644 --- a/test/sanity-check/cumulative_moving_average.h.t.cpp +++ b/test/sanity-check/cumulative_moving_average.h.t.cpp @@ -26,4 +26,4 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ******************************************************************************/ -#include +#include diff --git a/test/test_cumulative_moving_average.cpp b/test/test_pseudo_moving_average.cpp similarity index 86% rename from test/test_cumulative_moving_average.cpp rename to test/test_pseudo_moving_average.cpp index 6b7d54fb..384a7a36 100644 --- a/test/test_cumulative_moving_average.cpp +++ b/test/test_pseudo_moving_average.cpp @@ -31,7 +31,7 @@ SOFTWARE. #include #include -#include "etl/cumulative_moving_average.h" +#include "etl/pseudo_moving_average.h" #include "etl/scaled_rounding.h" namespace @@ -39,12 +39,12 @@ namespace const size_t SAMPLE_SIZE = 10UL; const size_t SCALING = 100UL; - SUITE(test_cumulative_moving_average) + SUITE(test_pseudo_moving_average) { //************************************************************************* TEST(integral_signed_average_positive) { - using CMA = etl::cumulative_moving_average; + using CMA = etl::pseudo_moving_average; CMA cma(0); CHECK_EQUAL(0, cma.value()); @@ -67,7 +67,7 @@ namespace { std::array data{ 9, 1, 8, 2, 7, 3, 6, 4, 5 }; - using CMA = etl::cumulative_moving_average; + using CMA = etl::pseudo_moving_average; CMA cma(0); CHECK_EQUAL(0, cma.value()); @@ -80,7 +80,7 @@ namespace //************************************************************************* TEST(integral_signed_average_negative) { - using CMA = etl::cumulative_moving_average; + using CMA = etl::pseudo_moving_average; CMA cma(0); CHECK_EQUAL(0, cma.value()); @@ -103,7 +103,7 @@ namespace { std::array data{ -9, -1, -8, -2, -7, -3, -6, -4, -5 }; - using CMA = etl::cumulative_moving_average; + using CMA = etl::pseudo_moving_average; CMA cma(0); CHECK_EQUAL(0, cma.value()); @@ -116,7 +116,7 @@ namespace //************************************************************************* TEST(integral_unsigned_average_positive) { - using CMA = etl::cumulative_moving_average; + using CMA = etl::pseudo_moving_average; CMA cma(0U); CHECK_EQUAL(0U, cma.value()); @@ -139,7 +139,7 @@ namespace { std::array data{ 9U, 1U, 8U, 2U, 7U, 3U, 6U, 4U, 5U }; - using CMA = etl::cumulative_moving_average; + using CMA = etl::pseudo_moving_average; CMA cma(0U); CHECK_EQUAL(0U, cma.value()); @@ -152,7 +152,7 @@ namespace //************************************************************************* TEST(integral_signed_average_positive_runtime_sample_size) { - using CMA = etl::cumulative_moving_average; + using CMA = etl::pseudo_moving_average; CMA cma(0, SAMPLE_SIZE * 2U); CHECK_EQUAL(0, cma.value()); @@ -177,7 +177,7 @@ namespace { std::array data{ 9, 1, 8, 2, 7, 3, 6, 4, 5 }; - using CMA = etl::cumulative_moving_average; + using CMA = etl::pseudo_moving_average; CMA cma(0, SAMPLE_SIZE * 2U); CHECK_EQUAL(0, cma.value()); @@ -192,7 +192,7 @@ namespace //************************************************************************* TEST(integral_signed_average_negative_runtime_sample_size) { - using CMA = etl::cumulative_moving_average; + using CMA = etl::pseudo_moving_average; CMA cma(0, SAMPLE_SIZE * 2U); CHECK_EQUAL(0, cma.value()); @@ -217,7 +217,7 @@ namespace { std::array data{ -9, -1, -8, -2, -7, -3, -6, -4, -5 }; - using CMA = etl::cumulative_moving_average; + using CMA = etl::pseudo_moving_average; CMA cma(0, SAMPLE_SIZE * 2U); CHECK_EQUAL(0, cma.value()); @@ -232,7 +232,7 @@ namespace //************************************************************************* TEST(integral_unsigned_average_positive_runtime_sample_size) { - using CMA = etl::cumulative_moving_average; + using CMA = etl::pseudo_moving_average; CMA cma(0U, SAMPLE_SIZE * 2U); CHECK_EQUAL(0U, cma.value()); @@ -257,7 +257,7 @@ namespace { std::array data{ 9U, 1U, 8U, 2U, 7U, 3U, 6U, 4U, 5U }; - using CMA = etl::cumulative_moving_average; + using CMA = etl::pseudo_moving_average; CMA cma(0U, SAMPLE_SIZE * 2U); CHECK_EQUAL(0U, cma.value()); @@ -272,7 +272,7 @@ namespace //************************************************************************* TEST(floating_point_average) { - using CMA = etl::cumulative_moving_average; + using CMA = etl::pseudo_moving_average; CMA cma(0); CHECK_EQUAL(0.0, cma.value()); @@ -295,7 +295,7 @@ namespace { std::array data{ 9.0, 1.0, 8.0, 2.0, 7.0, 3.0, 6.0, 4.0, 5.0 }; - using CMA = etl::cumulative_moving_average; + using CMA = etl::pseudo_moving_average; CMA cma(0); CHECK_EQUAL(0.0, cma.value()); @@ -308,7 +308,7 @@ namespace //************************************************************************* TEST(floating_point_average_runtime_sample_size) { - using CMA = etl::cumulative_moving_average; + using CMA = etl::pseudo_moving_average; CMA cma(0, SAMPLE_SIZE * 2); CHECK_EQUAL(0.0, cma.value()); @@ -333,7 +333,7 @@ namespace { std::array data{ 9.0, 1.0, 8.0, 2.0, 7.0, 3.0, 6.0, 4.0, 5.0 }; - using CMA = etl::cumulative_moving_average; + using CMA = etl::pseudo_moving_average; CMA cma(0, SAMPLE_SIZE * 2); CHECK_EQUAL(0.0, cma.value()); diff --git a/test/vs2019/etl.vcxproj b/test/vs2019/etl.vcxproj index fc89c487..bbec9b79 100644 --- a/test/vs2019/etl.vcxproj +++ b/test/vs2019/etl.vcxproj @@ -2411,7 +2411,7 @@ - + @@ -10973,7 +10973,7 @@ - + diff --git a/test/vs2019/etl.vcxproj.filters b/test/vs2019/etl.vcxproj.filters index 7c94ce46..edb08c81 100644 --- a/test/vs2019/etl.vcxproj.filters +++ b/test/vs2019/etl.vcxproj.filters @@ -588,7 +588,7 @@ ETL\Frameworks - + ETL\Maths @@ -1790,7 +1790,7 @@ Tests\Algorithms - + Tests\Algorithms