From 7fa4acddadab2e48940ab2c3546571f5f8343be8 Mon Sep 17 00:00:00 2001 From: Alexandre Hiroyuki Date: Mon, 23 Sep 2024 15:42:41 -0300 Subject: [PATCH] feat(DataTomeCumulative): :sparkles: add DataTomeCumulative class add DataTomeCumulative to calculate the cumulative average #9 --- README.md | 7 ++++ .../moving_average_print.ino | 2 +- .../partials_example/partials_example.ino | 2 +- library.json | 4 +- library.properties | 2 +- src/DataTome.h | 1 + src/DataTomeCumulative.h | 37 +++++++++++++++++++ src/DataTomeMvAvg.h | 12 +++--- 8 files changed, 56 insertions(+), 11 deletions(-) create mode 100644 src/DataTomeCumulative.h diff --git a/README.md b/README.md index f92f318..12933ad 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,11 @@ Data Tome is a C++ library for data analysis and data filtering on embedded devices (IoT). Focus on the developer's experience and performance. +- Simple Moving Average (SMA). +- Exponential Moving Average (EMA). +- Simple Moving Median (implemented on DataTomeAnalysis). +- Variance, Standard Deviation, and more. + ## Getting Started - This library is listed in the official [Arduino Library Manager](https://www.arduino.cc/reference/en/libraries/datatome/). @@ -36,6 +41,8 @@ This library calculates statistical functions using a time-series sample impleme [Read here how to contribute](https://github.com/AlexandreHiroyuki/DataTome/blob/master/CONTRIBUTING.md). +It describes how to report issues, code conventions, testing, and how to publish a package on the PlatformIO Registry. + ## Developed by **Alexandre Hiroyuki** – [GitHub](https://github.com/AlexandreHiroyuki) – [LinkedIn](https://www.linkedin.com/in/alexandre-hiroyuki-yamauchi/) diff --git a/examples/moving_average_print/moving_average_print.ino b/examples/moving_average_print/moving_average_print.ino index 7505e45..54d0fdd 100644 --- a/examples/moving_average_print/moving_average_print.ino +++ b/examples/moving_average_print/moving_average_print.ino @@ -4,7 +4,7 @@ // Create an Arithmetic Moving Average object of unsigned int type, // 10 in size -DataTomeMvAvg test(10); +DataTomeMvAvg test(10); // This variable just generates input for average test unsigned delta_x = 0; diff --git a/examples/partials_example/partials_example.ino b/examples/partials_example/partials_example.ino index d8392f0..6369349 100644 --- a/examples/partials_example/partials_example.ino +++ b/examples/partials_example/partials_example.ino @@ -4,7 +4,7 @@ // Create an Arithmetic Moving Average object of unsigned int type, // 10 in size -DataTomeMvAvg integer_mv(10); +DataTomeMvAvg integer_mv(10); // This variable just generates input for average integer_mv unsigned delta_x = 0; diff --git a/library.json b/library.json index 04b6f29..418c18d 100644 --- a/library.json +++ b/library.json @@ -1,9 +1,9 @@ { "$schema": "https://raw.githubusercontent.com/platformio/platformio-core/develop/platformio/assets/schema/library.json", "name": "DataTome", - "version": "1.7.0", + "version": "1.8.0", "description": "Data analysis and filtering using time series for embedded devices (IoT). All in a single C++ library, Data Tome. Focus on the developer's experience and performance.", - "keywords": "sensors, input, data, processing, analysis, arduino, library, filter, moving average, smooth, standard, deviation, mean, partial, tome, datatome", + "keywords": "sensors, input, data, processing, analysis, arduino, library, filter, moving average, smooth, standard, deviation, mean, median, partial, tome, datatome", "repository": { "type": "git", "url": "https://github.com/AlexandreHiroyuki/DataTome" diff --git a/library.properties b/library.properties index 53dfc04..2591048 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=DataTome -version=1.7.0 +version=1.8.0 author=Alexandre Hiroyuki Yamauchi maintainer=Alexandre Hiroyuki Yamauchi sentence=Data analysis and filtering using time series for embedded devices (IoT). All in a single C++ library, Data Tome. diff --git a/src/DataTome.h b/src/DataTome.h index 1098627..42ebf72 100644 --- a/src/DataTome.h +++ b/src/DataTome.h @@ -8,6 +8,7 @@ #define DATA_TOME_H #include +#include #include #include diff --git a/src/DataTomeCumulative.h b/src/DataTomeCumulative.h new file mode 100644 index 0000000..7fa9935 --- /dev/null +++ b/src/DataTomeCumulative.h @@ -0,0 +1,37 @@ +/*************************************************************** + DataTomeCumulative.h + Created by Alexandre Hiroyuki Yamauchi, September 23, 2024. +***************************************************************/ + +#ifndef DATA_TOME_CUMULATIVE_H +#define DATA_TOME_CUMULATIVE_H + +#include + +// WARNING: using this class with integer types may result in loss of precision +// due to cumulative integer divisions rounding. +template +class DataTomeCumulative { + protected: + TypeOfSum _cumulative_average; + unsigned long int _count; + + public: + DataTomeCumulative() : _cumulative_average(0), _count(0) {} + + DataTomeCumulative &push(TypeOfSum input) { + if (_count >= ULONG_MAX) { + _cumulative_average = 0; + _count = 0; + } + + _cumulative_average += (input - _cumulative_average) / (_count + 1); + + _count++; + return *this; + } + + TypeOfSum get() { return _cumulative_average; } +}; + +#endif // DATA_TOME_CUMULATIVE_H \ No newline at end of file diff --git a/src/DataTomeMvAvg.h b/src/DataTomeMvAvg.h index aa189df..c817261 100644 --- a/src/DataTomeMvAvg.h +++ b/src/DataTomeMvAvg.h @@ -71,11 +71,11 @@ class DataTomeMvAvg { TypeOfArray get() { return (_average_sum / ((_average_counter == 0) ? 1 : _average_counter)); } - TypeOfArray get(size_t n_points) { + inline TypeOfArray get(size_t n_points) { // @alias get_by_brute return get_by_brute(n_points); } - TypeOfArray mean() { + inline TypeOfArray mean() { // @alias get return get(); } @@ -131,7 +131,7 @@ class DataTomeMvAvg { size_t size_of_memory() { return _array_size * sizeof(TypeOfArray); } size_t point_count() { return _average_counter; } - size_t count() { + inline size_t count() { // @alias point_count return point_count(); } @@ -151,7 +151,7 @@ class DataTomeMvAvg { _array_size = new_size; return *this; } - DataTomeMvAvg &resize(size_t new_size) { + inline DataTomeMvAvg &resize(size_t new_size) { // @alias grow return grow(new_size); } @@ -207,7 +207,7 @@ class DataTomeMvAvg { return _partial_sums[id] / ((_average_counter == 0) ? 1 : _average_counter); } - TypeOfArray partial_mean(size_t id) { + inline TypeOfArray partial_mean(size_t id) { // @alias partial_get return partial_get(id); } @@ -228,7 +228,7 @@ class DataTomeMvAvg { else return _average_counter; } - size_t partial_count(size_t id) { + inline size_t partial_count(size_t id) { // @alias partial_point_count return partial_point_count(id); }