0.3.0 MINMAX

This commit is contained in:
Rob Tillaart 2024-09-11 11:37:18 +02:00
parent a29a63ec8b
commit 13f80f4903
9 changed files with 151 additions and 22 deletions

View File

@ -6,10 +6,16 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.3.0] - 2024-09-11
- fix #11, Add linear dampening option.
- add example - minmax_dampening.ino
- update readme.md
----
## [0.2.2] - 2023-12-12
- add **void setResetDefaults(float minimum, float maximum)**
- update readme
- update readme.md
## [0.2.1] - 2023-11-14
- update readme.md

View File

@ -1,7 +1,7 @@
//
// FILE: MINMAX.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.2.2
// VERSION: 0.3.0
// DATE: 2021-10-14
// PURPOSE: MINMAX library - simple peak finder
// URL: https://github.com/RobTillaart/MINMAX
@ -16,6 +16,7 @@ MINMAX::MINMAX()
_minimumDefault = 0;
_maximumDefault = 0;
_callback = NULL;
_dampening = 0;
reset();
}
@ -29,6 +30,19 @@ uint8_t MINMAX::add(const float value)
reset();
rv |= MINMAX_RESET_DONE;
}
// dampening - experimental
if (_dampening > 0)
{
if (_minimum + _dampening < _maximum)
{
_minimum += _dampening;
}
if (_maximum - _dampening > _minimum)
{
_maximum -= _dampening;
}
}
// new run and range not adjusted by setResetDefaults()
if ((_count == 0) && (_minimum == 0) && (_maximum == 0))
{
@ -36,12 +50,14 @@ uint8_t MINMAX::add(const float value)
_lastMin = _lastMax = millis();
rv |= MINMAX_MIN_CHANGED | MINMAX_MAX_CHANGED;
}
// adjust minimum
if (value < _minimum)
{
_minimum = value;
_lastMin = millis();
rv |= MINMAX_MIN_CHANGED;
}
// adjust maximum
if (value > _maximum)
{
_maximum = value;
@ -129,6 +145,17 @@ uint32_t MINMAX::lastMax()
return _lastMax;
}
void MINMAX::setDampening(const float value)
{
_dampening = value;
}
// -- END OF FILE --
float MINMAX::getDampening()
{
return _dampening;
}
// -- END OF FILE --

View File

@ -2,7 +2,7 @@
//
// FILE: MINMAX.h
// AUTHOR: Rob Tillaart
// VERSION: 0.2.2
// VERSION: 0.3.0
// DATE: 2021-10-14
// PURPOSE: MINMAX library - simple peak finder
// URL: https://github.com/RobTillaart/MINMAX
@ -10,7 +10,7 @@
#include "Arduino.h"
#define MINMAX_LIB_VERSION (F("0.2.2"))
#define MINMAX_LIB_VERSION (F("0.3.0"))
#define MINMAX_NO_CHANGE 0X00
#define MINMAX_MIN_CHANGED 0X01
@ -51,6 +51,10 @@ public:
uint32_t lastMin();
uint32_t lastMax();
// in/decrease MIN and MAX - linear - with this value if possible.
void setDampening(const float value);
float getDampening();
private:
float _lastValue;
@ -59,6 +63,7 @@ private:
float _maximum;
float _minimumDefault;
float _maximumDefault;
float _dampening;
uint32_t _count;
uint32_t _resetCount;

View File

@ -38,8 +38,7 @@ Finally the library keeps track when the last peaks occurred.
```cpp
#include "MINMAX.h"
```
#### Core
### Core
- **MINMAX()** Constructor,
- **uint8_t add(float value)** add next value. Returns status (bit flags), see table below.
@ -52,7 +51,7 @@ If no call to **add()** is made yet it will return 0.
- **lastValue()** returns last value added.
#### AutoReset
### AutoReset
- **uint32_t count()** returns number of **add()** calls since last (auto)reset.
- **void setAutoResetCount(uint32_t count)** sets an auto-reset moment after count calls to **add()**.
@ -61,13 +60,13 @@ When count is set to 0, there will be no autoReset.
- **uint32_t getAutoResetCount()** returns set value.
#### Timestamps
### Timestamps
- **lastMin()** timestamp in millis() when minimum was last updated.
- **lastMax()** timestamp in millis() when maximum was last updated.
#### Return flags add()
### Return flags add()
Return flags of **add()** - is a bit mask.
@ -82,14 +81,14 @@ NOTE: After a reset() the next add() will return both MINMAX_MIN_CHANGED and MIN
NOTE: After an autoReset in add() it will return 0x83.
#### CallBack
### CallBack
- **void addCallBack( void (\* func)(void) )** add a function to be called
when the minimum or the maximum has changed.
See examples.
#### setResetDefaults()
### setResetDefaults()
- **void setResetDefaults(minimum, maximum)** sets the default values for minimum and maximum defining an initial range / window when **reset()** is called.
This will reduce an abundance of new min/max values in the first part of a stream, possibly causing unneeded call back calls.
@ -113,16 +112,35 @@ mm.reset(); // activate them
```
### Dampening
Since 0.3.0 the MINMAX library has experimental support for a linear dampening
of the minimum and maximum.
In every call to **add()** the values of MIN and MAX are brought closer together.
Therefore one does not need to reset them as these two are constantly adjusted
towards each other.
The amount of dampening is a positive float and its value depends on the typical
values one adds with **mm.add(value)**.
If the dampening value is too large, the min and max will follow the "signal"
quite fast.
Note: MIN is dampened first, and MAX is later. This might cause that MAX cannot
be dampened anymore for the full amount. This is an arbitrary choice.
- **void setDampening(const float value)** set dampening value. Default 0.
- **float getDampening()** get the current dampening value.
Note: The behavior might change a bit in the future e.g. both dampen for 50% of
the dampening value seems to be a valid behavior. Also make the dampening for MAX and MIN different could be an option.
## Obsolete
- **void autoReset(uint32_t count)** obsolete since 0.2.0
Replaced by **void setAutoResetCount(uint32_t count)**
## Operation
The examples show the basic working of the functions.
## Future
@ -149,6 +167,7 @@ a value is out of a predefined range.
- **void setResetDefaults(minimum, maximum, bool adjust = true)** add an adjust flag
that allows / reject adaption of min / max. (extended class).
- define MINMAX_MIN_CHANGED => MINMAX_MIN_EXCEEDED (new or reuse?)
- improve dampening strategy(?)
#### Wont (unless)

View File

@ -0,0 +1,55 @@
//
// FILE: minmax_dampening.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo
// URL: https://github.com/RobTillaart/minmax
//
// use plotter to get a view
#include "MINMAX.h"
MINMAX mm;
uint32_t start, stop;
float n = 0;
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("MINMAX_LIB_VERSION: ");
Serial.println(MINMAX_LIB_VERSION);
Serial.println();
mm.reset();
mm.setAutoResetCount(10000);
}
void loop()
{
// mm.setDampening(1);
// int r = random(n--) - 500;
// if (n <= 0) n = 1000;
mm.setDampening(0.5);
int r = sin(n) * 100 + 100;
n += 0.01;
mm.add(r);
Serial.print(r);
Serial.print("\t");
Serial.print(mm.minimum());
Serial.print("\t");
Serial.print(mm.maximum());
Serial.print("\n");
delay(10);
}
// -- END OF FILE --

View File

@ -12,14 +12,19 @@ addCallBack KEYWORD2
setAutoResetCount KEYWORD2
getAutoResetCount KEYWORD2
minimum KEYWORD2
maximum KEYWORD2
count KEYWORD2
lastValue KEYWORD2
count KEYWORD2
minimum KEYWORD2
maximum KEYWORD2
lastMin KEYWORD2
lastMax KEYWORD2
setDampening KEYWORD2
getDampening KEYWORD2
# Constants (LITERAL1)
MINMAX_LIB_VERSION LITERAL1

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/MINMAX.git"
},
"version": "0.2.2",
"version": "0.3.0",
"license": "MIT",
"frameworks": "*",
"platforms": "*",

View File

@ -1,5 +1,5 @@
name=MINMAX
version=0.2.2
version=0.3.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=MINMAX library for Arduino.

View File

@ -108,6 +108,18 @@ unittest(test_lastValue)
}
unittest(test_dampening)
{
MINMAX mm;
for (int i = 0; i < 10; i++)
{
mm.setDampening(i);
assertEqualFloat(i, mm.getDampening(), 0.01);
}
}
unittest_main()