Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 224:90172915d0fb, committed 2018-10-17
- Comitter:
- bwang
- Date:
- Wed Oct 17 02:59:26 2018 +0000
- Parent:
- 223:b986e7cee521
- Child:
- 225:81b72a4bf18b
- Commit message:
- 10/16/2018 22:59 - renamed CircularBuffer to fCircularBuffer to avoid conflict with new mbed libs
Changed in this revision
--- a/CHANGELOG.txt Tue Oct 02 07:12:01 2018 +0000 +++ b/CHANGELOG.txt Wed Oct 17 02:59:26 2018 +0000 @@ -55,4 +55,5 @@ 06/26/2018 15:57 - added induction machine CurrentModel, which is probably wrong 10/02/2018 02:15 - PwmIn::get_throttle() returns 0, not -1, if throttle is disabled. 10/02/2018 02:21 - processCmd() now uses else-if statements, prints message if command is invalid -10/02/2018 03:10 - added overly-complicated LedBlinker class for blinking status codes over an LED, added blinker object to IOStruct, STATUS_LED to hardware.h \ No newline at end of file +10/02/2018 03:10 - added overly-complicated LedBlinker class for blinking status codes over an LED, added blinker object to IOStruct, STATUS_LED to hardware.h +10/16/2018 22:59 - renamed CircularBuffer to fCircularBuffer to avoid conflict with new mbed libs \ No newline at end of file
--- a/Filter/CircularBuffer.cpp Tue Oct 02 07:12:01 2018 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,152 +0,0 @@
-#include "mbed.h"
-#include "math.h"
-#include "Filter.h"
-
-CircularBuffer::CircularBuffer(int length, bool use_median) {
- _length = length;
- _use_median = use_median;
-
- oldest_index = 0;
- newest_index = -1;
- num = 0;
- sum = 0.0f;
-
- buf = (float*)malloc(_length * sizeof(float));
- sorted = (float*)malloc(_length * sizeof(float));
- for (int i = 0; i < _length; i++) {
- buf[i] = 0.0f;
- sorted[i] = 0.0f;
- }
-}
-
-float &CircularBuffer::at(int index) {
- int actual = oldest_index + index;
- if (actual >= _length) actual -= _length;
- return buf[actual];
-}
-
-void CircularBuffer::add(float x) {
- if (num < _length) {
- newest_index++;
- buf[newest_index] = x;
- sum += x;
- num++;
-
- if (!_use_median || _length < 4) return;
-
- /*insert x into sorted array*/
- int i = num - 1;
- while (i > 0 && sorted[i - 1] > x) {
- sorted[i] = sorted[i - 1];
- i--;
- }
- sorted[i] = x;
- } else {
- /*update circular buffer*/
- float oldest = buf[oldest_index];
-
- sum -= buf[oldest_index];
- oldest_index++;
- if (oldest_index >= _length) oldest_index -= _length;
-
- newest_index++;
- if (newest_index >= _length) newest_index -= _length;
- buf[newest_index] = x;
-
- sum += x;
-
- if (!_use_median || _length < 4) return;
-
- /*find sorted index of oldest element*/
- int removed;
- for (removed = 0; removed < _length; removed++) {
- if (sorted[removed] == oldest) break;
- }
-
- /*insert x*/
- int i;
- if (removed == _length - 1) {
- i = _length - 1;
- while (i > 0 && sorted[i - 1] > x) {
- sorted[i] = sorted[i - 1];
- i--;
- }
- sorted[i] = x;
- } else if (removed == 0) {
- i = 0;
- while (i < _length - 1 && sorted[i + 1] < x) {
- sorted[i] = sorted[i + 1];
- i++;
- }
- sorted[i] = x;
- } else if (sorted[removed - 1] <= x && sorted[removed + 1] >= x) {
- sorted[removed] = x;
- } else if (sorted[removed - 1] > x) {
- i = removed;
- while (i > 0 && sorted[i - 1] > x) {
- sorted[i] = sorted[i - 1];
- i--;
- }
- sorted[i] = x;
- } else {
- i = removed;
- while (i < _length - 1 && sorted[i + 1] < x) {
- sorted[i] = sorted[i + 1];
- i++;
- }
- sorted[i] = x;
- }
- }
-}
-
-float CircularBuffer::mean() {
- return sum / num;
-}
-
-float median3(float *buf) {
- if (buf[0] > buf[1]) {
- if (buf[1] > buf[2]) {
- return buf[1];
- } else if (buf[0] > buf[2]) {
- return buf[2];
- } else {
- return buf[0];
- }
- } else {
- if (buf[0] > buf[2]) {
- return buf[0];
- } else if (buf[1] > buf[2]) {
- return buf[2];
- } else {
- return buf[1];
- }
- }
-}
-float CircularBuffer::median() {
- if (_length == 1) {
- return buf[0];
- }
- if (_length == 2) {
- if (num < 2) return buf[0];
- return (buf[0] + buf[1]) / 2.0f;
- }
- if (_length == 3) {
- if (num < 2) return buf[0];
- if (num == 2) return (buf[0] + buf[1]) / 2.0f;
- return median3(buf);
- }
- if (num < _length) {
- if (num % 2 == 1) {
- return sorted[(num - 1) / 2];
- } else {
- return (sorted[num / 2] + sorted[num / 2 - 1]) / 2.0f;
- }
- }
- else {
- if (_length % 2 == 1) {
- return sorted[(_length - 1) / 2];
- } else {
- return (sorted[_length / 2] + sorted[_length / 2 - 1]) / 2.0f;
- }
- }
-}
\ No newline at end of file
--- a/Filter/Filter.cpp Tue Oct 02 07:12:01 2018 +0000
+++ b/Filter/Filter.cpp Wed Oct 17 02:59:26 2018 +0000
@@ -3,7 +3,7 @@
#include "Filter.h"
MedianFilter::MedianFilter(int length) {
- buf = new CircularBuffer(length, true);
+ buf = new fCircularBuffer(length, true);
}
float MedianFilter::update(float x) {
@@ -12,7 +12,7 @@
}
MovingAverageFilter::MovingAverageFilter(int length) {
- buf = new CircularBuffer(length, false);
+ buf = new fCircularBuffer(length, false);
}
float MovingAverageFilter::update(float x) {
--- a/Filter/Filter.h Tue Oct 02 07:12:01 2018 +0000
+++ b/Filter/Filter.h Wed Oct 17 02:59:26 2018 +0000
@@ -4,9 +4,9 @@
#include "mbed.h"
#include "math.h"
-class CircularBuffer {
+class fCircularBuffer {
public:
- CircularBuffer(int length, bool use_median);
+ fCircularBuffer(int length, bool use_median);
float oldest() {if (oldest_index >= 0) return buf[oldest_index]; return 0.0f;}
float newest() {if (newest_index >= 0) return buf[newest_index]; return 0.0f;}
int length() {return _length;}
@@ -38,7 +38,7 @@
MedianFilter(int length);
virtual float update(float x);
private:
- CircularBuffer *buf;
+ fCircularBuffer *buf;
};
class MovingAverageFilter : public Filter {
@@ -46,7 +46,7 @@
MovingAverageFilter(int length);
virtual float update(float x);
private:
- CircularBuffer *buf;
+ fCircularBuffer *buf;
};
#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Filter/fCircularBuffer.cpp Wed Oct 17 02:59:26 2018 +0000
@@ -0,0 +1,152 @@
+#include "mbed.h"
+#include "math.h"
+#include "Filter.h"
+
+fCircularBuffer::fCircularBuffer(int length, bool use_median) {
+ _length = length;
+ _use_median = use_median;
+
+ oldest_index = 0;
+ newest_index = -1;
+ num = 0;
+ sum = 0.0f;
+
+ buf = (float*)malloc(_length * sizeof(float));
+ sorted = (float*)malloc(_length * sizeof(float));
+ for (int i = 0; i < _length; i++) {
+ buf[i] = 0.0f;
+ sorted[i] = 0.0f;
+ }
+}
+
+float &fCircularBuffer::at(int index) {
+ int actual = oldest_index + index;
+ if (actual >= _length) actual -= _length;
+ return buf[actual];
+}
+
+void fCircularBuffer::add(float x) {
+ if (num < _length) {
+ newest_index++;
+ buf[newest_index] = x;
+ sum += x;
+ num++;
+
+ if (!_use_median || _length < 4) return;
+
+ /*insert x into sorted array*/
+ int i = num - 1;
+ while (i > 0 && sorted[i - 1] > x) {
+ sorted[i] = sorted[i - 1];
+ i--;
+ }
+ sorted[i] = x;
+ } else {
+ /*update circular buffer*/
+ float oldest = buf[oldest_index];
+
+ sum -= buf[oldest_index];
+ oldest_index++;
+ if (oldest_index >= _length) oldest_index -= _length;
+
+ newest_index++;
+ if (newest_index >= _length) newest_index -= _length;
+ buf[newest_index] = x;
+
+ sum += x;
+
+ if (!_use_median || _length < 4) return;
+
+ /*find sorted index of oldest element*/
+ int removed;
+ for (removed = 0; removed < _length; removed++) {
+ if (sorted[removed] == oldest) break;
+ }
+
+ /*insert x*/
+ int i;
+ if (removed == _length - 1) {
+ i = _length - 1;
+ while (i > 0 && sorted[i - 1] > x) {
+ sorted[i] = sorted[i - 1];
+ i--;
+ }
+ sorted[i] = x;
+ } else if (removed == 0) {
+ i = 0;
+ while (i < _length - 1 && sorted[i + 1] < x) {
+ sorted[i] = sorted[i + 1];
+ i++;
+ }
+ sorted[i] = x;
+ } else if (sorted[removed - 1] <= x && sorted[removed + 1] >= x) {
+ sorted[removed] = x;
+ } else if (sorted[removed - 1] > x) {
+ i = removed;
+ while (i > 0 && sorted[i - 1] > x) {
+ sorted[i] = sorted[i - 1];
+ i--;
+ }
+ sorted[i] = x;
+ } else {
+ i = removed;
+ while (i < _length - 1 && sorted[i + 1] < x) {
+ sorted[i] = sorted[i + 1];
+ i++;
+ }
+ sorted[i] = x;
+ }
+ }
+}
+
+float fCircularBuffer::mean() {
+ return sum / num;
+}
+
+float median3(float *buf) {
+ if (buf[0] > buf[1]) {
+ if (buf[1] > buf[2]) {
+ return buf[1];
+ } else if (buf[0] > buf[2]) {
+ return buf[2];
+ } else {
+ return buf[0];
+ }
+ } else {
+ if (buf[0] > buf[2]) {
+ return buf[0];
+ } else if (buf[1] > buf[2]) {
+ return buf[2];
+ } else {
+ return buf[1];
+ }
+ }
+}
+float fCircularBuffer::median() {
+ if (_length == 1) {
+ return buf[0];
+ }
+ if (_length == 2) {
+ if (num < 2) return buf[0];
+ return (buf[0] + buf[1]) / 2.0f;
+ }
+ if (_length == 3) {
+ if (num < 2) return buf[0];
+ if (num == 2) return (buf[0] + buf[1]) / 2.0f;
+ return median3(buf);
+ }
+ if (num < _length) {
+ if (num % 2 == 1) {
+ return sorted[(num - 1) / 2];
+ } else {
+ return (sorted[num / 2] + sorted[num / 2 - 1]) / 2.0f;
+ }
+ }
+ else {
+ if (_length % 2 == 1) {
+ return sorted[(_length - 1) / 2];
+ } else {
+ return (sorted[_length / 2] + sorted[_length / 2 - 1]) / 2.0f;
+ }
+ }
+}
\ No newline at end of file