Skip to content

Commit

Permalink
Add exponential moving average (#17)
Browse files Browse the repository at this point in the history
* test(test): ✅ add build tests on github workflows

#13

* test(test): ✅ fix build test arduino uno env

#13

* feat(DataTomeCumulative): ✨ add DataTomeCumulative class

add DataTomeCumulative to calculate the cumulative average

#9

* feat(DataTomeExpAvg): ✨ add DataTomeExpAvg with unit tests

#9 #19
  • Loading branch information
AlexandreHiroyuki authored Sep 24, 2024
1 parent 5ca50c8 commit c0692b1
Show file tree
Hide file tree
Showing 18 changed files with 280 additions and 104 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ jobs:
env:
PLATFORMIO_AUTH_TOKEN: ${{ secrets.PLATFORMIO_AUTH_TOKEN }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.9'
- name: Install PlatformIO Core
Expand Down
9 changes: 7 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ jobs:
os: [ubuntu-latest, macos-latest, windows-latest]

steps:
- uses: actions/checkout@v3
- uses: actions/cache@v3
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: |
~/.cache/pip
Expand All @@ -26,3 +26,8 @@ jobs:
pip install platformio
- name: Run Tests on the Desktop Environment
run: platformio test -e desktop
- name: Run Build Test for Arduino Uno
run: platformio run -e arduino_uno -t build_test.cpp
- name: Run Build Test for ESP32 Dev Module
run: platformio run -e esp32dev -t build_test.cpp

4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
"test",
"readme",
"library_metadata",
"DataTome"
"DataTome",
"DataTomeCumulative",
"DataTomeExpAvg"
]
}
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/)
59 changes: 59 additions & 0 deletions build_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Include lib:
#include <Arduino.h>
#include <DataTome.h>

// Create an Arithmetic Moving Average object of unsigned int type,
// 10 in size
DataTomeMvAvg<int, long int> test(10);
DataTomeAnalysis<int, long int> test2(10);
DataTomeCumulative<double> test3;
DataTomeExpAvg<double> test4;

// This variable just generates input for average test
int delta_x = 0;

void setup() {
// Initialize serial interface
Serial.begin(9600);
}

void loop() {
// Pushes the input in the moving average object
test.push(delta_x);
test2.push(delta_x);
test3.push(delta_x);
test4.push(delta_x);

// Generates the next input
delta_x += 3;
if (delta_x > 1000) delta_x = 0;

// Prints each value stored in the moving average
for (uint8_t i = 0; i < test.size(); i++) {
Serial.print(test[i]);
Serial.print(" ");
}
// Prints the result of the average
Serial.print("= ");
Serial.print(test.get());
// Prints the value stored in the first and last indexes
Serial.print(" | f: ");
Serial.print(test.front());
Serial.print(" b: ");
Serial.println(test.back());

Serial.print("Analysis: ");
Serial.print(test2.mean());
Serial.print(" | Std: ");
Serial.print(test2.std());
Serial.print(" | Median: ");
Serial.println(test2.median());

Serial.print("Cumulative: ");
Serial.println(test3.get());

Serial.print(" | ExpAvg: ");
Serial.println(test4.get());

delay(1000);
}
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
39 changes: 0 additions & 39 deletions include/README

This file was deleted.

46 changes: 0 additions & 46 deletions lib/README

This file was deleted.

14 changes: 10 additions & 4 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 All @@ -22,8 +22,11 @@
"platforms": "*",
"headers": [
"DataTome.h",
"DataTomeAnalysis.h",
"DataTomeCumulative.h",
"DataTomeExpAvg",
"DataTomeMvAvg.h",
"DataTomeAnalysis.h"
"DataTomeUtils.h"
],
"examples": [
{
Expand All @@ -43,9 +46,12 @@
],
"export": {
"exclude": [
"test/",
".github",
".gitignore",
".vscode"
".vscode",
"build_test.cpp",
"docs/icon.png"
]
},
"srcDir": "src"
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 <alex.hiroyuki@outlook.com>
maintainer=Alexandre Hiroyuki Yamauchi <alex.hiroyuki@outlook.com>
sentence=Data analysis and filtering using time series for embedded devices (IoT). All in a single C++ library, Data Tome.
Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ platform = espressif32
board = esp32dev
framework = arduino

[env:arduino_avr]
[env:arduino_uno]
framework = arduino
platform = atmelavr
board = uno
Expand Down
2 changes: 2 additions & 0 deletions src/DataTome.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#define DATA_TOME_H

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

Expand Down
38 changes: 38 additions & 0 deletions src/DataTomeCumulative.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/***************************************************************
DataTomeCumulative.h
Created by Alexandre Hiroyuki Yamauchi, September 23, 2024.
***************************************************************/

#ifndef DATA_TOME_CUMULATIVE_H
#define DATA_TOME_CUMULATIVE_H

#include <limits.h>

// RECOMMENDED: use double type for TypeOfSum
// WARNING: using this class with integer types may result in a loss of
// precision due to cumulative rounding of integer divisions.
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;
}
_count++;

_cumulative_average += (input - _cumulative_average) / (_count);

return *this;
}

TypeOfSum get() { return _cumulative_average; }
};

#endif // DATA_TOME_CUMULATIVE_H
39 changes: 39 additions & 0 deletions src/DataTomeExpAvg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/***************************************************************
DataTomeExpAvg.h
Created by Alexandre Hiroyuki Yamauchi, August 19, 2024.
***************************************************************/

#ifndef DATA_TOME_EXP_AVG_H
#define DATA_TOME_EXP_AVG_H

#include <limits.h>

// RECOMMENDED: use double type for TypeOfSum
// WARNING: using this class with integer types may result in a loss of
// precision due to cumulative rounding of integer divisions.
template <typename TypeOfSum>
class DataTomeExpAvg {
protected:
TypeOfSum _exp_avg;
unsigned long int _count;

public:
DataTomeExpAvg() : _exp_avg(0), _count(0) {}

DataTomeExpAvg<TypeOfSum> &push(TypeOfSum input) {
if (_count >= ULONG_MAX) {
_exp_avg = 0;
_count = 0;
}
_count++;

TypeOfSum multiplier = 2 / (_count);
_exp_avg = (input * multiplier) + (_exp_avg * (1 - multiplier));

return *this;
}

TypeOfSum get() { return _exp_avg; }
};

#endif // DATA_TOME_EXP_AVG_H
Loading

0 comments on commit c0692b1

Please sign in to comment.