Skip to content

Commit

Permalink
Changes as requested (#16)
Browse files Browse the repository at this point in the history
* Cconfigurable via defines, fixed static version
- It is now possible to change the min/max sizes via defines
- It is now possible to switch from dynamic to static version via define
- Fixed static version being broken

* Added requested documentation & version updates
  • Loading branch information
dralois authored Jun 5, 2022
1 parent 13593d3 commit 4126825
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ the running median in the next steps in advance.
to remove the oldest and keep them sorted to be able to select the median.


#### Note MEDIAN_MAX_SIZE
#### 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
Expand All @@ -43,6 +43,19 @@ 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.


### Note: Configurable Options

There are several options that can be configured via defines at compile time, those being:
- RUNNING_MEDIAN_USE_MALLOC: bool
- true (default): Dynamic memory allocation is used for the buffer.
- false: Static buffers of size MEDIAN_MAX_SIZE are used.
- MEDIAN_MIN_SIZE: uint
- Dynamic / Static: The buffer stores at least this many items.
- MEDIAN_MAX_SIZE: uint
- Dynamic: Not used.
- Static: The buffer stores at most this many items.


## Interface


Expand Down
11 changes: 7 additions & 4 deletions RunningMedian.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: RunningMedian.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.3.4
// VERSION: 0.3.5
// PURPOSE: RunningMedian library for Arduino
//
// HISTORY:
Expand Down Expand Up @@ -30,6 +30,7 @@
// --> better performance for large arrays.
// 0.3.3 2021-01-22 better insertionSort (+ clean up test code)
// 0.3.4 2021-12-28 update library.json, readme, license, minor edits
// 0.3.5 2022-06-05 configuration options, fixed static version not working


#include "RunningMedian.h"
Expand All @@ -39,9 +40,11 @@ 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;
#if !RUNNING_MEDIAN_USE_MALLOC
if (_size > MEDIAN_MAX_SIZE) _size = MEDIAN_MAX_SIZE;
#endif

#ifdef RUNNING_MEDIAN_USE_MALLOC
#if RUNNING_MEDIAN_USE_MALLOC
_values = (float *) malloc(_size * sizeof(float));
_sortIdx = (uint8_t *) malloc(_size * sizeof(uint8_t));
#endif
Expand All @@ -51,7 +54,7 @@ RunningMedian::RunningMedian(const uint8_t size)

RunningMedian::~RunningMedian()
{
#ifdef RUNNING_MEDIAN_USE_MALLOC
#if RUNNING_MEDIAN_USE_MALLOC
free(_values);
free(_sortIdx);
#endif
Expand Down
14 changes: 10 additions & 4 deletions RunningMedian.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// FILE: RunningMedian.h
// AUTHOR: Rob Tillaart
// PURPOSE: RunningMedian library for Arduino
// VERSION: 0.3.4
// VERSION: 0.3.5
// URL: https://github.com/RobTillaart/RunningMedian
// URL: http://arduino.cc/playground/Main/RunningMedian
// HISTORY: See RunningMedian.cpp
Expand All @@ -12,24 +12,30 @@

#include "Arduino.h"

#define RUNNING_MEDIAN_VERSION (F("0.3.4"))
#define RUNNING_MEDIAN_VERSION (F("0.3.5"))


// fall back to fixed storage for dynamic version => remove true
#ifndef RUNNING_MEDIAN_USE_MALLOC
#define RUNNING_MEDIAN_USE_MALLOC true
#endif


// MEDIAN_MIN_SIZE should at least be 3 to be practical,
#ifndef MEDIAN_MIN_SIZE
#define MEDIAN_MIN_SIZE 3
#endif


#ifndef MEDIAN_MAX_SIZE
#ifdef RUNNING_MEDIAN_USE_MALLOC
// max 250 to not overflow uint8_t internal variables
#define MEDIAN_MAX_SIZE 255
#else
// using fixed memory will be limited to 19 elements.
#define MEDIAN_MAX_SIZE 19
#endif
#endif


class RunningMedian
Expand Down Expand Up @@ -81,12 +87,12 @@ class RunningMedian

// _values holds the elements themself
// _p holds the index for sorted
#ifdef RUNNING_MEDIAN_USE_MALLOC
#if RUNNING_MEDIAN_USE_MALLOC
float * _values;
uint8_t * _sortIdx;
#else
float _values[MEDIAN_MAX_SIZE];
uint8_t _p[MEDIAN_MAX_SIZE];
uint8_t _sortIdx[MEDIAN_MAX_SIZE];
#endif
void sort();
};
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/RunningMedian.git"
},
"version": "0.3.4",
"version": "0.3.5",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",
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=RunningMedian
version=0.3.4
version=0.3.5
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.
Expand Down

0 comments on commit 4126825

Please sign in to comment.