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.
Filter/Filter.h@253:a9bf7a9f7fbb, 2021-08-17 (annotated)
- Committer:
- j_f_jensen
- Date:
- Tue Aug 17 19:22:43 2021 +0000
- Revision:
- 253:a9bf7a9f7fbb
- Parent:
- 224:90172915d0fb
test
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| bwang | 150:08c13bfc7417 | 1 | #ifndef __FILTER_H |
| bwang | 150:08c13bfc7417 | 2 | #define __FILTER_H |
| bwang | 150:08c13bfc7417 | 3 | |
| bwang | 144:a9e7fa1c98d7 | 4 | #include "mbed.h" |
| bwang | 144:a9e7fa1c98d7 | 5 | #include "math.h" |
| bwang | 144:a9e7fa1c98d7 | 6 | |
| bwang | 224:90172915d0fb | 7 | class fCircularBuffer { |
| bwang | 144:a9e7fa1c98d7 | 8 | public: |
| bwang | 224:90172915d0fb | 9 | fCircularBuffer(int length, bool use_median); |
| bwang | 144:a9e7fa1c98d7 | 10 | float oldest() {if (oldest_index >= 0) return buf[oldest_index]; return 0.0f;} |
| bwang | 144:a9e7fa1c98d7 | 11 | float newest() {if (newest_index >= 0) return buf[newest_index]; return 0.0f;} |
| bwang | 144:a9e7fa1c98d7 | 12 | int length() {return _length;} |
| bwang | 144:a9e7fa1c98d7 | 13 | void add(float x); /*recomputes mean, median - O(length) time*/ |
| bwang | 144:a9e7fa1c98d7 | 14 | float &at(int index); |
| bwang | 144:a9e7fa1c98d7 | 15 | public: |
| bwang | 144:a9e7fa1c98d7 | 16 | float &operator[](int index) {return at(index);} |
| bwang | 144:a9e7fa1c98d7 | 17 | public: |
| bwang | 144:a9e7fa1c98d7 | 18 | /*O(1) time*/ |
| bwang | 144:a9e7fa1c98d7 | 19 | float mean(); |
| bwang | 144:a9e7fa1c98d7 | 20 | float median(); |
| bwang | 144:a9e7fa1c98d7 | 21 | private: |
| bwang | 144:a9e7fa1c98d7 | 22 | int _length; |
| bwang | 147:c1b2379b8874 | 23 | bool _use_median; |
| bwang | 144:a9e7fa1c98d7 | 24 | int oldest_index, newest_index, num; |
| bwang | 144:a9e7fa1c98d7 | 25 | float sum; |
| bwang | 144:a9e7fa1c98d7 | 26 | private: |
| bwang | 144:a9e7fa1c98d7 | 27 | float *buf; |
| bwang | 144:a9e7fa1c98d7 | 28 | float *sorted; |
| bwang | 146:296bcc30e65d | 29 | }; |
| bwang | 146:296bcc30e65d | 30 | |
| bwang | 146:296bcc30e65d | 31 | class Filter { |
| bwang | 146:296bcc30e65d | 32 | public: |
| bwang | 146:296bcc30e65d | 33 | virtual float update(float x) = 0; |
| bwang | 146:296bcc30e65d | 34 | }; |
| bwang | 146:296bcc30e65d | 35 | |
| bwang | 146:296bcc30e65d | 36 | class MedianFilter : public Filter { |
| bwang | 146:296bcc30e65d | 37 | public: |
| bwang | 146:296bcc30e65d | 38 | MedianFilter(int length); |
| bwang | 146:296bcc30e65d | 39 | virtual float update(float x); |
| bwang | 146:296bcc30e65d | 40 | private: |
| bwang | 224:90172915d0fb | 41 | fCircularBuffer *buf; |
| bwang | 146:296bcc30e65d | 42 | }; |
| bwang | 146:296bcc30e65d | 43 | |
| bwang | 146:296bcc30e65d | 44 | class MovingAverageFilter : public Filter { |
| bwang | 146:296bcc30e65d | 45 | public: |
| bwang | 146:296bcc30e65d | 46 | MovingAverageFilter(int length); |
| bwang | 146:296bcc30e65d | 47 | virtual float update(float x); |
| bwang | 146:296bcc30e65d | 48 | private: |
| bwang | 224:90172915d0fb | 49 | fCircularBuffer *buf; |
| bwang | 146:296bcc30e65d | 50 | }; |
| bwang | 150:08c13bfc7417 | 51 | |
| bwang | 150:08c13bfc7417 | 52 | #endif |