Bayley Wang
/
foc-ed_in_the_bot_compact
robot
Embed:
(wiki syntax)
Show/hide line numbers
Filter.h
00001 #ifndef __FILTER_H 00002 #define __FILTER_H 00003 00004 #include "mbed.h" 00005 #include "math.h" 00006 00007 class fCircularBuffer { 00008 public: 00009 fCircularBuffer(int length, bool use_median); 00010 float oldest() {if (oldest_index >= 0) return buf[oldest_index]; return 0.0f;} 00011 float newest() {if (newest_index >= 0) return buf[newest_index]; return 0.0f;} 00012 int length() {return _length;} 00013 void add(float x); /*recomputes mean, median - O(length) time*/ 00014 float &at(int index); 00015 public: 00016 float &operator[](int index) {return at(index);} 00017 public: 00018 /*O(1) time*/ 00019 float mean(); 00020 float median(); 00021 private: 00022 int _length; 00023 bool _use_median; 00024 int oldest_index, newest_index, num; 00025 float sum; 00026 private: 00027 float *buf; 00028 float *sorted; 00029 }; 00030 00031 class Filter { 00032 public: 00033 virtual float update(float x) = 0; 00034 }; 00035 00036 class MedianFilter : public Filter { 00037 public: 00038 MedianFilter(int length); 00039 virtual float update(float x); 00040 private: 00041 fCircularBuffer *buf; 00042 }; 00043 00044 class MovingAverageFilter : public Filter { 00045 public: 00046 MovingAverageFilter(int length); 00047 virtual float update(float x); 00048 private: 00049 fCircularBuffer *buf; 00050 }; 00051 00052 #endif
Generated on Tue Jul 12 2022 17:58:39 by 1.7.2