Kuhnya Changed IP for MQTT. Added JLed

This commit is contained in:
Lexa
2025-08-21 11:07:47 +03:00
parent 4406739f56
commit c42f26105c
35 changed files with 2279 additions and 15 deletions

View File

@@ -0,0 +1,7 @@
compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
- uno
- leonardo
- due
- zero

View File

@@ -0,0 +1,13 @@
---
name: Arduino CI
on: [push, pull_request]
jobs:
arduino_ci:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: Arduino-CI/action@master
# Arduino-CI/action@v0.1.1

View File

@@ -0,0 +1 @@
{"type": "library", "name": "RunningMedian", "version": "0.3.3", "spec": {"owner": "robtillaart", "id": 1361, "name": "RunningMedian", "requirements": null, "url": null}}

View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2011-2021 Rob Tillaart
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,79 @@
[![Arduino CI](https://github.com/RobTillaart/RunningMedian/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/RunningMedian/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/RunningMedian.svg?maxAge=3600)](https://github.com/RobTillaart/RunningMedian/releases)
# RunningMedian
Arduino library to determine the running median by means of a circular buffer.
## Description
Running Median looks like a running average with a small but important twist.
Running average averages the last N samples while the running median takes
the last N samples, sort them and take the middle one, or the average of the
middle two in case the internal buffer size is even.
Important differences between running average and running median:
- Running median will return real data (e.g. a real sample from a sensor)
if one uses an odd size of the buffer (therefor preferred).
Running average may return a value that is never sampled.
- Running median will give zero weight to outliers, and 100% to the middle sample,
whereas running average gives the same weight to all samples.
- Running median will give often constant values for some time.
- As one knows the values in the buffer one can predict the maximum change of
the running median in the next steps in advance.
- Running median is slower as one needs to keep the values in timed order
to remove the oldest and keep them sorted to be able to select the median.
#### Note MEDIAN_MAX_SIZE
The maximum size of the internal buffer is defined by **MEDIAN_MAX_SIZE** and is
set to 255 (since version 0.3.1). The memory allocated currently is in the order
of 5 bytes per element plus some overhead, so 255 elements take ~1300 bytes.
For an UNO this is quite a bit.
With larger sizes the performance penalty to keep the internal array sorted
is large. For most applications a value much lower e.g. 19 is working well, and
is performance wise O(100x) faster in sorting than 255 elements.
## Interface
### Constructor
- **RunningMedian(const uint8_t size)** Constructor, dynamically allocates memory.
- **~RunningMedian()** Destructor
- **uint8_t getSize()** returns size of internal array
- **uint8_t getCount()** returns current used elements, getCount() <= getSize()
- **bool isFull()** returns true if the internal buffer is 100% filled.
### Base functions
- **clear()** resets internal buffer and variables, effectively emptird thr buffer.
- **add(const float value) ** adds a new value to internal buffer, optionally replacing the oldest element if the buffer is full
- **float getMedian()** returns the median == middle element
- **float getAverage()** returns average of **all** the values in the internal buffer
- **float getAverage(uint8_t nMedian)** returns average of **the middle n** values.
This effectively removes noise from the outliers in the samples.
- **float getHighest()** get the largest values in the buffer.
- **float getLowest()** get the smallest value in the buffer.
- **float getQuantile(const float q)** returns the Quantile value from the buffer.
This value is often interpolated.
### Less used functions
- **float getElement(const uint8_t n)** returns the n'th element from the values in time order.
- **float getSortedElement(const uint8_t n)** returns the n'th element from the values in size order (sorted ascending)
- **float predict(const uint8_t n)** predict the max change of median after n additions, n should be smaller than **getSize()/2**
## Operation
See examples

View File

@@ -0,0 +1,205 @@
//
// FILE: RunningMedian.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.3.3
// PURPOSE: RunningMedian library for Arduino
//
// HISTORY:
// 0.1.00 2011-02-16 initial version
// 0.1.01 2011-02-22 added remarks from CodingBadly
// 0.1.02 2012-03-15 added
// 0.1.03 2013-09-30 added _sorted flag, minor refactor
// 0.1.04 2013-10-17 added getAverage(uint8_t) - kudo's to Sembazuru
// 0.1.05 2013-10-18 fixed bug in sort; removes default constructor; dynamic memory
// 0.1.06 2013-10-19 faster sort, dynamic arrays, replaced sorted float array with indirection array
// 0.1.07 2013-10-19 add correct median if _count is even.
// 0.1.08 2013-10-20 add getElement(), add getSottedElement() add predict()
// 0.1.09 2014-11-25 float to double (support ARM)
// 0.1.10 2015-03-07 fix clear
// 0.1.11 2015-03-29 undo 0.1.10 fix clear
// 0.1.12 2015-07-12 refactor constructor + const
// 0.1.13 2015-10-30 fix getElement(n) - kudos to Gdunge
// 0.1.14 2017-07-26 revert double to float - issue #33
// 0.1.15 2018-08-24 make runningMedian Configurable #110
// 0.2.0 2020-04-16 refactor.
// 0.2.1 2020-06-19 fix library.json
// 0.2.2 2021-01-03 add Arduino-CI + unit tests
// 0.3.0 2021-01-04 malloc memory as default storage
// 0.3.1 2021-01-16 Changed size parameter to 255 max
// 0.3.2 2021-01-21 replaced bubbleSort by insertionSort
// --> better performance for large arrays.
// 0.3.3 2021-01-22 better insertionSort (+ cleanup test code)
#include "RunningMedian.h"
RunningMedian::RunningMedian(const uint8_t size)
{
_size = size;
if (_size < MEDIAN_MIN_SIZE) _size = MEDIAN_MIN_SIZE;
// if (_size > MEDIAN_MAX_SIZE) _size = MEDIAN_MAX_SIZE;
#ifdef RUNNING_MEDIAN_USE_MALLOC
_values = (float *) malloc(_size * sizeof(float));
_sortIdx = (uint8_t *) malloc(_size * sizeof(uint8_t));
#endif
clear();
}
RunningMedian::~RunningMedian()
{
#ifdef RUNNING_MEDIAN_USE_MALLOC
free(_values);
free(_sortIdx);
#endif
}
// resets all internal counters
void RunningMedian::clear()
{
_count = 0;
_index = 0;
_sorted = false;
for (uint8_t i = 0; i < _size; i++)
{
_sortIdx[i] = i;
}
}
// adds a new value to the data-set
// or overwrites the oldest if full.
void RunningMedian::add(float value)
{
_values[_index++] = value;
if (_index >= _size) _index = 0; // wrap around
if (_count < _size) _count++;
_sorted = false;
}
float RunningMedian::getMedian()
{
if (_count == 0) return NAN;
if (_sorted == false) sort();
if (_count & 0x01) // is it odd sized?
{
return _values[_sortIdx[_count / 2]];
}
return (_values[_sortIdx[_count / 2]] + _values[_sortIdx[_count / 2 - 1]]) / 2;
}
float RunningMedian::getQuantile(float q)
{
if (_count == 0) return NAN;
if ((q < 0) || (q > 1)) return NAN;
if (_sorted == false) sort();
const float id = (_count - 1) * q;
const uint8_t lo = floor(id);
const uint8_t hi = ceil(id);
const float qs = _values[_sortIdx[lo]];
const float h = (id - lo);
return (1.0 - h) * qs + h * _values[_sortIdx[hi]];
}
float RunningMedian::getAverage()
{
if (_count == 0) return NAN;
float sum = 0;
for (uint8_t i = 0; i < _count; i++)
{
sum += _values[i];
}
return sum / _count;
}
float RunningMedian::getAverage(uint8_t nMedians)
{
if ((_count == 0) || (nMedians == 0)) return NAN;
if (_count < nMedians) nMedians = _count; // when filling the array for first time
uint8_t start = ((_count - nMedians) / 2);
uint8_t stop = start + nMedians;
if (_sorted == false) sort();
float sum = 0;
for (uint8_t i = start; i < stop; i++)
{
sum += _values[_sortIdx[i]];
}
return sum / nMedians;
}
float RunningMedian::getElement(const uint8_t n)
{
if ((_count == 0) || (n >= _count)) return NAN;
uint8_t pos = _index + n;
if (pos >= _count) // faster than %
{
pos -= _count;
}
return _values[pos];
}
float RunningMedian::getSortedElement(const uint8_t n)
{
if ((_count == 0) || (n >= _count)) return NAN;
if (_sorted == false) sort();
return _values[_sortIdx[n]];
}
// n can be max <= half the (filled) size
float RunningMedian::predict(const uint8_t n)
{
uint8_t mid = _count / 2;
if ((_count == 0) || (n >= mid)) return NAN;
float med = getMedian(); // takes care of sorting !
if (_count & 0x01) // odd # elements
{
return max(med - _values[_sortIdx[mid - n]], _values[_sortIdx[mid + n]] - med);
}
// even # elements
float f1 = (_values[_sortIdx[mid - n]] + _values[_sortIdx[mid - n - 1]]) / 2;
float f2 = (_values[_sortIdx[mid + n]] + _values[_sortIdx[mid + n - 1]]) / 2;
return max(med - f1, f2 - med) / 2;
}
void RunningMedian::sort()
{
// insertSort
for (uint16_t i = 1; i < _count; i++)
{
uint16_t z = i;
uint16_t temp = _sortIdx[z];
while ((z > 0) && (_values[temp] < _values[_sortIdx[z - 1]]))
{
_sortIdx[z] = _sortIdx[z - 1];
z--;
}
_sortIdx[z] = temp;
}
_sorted = true;
}
// -- END OF FILE --

View File

@@ -0,0 +1,94 @@
#pragma once
//
// FILE: RunningMedian.h
// AUTHOR: Rob Tillaart
// PURPOSE: RunningMedian library for Arduino
// VERSION: 0.3.3
// URL: https://github.com/RobTillaart/RunningMedian
// URL: http://arduino.cc/playground/Main/RunningMedian
// HISTORY: See RunningMedian.cpp
//
#include "Arduino.h"
#define RUNNING_MEDIAN_VERSION (F("0.3.3"))
// fall back to fixed storage for dynamic version => remove true
#define RUNNING_MEDIAN_USE_MALLOC true
// MEDIAN_MIN_SIZE should at least be 3 to be practical,
#define MEDIAN_MIN_SIZE 3
#ifdef RUNNING_MEDIAN_USE_MALLOC
// max 250 to not overflow uint8_t internal vars
#define MEDIAN_MAX_SIZE 255
#else
// using fixed memory will be limited to 19 elements.
#define MEDIAN_MAX_SIZE 19
#endif
class RunningMedian
{
public:
// # elements in the internal buffer
// odd sizes results in a 'real' middle element and will be a bit faster.
// even sizes takes the average of the two middle elements as median
explicit RunningMedian(const uint8_t size);
~RunningMedian();
// resets internal buffer and var
void clear();
// adds a new value to internal buffer, optionally replacing the oldest element.
void add(const float value);
// returns the median == middle element
float getMedian();
// returns the Quantile
float getQuantile(const float q);
// returns average of the values in the internal buffer
float getAverage();
// returns average of the middle nMedian values, removes noise from outliers
float getAverage(uint8_t nMedian);
float getHighest() { return getSortedElement(_count - 1); };
float getLowest() { return getSortedElement(0); };
// get n'th element from the values in time order
float getElement(const uint8_t n);
// get n'th element from the values in size order
float getSortedElement(const uint8_t n);
// predict the max change of median after n additions
float predict(const uint8_t n);
uint8_t getSize() { return _size; };
// returns current used elements, getCount() <= getSize()
uint8_t getCount() { return _count; };
bool isFull() { return (_count == _size); }
protected:
boolean _sorted; // _sortIdx{} is up to date
uint8_t _size; // max number of values
uint8_t _count; // current number of values
uint8_t _index; // next index to add.
// _values holds the elements themself
// _p holds the index for sorted
#ifdef RUNNING_MEDIAN_USE_MALLOC
float * _values;
uint8_t * _sortIdx;
#else
float _values[MEDIAN_MAX_SIZE];
uint8_t _p[MEDIAN_MAX_SIZE];
#endif
void sort();
};
// END OF FILE

View File

@@ -0,0 +1,50 @@
//
// FILE: RunningMedian.ino
// AUTHOR: Rob Tillaart ( kudos to Sembazuru)
// VERSION: 0.1.2
// PURPOSE: demo basic usage
// DATE: 2013-10-17
// URL: https://github.com/RobTillaart/RunningMedian
//
#include <RunningMedian.h>
RunningMedian samples = RunningMedian(5);
void setup()
{
Serial.begin(115200);
Serial.print("Running Median Version: ");
Serial.println(RUNNING_MEDIAN_VERSION);
}
void loop()
{
test1();
}
void test1()
{
int x = analogRead(A0);
samples.add(x);
long l = samples.getLowest();
long m = samples.getMedian();
long a = samples.getAverage();
long h = samples.getHighest();
Serial.print(millis());
Serial.print("\t");
Serial.print(x);
Serial.print("\t");
Serial.print(l);
Serial.print("\t");
Serial.print(a);
Serial.print("\t");
Serial.print(m);
Serial.print("\t");
Serial.println(h);
delay(100);
}
// -- END OF FILE --

View File

@@ -0,0 +1,78 @@
//
// FILE: RunningMedian2.ino
// AUTHOR: Rob Tillaart ( kudos to Sembazuru)
// VERSION: 0.1.2
// PURPOSE: demo most functions
// DATE: 2013-10-17
// URL: https://github.com/RobTillaart/RunningMedian
//
#include "RunningMedian.h"
RunningMedian samples = RunningMedian(100);
long count = 0;
void setup()
{
Serial.begin(115200);
Serial.print(F("Running Median Version: "));
Serial.println(RUNNING_MEDIAN_VERSION);
}
void loop()
{
test1();
}
void test1()
{
if (count % 20 == 0)
{
Serial.println(F("\nmsec \tAnR \tSize \tCnt \tLow \tAvg \tAvg(7) \tAvg(3) \tMed \tHigh \tPre(1) \tPre(2)"));
}
count++;
long x = analogRead(A0);
samples.add(x);
float l = samples.getLowest();
float m = samples.getMedian();
float a = samples.getAverage();
float a7 = samples.getAverage(7);
float a3 = samples.getAverage(3);
float h = samples.getHighest();
int s = samples.getSize();
int c = samples.getCount();
float p1 = samples.predict(1);
float p2 = samples.predict(2);
Serial.print(millis());
Serial.print('\t');
Serial.print(x);
Serial.print('\t');
Serial.print(s);
Serial.print('\t');
Serial.print(c);
Serial.print('\t');
Serial.print(l);
Serial.print('\t');
Serial.print(a, 2);
Serial.print('\t');
Serial.print(a7, 2);
Serial.print('\t');
Serial.print(a3, 2);
Serial.print('\t');
Serial.print(m);
Serial.print('\t');
Serial.print(h);
Serial.print('\t');
Serial.print(p1, 2);
Serial.print('\t');
Serial.println(p2, 2);
delay(100);
}

View File

@@ -0,0 +1,40 @@
//
// FILE: RunningMedianQuantileTest.ino
// AUTHOR: f-s ( derived from Rob Tillaart )
// VERSION: 0.1.2
// PURPOSE: demo basic quantile usage
// DATE: 2020-09-02
// URL: https://github.com/RobTillaart/RunningMedian
//
#include <RunningMedian.h>
RunningMedian samples = RunningMedian(5);
void setup()
{
Serial.begin(115200);
Serial.print("Running Median Version: ");
Serial.println(RUNNING_MEDIAN_VERSION);
}
void loop()
{
test1();
}
void test1()
{
int x = analogRead(A0);
samples.add(x);
// calculate the 5% quantile => 0.05
long q = samples.getQuantile(0.05);
Serial.print(millis());
Serial.print("\t");
Serial.println(q);
delay(100);
}
// -- END OF FILE --

View File

@@ -0,0 +1,163 @@
//
// FILE: runningMedianTest1.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.1
// PURPOSE: test functionality
// DATE: 2013-10-28
// URL: https://github.com/RobTillaart/RunningMedian
//
#include <RunningMedian.h>
const int sourceData[] =
{ // 50 consecutive samples from Sharp distance sensor model GP2Y0A710K0F while stationary.
300, 299, 296, 343, 307, 304, 303, 305, 300, 340,
308, 305, 300, 304, 311, 304, 300, 300, 304, 304,
284, 319, 306, 304, 300, 302, 305, 310, 306, 304,
308, 300, 299, 304, 300, 305, 307, 303, 326, 311,
306, 304, 305, 300, 300, 307, 302, 305, 296, 300
};
const int sourceSize = (sizeof(sourceData)/sizeof(sourceData[0]));
RunningMedian samples = RunningMedian(sourceSize);
void setup()
{
Serial.begin(115200);
while (!Serial); // Wait for serial port to connect. Needed for Leonardo only.
delay(1000); // Simply to allow time for the ERW versions of the IDE time to automagically open the Serial Monitor. 1 second chosen arbitrarily.
Serial.print(F("Running Median Version: "));
Serial.println(RUNNING_MEDIAN_VERSION);
#ifdef RUNNING_MEDIAN_USE_MALLOC
Serial.println(F("Dynamic version using malloc() enabled"));
#else
Serial.print(F("Static version, will always allocate an array of "));
Serial.print(MEDIAN_MAX_SIZE, DEC);
Serial.println(F(" floats."));
#endif
test1();
Serial.println("\ndone..\n");
}
void loop()
{
}
void test1()
{
uint32_t start = 0;
uint32_t stop = 0;
float result = 0;
Serial.print(F("Requested median array size = "));
Serial.println(sourceSize);
Serial.print(F(" Actual allocated size = "));
Serial.println(samples.getSize());
Serial.println();
// 50 iterations !!
for (uint8_t i = 0; i <= (sourceSize - 1); i++)
{
Serial.print(F("Loop number : "));
Serial.println(i + 1);
start = micros();
samples.add(sourceData[i]);
stop = micros();
Serial.print(F("Time to add the next element to the array = "));
Serial.println(stop - start);
Serial.println(F("Cumulative source data added:"));
Serial.print(F(" "));
for (uint8_t j = 0; j <= i; j++)
{
Serial.print(sourceData[j]);
Serial.print(F(" "));
}
Serial.println();
Serial.println(F("Unsorted accumulated array:"));
Serial.print(F(" "));
for (uint8_t j = 0; j < samples.getCount(); j++)
{
Serial.print(samples.getElement(j));
Serial.print(F(" "));
}
Serial.println();
start = micros();
result = samples.getSortedElement(0);
stop = micros();
Serial.print(F("Time to sort array and return element number zero = "));
Serial.println(stop - start);
Serial.println(F("Sorted accumulated array:"));
Serial.print(F(" "));
for (uint8_t j = 0; j < samples.getCount(); j++)
{
Serial.print(samples.getSortedElement(j));
Serial.print(F(" "));
}
Serial.println();
start = micros();
result = samples.getMedian();
stop = micros();
Serial.print(F("getMedian() result = "));
Serial.println(result);
Serial.print(F("Time to execute getMedian() = "));
Serial.println(stop - start);
start = micros();
result = samples.getAverage();
stop = micros();
Serial.print(F("getAverage() result = "));
Serial.println(result);
Serial.print(F("Time to execute getAverage() = "));
Serial.println(stop - start);
Serial.println(F("getAverage(x) results where:"));
for (uint8_t j = 1; j <= samples.getCount(); j++)
{
start = micros();
result = samples.getAverage(j);
stop = micros();
Serial.print(F(" x = "));
Serial.print(j);
Serial.print(F(" => "));
Serial.print(result);
Serial.print(F(" Time to execute = "));
Serial.println(stop - start);
}
Serial.println(F("predict(x) results where:"));
for (uint8_t j = 1; j <= (samples.getCount() / 2); j++)
{
start = micros();
result = samples.predict(j);
stop = micros();
Serial.print(F(" x = "));
Serial.print(j);
Serial.print(F(" => "));
Serial.print(result);
Serial.print(F(" Time to execute = "));
Serial.println(stop - start);
}
Serial.println();
Serial.println();
}
}
// -- END OF FILE --

View File

@@ -0,0 +1,66 @@
//
// FILE: RunningMedian_large.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.3
// PURPOSE: demo most functions
// DATE: 2013-10-17
// URL: https://github.com/RobTillaart/RunningMedian
//
#include "RunningMedian.h"
RunningMedian samples = RunningMedian(255);
long count = 0;
uint32_t start, dur1, dur2, dur3;
void setup()
{
Serial.begin(115200);
Serial.print(F("Running Median Version: "));
Serial.println(RUNNING_MEDIAN_VERSION);
Serial.println(samples.getSize());
}
void loop()
{
if (count < 255)
{
start = micros();
samples.add(256 - count);
dur1 = micros() - start;
start = micros();
count = samples.getCount();
dur2 = micros() - start;
start = micros();
float value = samples.getMedian();
dur3 = micros() - start;
Serial.print(count);
Serial.print('\t');
Serial.print(dur1);
Serial.print('\t');
Serial.print(dur2);
Serial.print('\t');
Serial.print(dur3);
Serial.print('\t');
Serial.println();
}
if (count == 255)
{
for (int i = 0; i < 255; i++)
{
Serial.println(samples.getSortedElement(i));
}
}
count++;
}
// -- END OF FILE --

View File

@@ -0,0 +1,23 @@
# Syntax Coloring Map for RunningMedian
# Datatypes (KEYWORD1)
RunningMedian KEYWORD1
# Methods and Functions (KEYWORD2)
add KEYWORD2
clear KEYWORD2
getMedian KEYWORD2
getQuantile KEYWORD2
getAverage KEYWORD2
getHighest KEYWORD2
getLowest KEYWORD2
getSize KEYWORD2
getCount KEYWORD2
getElement KEYWORD2
getSortedElement KEYWORD2
predict KEYWORD2
getStatus KEYWORD2
# Constants (LITERAL1)
OK LITERAL1
NOK LITERAL1

View File

@@ -0,0 +1,21 @@
{
"name": "RunningMedian",
"keywords": "running, moving, median, average, outliers",
"description": "The library stores the last N individual values in a buffer to select the median. It filters outliers.",
"authors":
[
{
"name": "Rob Tillaart",
"email": "Rob.Tillaart@gmail.com",
"maintainer": true
}
],
"repository":
{
"type": "git",
"url": "https://github.com/RobTillaart/RunningMedian.git"
},
"version":"0.3.3",
"frameworks": "arduino",
"platforms": "*"
}

View File

@@ -0,0 +1,11 @@
name=RunningMedian
version=0.3.3
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=The library stores the last N individual values in a buffer to select the median.
paragraph=This will filter outliers in a chain of samples very well.
category=Data Processing
url=https://github.com/RobTillaart/RunningMedian
architectures=*
includes=RunningMedian.h
depends=

View File

@@ -0,0 +1,123 @@
//
// FILE: unit_test_001.cpp
// AUTHOR: Rob Tillaart
// DATE: 2021-01-03
// PURPOSE: unit tests for the RunningMedian
// https://github.com/RobTillaart/RunningMedian
// https://github.com/Arduino-CI/arduino_ci/blob/master/REFERENCE.md
//
// supported assertions
// ----------------------------
// assertEqual(expected, actual); // a == b
// assertNotEqual(unwanted, actual); // a != b
// assertComparativeEquivalent(expected, actual); // abs(a - b) == 0 or (!(a > b) && !(a < b))
// assertComparativeNotEquivalent(unwanted, actual); // abs(a - b) > 0 or ((a > b) || (a < b))
// assertLess(upperBound, actual); // a < b
// assertMore(lowerBound, actual); // a > b
// assertLessOrEqual(upperBound, actual); // a <= b
// assertMoreOrEqual(lowerBound, actual); // a >= b
// assertTrue(actual);
// assertFalse(actual);
// assertNull(actual);
// // special cases for floats
// assertEqualFloat(expected, actual, epsilon); // fabs(a - b) <= epsilon
// assertNotEqualFloat(unwanted, actual, epsilon); // fabs(a - b) >= epsilon
// assertInfinity(actual); // isinf(a)
// assertNotInfinity(actual); // !isinf(a)
// assertNAN(arg); // isnan(a)
// assertNotNAN(arg); // !isnan(a)
#include <ArduinoUnitTests.h>
#include "Arduino.h"
#include "RunningMedian.h"
unittest_setup()
{
}
unittest_teardown()
{
}
unittest(test_constructor)
{
fprintf(stderr, "VERSION: %s\n", RUNNING_MEDIAN_VERSION);
RunningMedian samples = RunningMedian(5);
assertEqual(5, samples.getSize());
assertEqual(0, samples.getCount());
// TODO default values?
}
unittest(test_basic_add)
{
fprintf(stderr, "VERSION: %s\n", RUNNING_MEDIAN_VERSION);
RunningMedian samples = RunningMedian(5);
int cnt = 0;
for (int i = 0, cnt = 0; i < 50; i+=10)
{
samples.add(i);
cnt++;
assertEqual(cnt, samples.getCount());
}
assertEqual(5, samples.getSize());
assertEqualFloat(20, samples.getMedian(), 0.0001);
assertEqualFloat(20, samples.getAverage(), 0.0001);
assertEqualFloat(00, samples.getLowest(), 0.0001);
assertEqualFloat(40, samples.getHighest(), 0.0001);
samples.add(100); // 6th element
assertEqual(5, samples.getSize());
assertEqual(5, samples.getCount());
assertEqualFloat(30, samples.getMedian(), 0.0001);
assertEqualFloat(40, samples.getAverage(), 0.0001);
assertEqualFloat(10, samples.getLowest(), 0.0001);
assertEqualFloat(100, samples.getHighest(), 0.0001);
samples.clear();
assertEqual(5, samples.getSize());
assertEqual(0, samples.getCount());
}
unittest(test_big)
{
fprintf(stderr, "VERSION: %s\n", RUNNING_MEDIAN_VERSION);
RunningMedian samples = RunningMedian(100);
assertEqual(100, samples.getSize());
assertEqual(0, samples.getCount());
for (int i = 0; i < 110; i++)
{
samples.add(i);
}
assertEqual(100, samples.getSize());
assertEqual(100, samples.getCount());
assertEqualFloat(59.5, samples.getMedian(), 0.0001);
assertEqualFloat(59.5, samples.getAverage(), 0.0001);
assertEqualFloat(10, samples.getLowest(), 0.0001);
assertEqualFloat(109, samples.getHighest(), 0.0001);
samples.clear();
assertEqual(100, samples.getSize());
assertEqual(0, samples.getCount());
}
unittest_main()
// --------