APM:Libraries
ModeFilter.h
Go to the documentation of this file.
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 //
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);
34 
35  // get - get latest filtered value from filter (equal to the value returned by latest call to apply method)
36  virtual T get() const {
37  return _output;
38  }
39 
40 private:
41  // private methods
42  uint8_t _return_element;
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
74 
75 // Constructor //////////////////////////////////////////////////////////////
76 
77 template <class T, uint8_t FILTER_SIZE>
78 ModeFilter<T,FILTER_SIZE>::ModeFilter(uint8_t return_element) :
79  FilterWithBuffer<T,FILTER_SIZE>(),
80  _return_element(return_element),
81  drop_high_sample(true)
82 {
83  // ensure we have a valid return_nth_element value. if not, revert to median
84  if( _return_element >= FILTER_SIZE )
85  _return_element = FILTER_SIZE / 2;
86 };
87 
88 // Public Methods //////////////////////////////////////////////////////////////
89 
90 template <class T, uint8_t FILTER_SIZE>
92 {
93  // insert the new items into the samples buffer
94  isort(sample, drop_high_sample);
95 
96  // next time drop from the other end of the sample buffer
98 
99  // return results
101  // middle sample if buffer is not yet full
103  }else{
104  // return element specified by user in constructor
106  }
107 }
108 
109 //
110 // insertion sort - takes a new sample and pushes it into the sample array
111 // drops either the highest or lowest sample depending on the 'drop_high_sample' parameter
112 //
113 template <class T, uint8_t FILTER_SIZE>
114 void ModeFilter<T,FILTER_SIZE>:: isort(T new_sample, bool drop_high)
115 {
116  int8_t i;
117 
118  // if the buffer isn't full simply increase the #items in the buffer (i.e. sample_index)
119  // the rest is the same as dropping the high sample
122  drop_high = true;
123  }
124 
125  if( drop_high ) { // drop highest sample from the buffer to make room for our new sample
126 
127  // start from top. Note: sample_index always points to the next open space so we start from sample_index-1
129 
130  // if the next element is higher than our new sample, push it up one position
131  while(i > 0 && FilterWithBuffer<T,FILTER_SIZE>::samples[i-1] > new_sample) {
133  i--;
134  }
135 
136  // add our new sample to the buffer
138 
139  }else{ // drop lowest sample from the buffer to make room for our new sample
140 
141  // start from the bottom
142  i = 0;
143 
144  // if the element is lower than our new sample, push it down one position
147  i++;
148  }
149 
150  // add our new sample to the buffer
152  }
153 }
ModeFilter< uint8_t, 4 > ModeFilterUInt8_Size4
Definition: ModeFilter.h:55
ModeFilter< int8_t, 4 > ModeFilterInt8_Size4
Definition: ModeFilter.h:50
ModeFilter< int8_t, 5 > ModeFilterInt8_Size5
Definition: ModeFilter.h:51
ModeFilter< uint16_t, 3 > ModeFilterUInt16_Size3
Definition: ModeFilter.h:64
ModeFilter< uint8_t, 6 > ModeFilterUInt8_Size6
Definition: ModeFilter.h:57
ModeFilter< float, 6 > ModeFilterFloat_Size6
Definition: ModeFilter.h:72
ModeFilter< int8_t, 6 > ModeFilterInt8_Size6
Definition: ModeFilter.h:52
ModeFilter< uint8_t, 7 > ModeFilterUInt8_Size7
Definition: ModeFilter.h:58
ModeFilter< int16_t, 7 > ModeFilterInt16_Size7
Definition: ModeFilter.h:63
ModeFilter< uint16_t, 5 > ModeFilterUInt16_Size5
Definition: ModeFilter.h:66
ModeFilter< int8_t, 3 > ModeFilterInt8_Size3
Definition: ModeFilter.h:49
ModeFilter< uint8_t, 3 > ModeFilterUInt8_Size3
Definition: ModeFilter.h:54
ModeFilter< float, 4 > ModeFilterFloat_Size4
Definition: ModeFilter.h:70
ModeFilter< float, 7 > ModeFilterFloat_Size7
Definition: ModeFilter.h:73
void isort(T sample, bool drop_high_sample)
Definition: ModeFilter.h:114
ModeFilter< int16_t, 4 > ModeFilterInt16_Size4
Definition: ModeFilter.h:60
A filter with a buffer. This is implemented separately to the base Filter class to get around restric...
virtual T apply(T sample)
Definition: ModeFilter.h:91
ModeFilter< int16_t, 3 > ModeFilterInt16_Size3
Definition: ModeFilter.h:59
ModeFilter< float, 5 > ModeFilterFloat_Size5
Definition: ModeFilter.h:71
ModeFilter< int16_t, 5 > ModeFilterInt16_Size5
Definition: ModeFilter.h:61
ModeFilter< int16_t, 6 > ModeFilterInt16_Size6
Definition: ModeFilter.h:62
A pure virtual interface class.
ModeFilter< uint8_t, 5 > ModeFilterUInt8_Size5
Definition: ModeFilter.h:56
ModeFilter< uint16_t, 6 > ModeFilterUInt16_Size6
Definition: ModeFilter.h:67
uint8_t _return_element
Definition: ModeFilter.h:42
ModeFilter< float, 3 > ModeFilterFloat_Size3
Definition: ModeFilter.h:69
ModeFilter< uint16_t, 7 > ModeFilterUInt16_Size7
Definition: ModeFilter.h:68
ModeFilter< uint16_t, 4 > ModeFilterUInt16_Size4
Definition: ModeFilter.h:65
bool drop_high_sample
Definition: ModeFilter.h:45
ModeFilter< int8_t, 7 > ModeFilterInt8_Size7
Definition: ModeFilter.h:53
ModeFilter(uint8_t return_element)
Definition: ModeFilter.h:78