Library for finding peaks (minimum and maximum) in a signal.
The MINMAX library is a simple peak finder in a stream of floats.
The add() function indicates after adding a number if the minimum and/or the maximum value has been changed by means of a bit flag which is returned. If a peak is found, it will be used as the new minimum() or maximum() until a call to reset().
The library can reset() the minimum and maximum to 0 to start again. The first add() after the reset() will set the minimum and maximum to the value added.
The library has the option to auto-reset after a predefined number of add() calls.
Finally the library keeps track when the last peaks occurred.
#include "MINMAX.h"
Return flags of add() - is a bit mask.
flag | value | description |
---|---|---|
MINMAX_NO_CHANGE | 0x00 | no change |
MINMAX_MIN_CHANGED | 0x01 | minimum changed |
MINMAX_MAX_CHANGED | 0x02 | maximum changed |
MINMAX_RESET_DONE | 0x80 | reset done |
NOTE: After a reset() the next add() will return both MINMAX_MIN_CHANGED and MINMAX_MAX_CHANGED (0x03)
NOTE: After an autoReset in add() it will return 0x83.
The constructor sets both to zero (0) by default. The user can now override these values. There are no default values for the parameters in the function. The user is responsible and even allowed to set minimum to be larger than maximum. The new values become active after the call to reset().
The function does not change or reset the lastMin() and lastMax() timestamps. Only after reset() these are set to 0 again.
Note that with an initial range set, the lastMin() and lastMax() timestamps may be zero for quite a while.
Typical code snippet
mm.setResetDefaults(-10, 20); // arbitrary values
mm.reset(); // activate them
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.
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.
If you appreciate my libraries, you can support the development and maintenance. Improve the quality of the libraries by providing issues and Pull Requests, or donate through PayPal or GitHub sponsors.
Thank you,