Skip to content

Commit 224f0e4

Browse files
committed
+ version 0.1.08
+ added getElement() + added getSortedElement() + added predict() + refactor getHighest(), getLowest()
1 parent 0acddd3 commit 224f0e4

File tree

4 files changed

+60
-23
lines changed

4 files changed

+60
-23
lines changed

libraries/RunningMedian/RunningMedian.cpp

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: RunningMedian.cpp
33
// AUTHOR: Rob dot Tillaart at gmail dot com
4-
// VERSION: 0.1.07
4+
// VERSION: 0.1.08
55
// PURPOSE: RunningMedian library for Arduino
66
//
77
// HISTORY:
@@ -13,6 +13,7 @@
1313
// 0.1.05 - 2013-10-18 fixed bug in sort; removes default constructor; dynamic memory
1414
// 0.1.06 - 2013-10-19 faster sort, dynamic arrays, replaced sorted float array with indirection array
1515
// 0.1.07 - 2013-10-19 add correct median if _cnt is even.
16+
// 0.1.08 - 2013-10-20 add getElement(), add getSottedElement() add predict()
1617
//
1718
// Released to the public domain
1819
//
@@ -71,25 +72,9 @@ float RunningMedian::getMedian()
7172
}
7273

7374
#ifdef RUNNING_MEDIAN_ALL
74-
float RunningMedian::getHighest()
75-
{
76-
if (_cnt > 0)
77-
{
78-
if (_sorted == false) sort(); // assumes other fields are also retrieved otherwise inefficient
79-
return _ar[_p[_cnt-1]];
80-
}
81-
return NAN;
82-
}
75+
float RunningMedian::getHighest() { return getSortedElement(_cnt-1); }
8376

84-
float RunningMedian::getLowest()
85-
{
86-
if (_cnt > 0)
87-
{
88-
if (_sorted == false) sort();
89-
return _ar[_p[0]];
90-
}
91-
return NAN;
92-
}
77+
float RunningMedian::getLowest() { return getSortedElement(0); }
9378

9479
float RunningMedian::getAverage()
9580
{
@@ -119,6 +104,44 @@ float RunningMedian::getAverage(uint8_t nMedians)
119104
return NAN;
120105
}
121106

107+
float RunningMedian::getElement(uint8_t n)
108+
{
109+
if ((_cnt > 0) && (n < _cnt))
110+
{
111+
return _ar[n];
112+
}
113+
return NAN;
114+
}
115+
116+
float RunningMedian::getSortedElement(uint8_t n)
117+
{
118+
if ((_cnt > 0) && (n < _cnt))
119+
{
120+
if (_sorted == false) sort();
121+
return _ar[_p[n]];
122+
}
123+
return NAN;
124+
}
125+
126+
float RunningMedian::predict(uint8_t n)
127+
{
128+
if ((_cnt > 0) && (n < _cnt/2))
129+
{
130+
float med = getMedian(); // takes care of sorting !
131+
if (_cnt & 0x01)
132+
{
133+
return max(med - _ar[_p[_cnt/2-n]], _ar[_p[_cnt/2+n]] - med);
134+
}
135+
else
136+
{
137+
float f1 = (_ar[_p[_cnt/2 - n]] + _ar[_p[_cnt/2 - n - 1]])/2;
138+
float f2 = (_ar[_p[_cnt/2 + n]] + _ar[_p[_cnt/2 + n - 1]])/2;
139+
return max(med - f1, f2 - med)/2;
140+
}
141+
}
142+
return NAN;
143+
}
144+
122145
uint8_t RunningMedian::getSize() { return _size; };
123146

124147
uint8_t RunningMedian::getCount() { return _cnt; };

libraries/RunningMedian/RunningMedian.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// FILE: RunningMedian.h
55
// AUTHOR: Rob dot Tillaart at gmail dot com
66
// PURPOSE: RunningMedian library for Arduino
7-
// VERSION: 0.1.07
7+
// VERSION: 0.1.08
88
// URL: http://arduino.cc/playground/Main/RunningMedian
99
// HISTORY: See RunningMedian.cpp
1010
//
@@ -53,6 +53,10 @@ class RunningMedian
5353
float getHighest(); // returns highest element
5454
float getLowest(); // return lowest element
5555

56+
float getElement(uint8_t n); // get n'th element from the values in time order
57+
float getSortedElement(uint8_t n); // get n'th element from the values in size order
58+
float predict(uint8_t n); // predict the max change of median after n additions
59+
5660
uint8_t getSize(); // returns size of internal buffer
5761
uint8_t getCount(); // returns current used elements, getCount() <= getSize()
5862
#endif

libraries/RunningMedian/examples/RunningMedian2/RunningMedian2.ino

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
#include "RunningMedian.h"
1313

14-
RunningMedian samples = RunningMedian(7);
14+
RunningMedian samples = RunningMedian(100);
1515

1616
long count = 0;
1717

@@ -29,7 +29,7 @@ void loop()
2929

3030
void test1()
3131
{
32-
if (count % 20 == 0) Serial.println(F("\nmsec \tAnR \tSize \tCnt \tLow \tAvg \tAvg(7) \tAvg(3) \tMed \tHigh"));
32+
if (count % 20 == 0) Serial.println(F("\nmsec \tAnR \tSize \tCnt \tLow \tAvg \tAvg(7) \tAvg(3) \tMed \tHigh \tPre(1) \tPre(2)"));
3333
count++;
3434

3535
long x = analogRead(A0);
@@ -44,6 +44,8 @@ void test1()
4444
float h = samples.getHighest();
4545
int s = samples.getSize();
4646
int c = samples.getCount();
47+
float p1 = samples.predict(1);
48+
float p2 = samples.predict(2);
4749

4850
Serial.print(millis());
4951
Serial.print('\t');
@@ -63,7 +65,12 @@ void test1()
6365
Serial.print('\t');
6466
Serial.print(m);
6567
Serial.print('\t');
66-
Serial.println(h);
68+
Serial.print(h);
69+
Serial.print('\t');
70+
Serial.print(p1, 2);
71+
Serial.print('\t');
72+
Serial.println(p2, 2);
73+
6774
delay(100);
6875
}
6976

libraries/RunningMedian/keywords.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ getHighest KEYWORD2
2121
getLowest KEYWORD2
2222
getSize KEYWORD2
2323
getCount KEYWORD2
24+
getElement KEYWORD2
25+
getSortedElement KEYWORD2
26+
predict KEYWORD2
2427
getStatus KEYWORD2
2528

2629
#######################################

0 commit comments

Comments
 (0)