Skip to content

Commit

Permalink
feat(DataTomeCumulative): ✨ add DataTomeCumulative class
Browse files Browse the repository at this point in the history
add DataTomeCumulative to calculate the cumulative average

#9
  • Loading branch information
AlexandreHiroyuki committed Sep 23, 2024
1 parent 0edbc8f commit 7fa4acd
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 11 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/).
Expand All @@ -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/)
2 changes: 1 addition & 1 deletion examples/moving_average_print/moving_average_print.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

// Create an Arithmetic Moving Average object of unsigned int type,
// 10 in size
DataTomeMvAvg<unsigned, unsigned long> test(10);
DataTomeMvAvg<int, long int> test(10);

// This variable just generates input for average test
unsigned delta_x = 0;
Expand Down
2 changes: 1 addition & 1 deletion examples/partials_example/partials_example.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

// Create an Arithmetic Moving Average object of unsigned int type,
// 10 in size
DataTomeMvAvg<unsigned, unsigned long> integer_mv(10);
DataTomeMvAvg<int, long int> integer_mv(10);

// This variable just generates input for average integer_mv
unsigned delta_x = 0;
Expand Down
4 changes: 2 additions & 2 deletions library.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=DataTome
version=1.7.0
version=1.8.0
author=Alexandre Hiroyuki Yamauchi <[email protected]>
maintainer=Alexandre Hiroyuki Yamauchi <[email protected]>
sentence=Data analysis and filtering using time series for embedded devices (IoT). All in a single C++ library, Data Tome.
Expand Down
1 change: 1 addition & 0 deletions src/DataTome.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define DATA_TOME_H

#include <DataTomeAnalysis.h>
#include <DataTomeCumulative.h>
#include <DataTomeMvAvg.h>
#include <DataTomeUtils.h>

Expand Down
37 changes: 37 additions & 0 deletions src/DataTomeCumulative.h
Original file line number Diff line number Diff line change
@@ -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 <limits.h>

// WARNING: using this class with integer types may result in loss of precision
// due to cumulative integer divisions rounding.
template <typename TypeOfSum>
class DataTomeCumulative {
protected:
TypeOfSum _cumulative_average;
unsigned long int _count;

public:
DataTomeCumulative() : _cumulative_average(0), _count(0) {}

DataTomeCumulative<TypeOfSum> &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
12 changes: 6 additions & 6 deletions src/DataTomeMvAvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand All @@ -151,7 +151,7 @@ class DataTomeMvAvg {
_array_size = new_size;
return *this;
}
DataTomeMvAvg<TypeOfArray, TypeOfSum> &resize(size_t new_size) {
inline DataTomeMvAvg<TypeOfArray, TypeOfSum> &resize(size_t new_size) {
// @alias grow
return grow(new_size);
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down

0 comments on commit 7fa4acd

Please sign in to comment.