Line data Source code
1 : /* 2 : This program is free software: you can redistribute it and/or modify 3 : it under the terms of the GNU General Public License as published by 4 : the Free Software Foundation, either version 3 of the License, or 5 : (at your option) any later version. 6 : 7 : This program is distributed in the hope that it will be useful, 8 : but WITHOUT ANY WARRANTY; without even the implied warranty of 9 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 : GNU General Public License for more details. 11 : 12 : You should have received a copy of the GNU General Public License 13 : along with this program. If not, see <http://www.gnu.org/licenses/>. 14 : */ 15 : 16 : // 17 : /// @file ModeFilter.h 18 : /// @brief A class to apply a mode filter which is basically picking the median value from the last x samples 19 : /// the filter size (i.e buffer size) should always be an odd number 20 : #pragma once 21 : 22 : #include <inttypes.h> 23 : #include "FilterClass.h" 24 : #include "FilterWithBuffer.h" 25 : 26 : template <class T, uint8_t FILTER_SIZE> 27 : class ModeFilter : public FilterWithBuffer<T,FILTER_SIZE> 28 : { 29 : public: 30 : ModeFilter(uint8_t return_element); 31 : 32 : // apply - Add a new raw value to the filter, retrieve the filtered result 33 : virtual T apply(T sample) override; 34 : 35 : // get - get latest filtered value from filter (equal to the value returned by latest call to apply method) 36 4 : virtual T get() const { 37 4 : return _output; 38 : } 39 : 40 : private: 41 : // private methods 42 : uint8_t _return_element; 43 : T _output; 44 : void isort(T sample, bool drop_high_sample); 45 : bool drop_high_sample; // switch to determine whether to drop the highest or lowest sample when new value arrives 46 : }; 47 : 48 : // Typedef for convenience 49 : typedef ModeFilter<int8_t,3> ModeFilterInt8_Size3; 50 : typedef ModeFilter<int8_t,4> ModeFilterInt8_Size4; 51 : typedef ModeFilter<int8_t,5> ModeFilterInt8_Size5; 52 : typedef ModeFilter<int8_t,6> ModeFilterInt8_Size6; 53 : typedef ModeFilter<int8_t,7> ModeFilterInt8_Size7; 54 : typedef ModeFilter<uint8_t,3> ModeFilterUInt8_Size3; 55 : typedef ModeFilter<uint8_t,4> ModeFilterUInt8_Size4; 56 : typedef ModeFilter<uint8_t,5> ModeFilterUInt8_Size5; 57 : typedef ModeFilter<uint8_t,6> ModeFilterUInt8_Size6; 58 : typedef ModeFilter<uint8_t,7> ModeFilterUInt8_Size7; 59 : typedef ModeFilter<int16_t,3> ModeFilterInt16_Size3; 60 : typedef ModeFilter<int16_t,4> ModeFilterInt16_Size4; 61 : typedef ModeFilter<int16_t,5> ModeFilterInt16_Size5; 62 : typedef ModeFilter<int16_t,6> ModeFilterInt16_Size6; 63 : typedef ModeFilter<int16_t,7> ModeFilterInt16_Size7; 64 : typedef ModeFilter<uint16_t,3> ModeFilterUInt16_Size3; 65 : typedef ModeFilter<uint16_t,4> ModeFilterUInt16_Size4; 66 : typedef ModeFilter<uint16_t,5> ModeFilterUInt16_Size5; 67 : typedef ModeFilter<uint16_t,6> ModeFilterUInt16_Size6; 68 : typedef ModeFilter<uint16_t,7> ModeFilterUInt16_Size7; 69 : typedef ModeFilter<float,3> ModeFilterFloat_Size3; 70 : typedef ModeFilter<float,4> ModeFilterFloat_Size4; 71 : typedef ModeFilter<float,5> ModeFilterFloat_Size5; 72 : typedef ModeFilter<float,6> ModeFilterFloat_Size6; 73 : typedef ModeFilter<float,7> ModeFilterFloat_Size7;