APM:Libraries
AverageFilter.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 //
19 #pragma once
20 
21 #include "FilterClass.h"
22 #include "FilterWithBuffer.h"
23 
24 // 1st parameter <T> is the type of data being filtered.
25 // 2nd parameter <U> is a larger data type used during summation to prevent overflows
26 // 3rd parameter <FILTER_SIZE> is the number of elements in the filter
27 template <class T, class U, uint8_t FILTER_SIZE>
28 class AverageFilter : public FilterWithBuffer<T,FILTER_SIZE>
29 {
30 public:
31  // constructor
32  AverageFilter() : FilterWithBuffer<T,FILTER_SIZE>(), _num_samples(0) {
33  };
34 
35  // apply - Add a new raw value to the filter, retrieve the filtered result
36  virtual T apply(T sample);
37 
38  // reset - clear the filter
39  virtual void reset();
40 
41 protected:
42  // the number of samples in the filter, maxes out at size of the filter
43  uint8_t _num_samples;
44 };
45 
46 // Typedef for convenience (1st argument is the data type, 2nd is a larger datatype to handle overflows, 3rd is buffer size)
55 
64 
73 
75 
76 // Public Methods //////////////////////////////////////////////////////////////
77 
78 template <class T, class U, uint8_t FILTER_SIZE>
80 {
81  U result = 0;
82 
83  // call parent's apply function to get the sample into the array
85 
86  // increment the number of samples so far
87  _num_samples++;
88  if( _num_samples > FILTER_SIZE || _num_samples == 0 )
89  _num_samples = FILTER_SIZE;
90 
91  // get sum of all values - there is a risk of overflow here that we ignore
92  for(uint8_t i=0; i<FILTER_SIZE; i++)
94 
95  return (T)(result / _num_samples);
96 }
97 
98 // reset - clear all samples
99 template <class T, class U, uint8_t FILTER_SIZE>
101 {
102  // call parent's apply function to get the sample into the array
104 
105  // clear our variable
106  _num_samples = 0;
107 }
108 
109 /*
110  * This filter is intended to be used with integral types to be faster and
111  * avoid loss of precision on floating point arithmetic. The integral type
112  * chosen must be one that fits FILTER_SIZE values you are filtering.
113  *
114  * Differently from other average filters, the result is only returned when
115  * getf()/getd() is called
116  */
117 template <class T, class U, uint8_t FILTER_SIZE>
118 class AverageIntegralFilter : public AverageFilter<T,U,FILTER_SIZE>
119 {
120 public:
121  /*
122  * Add a new raw value to the filter: method signature is maintained from
123  * AverageFilter, but it doesn't retrieve the filtered value: return value
124  * is always 0. Call getf()/getd() in order to get the filtered value.
125  */
126  virtual T apply(T sample) override;
127 
128  // get the current value as a float
129  virtual float getf();
130 
131  // get the current value as a double
132  virtual double getd();
133 protected:
134  // the current sum of samples
135  U _sum = 0;
136 };
137 
138 template <class T, class U, uint8_t FILTER_SIZE>
140 {
141  T curr = this->samples[this->sample_index];
142 
143  // call parent's parent apply function to get the sample into the array
145 
146  // increment the number of samples so far
147  this->_num_samples++;
148  if (this->_num_samples > FILTER_SIZE || this->_num_samples == 0) {
149  this->_num_samples = FILTER_SIZE;
150  }
151 
152  _sum -= curr;
153  _sum += sample;
154 
155  // don't return the value: caller is forced to call getf() or getd()
156  return 0;
157 }
158 
159 template <class T, class U, uint8_t FILTER_SIZE>
161 {
162  if (this->_num_samples == 0) {
163  return 0.f;
164  }
165 
166  return (float)_sum / this->_num_samples;
167 }
168 
169 template <class T, class U, uint8_t FILTER_SIZE>
171 {
172  if (this->_num_samples == 0) {
173  return 0.f;
174  }
175 
176  return (double)_sum / this->_num_samples;
177 }
AverageFilter< float, float, 5 > AverageFilterFloat_Size5
Definition: AverageFilter.h:74
AverageFilter< int32_t, float, 3 > AverageFilterInt32_Size3
Definition: AverageFilter.h:66
AverageFilter< uint16_t, uint32_t, 4 > AverageFilterUInt16_Size4
Definition: AverageFilter.h:62
AverageFilter< int8_t, int16_t, 5 > AverageFilterInt8_Size5
Definition: AverageFilter.h:50
AverageFilter< int8_t, int16_t, 3 > AverageFilterInt8_Size3
Definition: AverageFilter.h:48
virtual T apply(T sample)
Definition: AverageFilter.h:79
virtual T apply(T sample)
const char * result
Definition: Printf.cpp:16
A filter with a buffer. This is implemented separately to the base Filter class to get around restric...
AverageFilter< int16_t, int32_t, 3 > AverageFilterInt16_Size3
Definition: AverageFilter.h:57
AverageFilter< int16_t, int32_t, 2 > AverageFilterInt16_Size2
Definition: AverageFilter.h:56
AverageFilter< int16_t, int32_t, 4 > AverageFilterInt16_Size4
Definition: AverageFilter.h:58
AverageFilter< int32_t, float, 2 > AverageFilterInt32_Size2
Definition: AverageFilter.h:65
AverageFilter< int32_t, float, 5 > AverageFilterInt32_Size5
Definition: AverageFilter.h:68
virtual T apply(T sample) override
T samples[FILTER_SIZE]
AverageFilter< int32_t, float, 4 > AverageFilterInt32_Size4
Definition: AverageFilter.h:67
AverageFilter< uint32_t, float, 5 > AverageFilterUInt32_Size5
Definition: AverageFilter.h:72
AverageFilter< int8_t, int16_t, 4 > AverageFilterInt8_Size4
Definition: AverageFilter.h:49
virtual double getd()
AverageFilter< int16_t, int32_t, 5 > AverageFilterInt16_Size5
Definition: AverageFilter.h:59
AverageFilter< uint32_t, float, 4 > AverageFilterUInt32_Size4
Definition: AverageFilter.h:71
uint8_t _num_samples
Definition: AverageFilter.h:43
AverageFilter< int8_t, int16_t, 2 > AverageFilterInt8_Size2
Definition: AverageFilter.h:47
AverageFilter< uint8_t, uint16_t, 3 > AverageFilterUInt8_Size3
Definition: AverageFilter.h:52
virtual void reset()
AverageFilter< uint32_t, float, 3 > AverageFilterUInt32_Size3
Definition: AverageFilter.h:70
AverageFilter< uint16_t, uint32_t, 2 > AverageFilterUInt16_Size2
Definition: AverageFilter.h:60
AverageFilter< uint16_t, uint32_t, 5 > AverageFilterUInt16_Size5
Definition: AverageFilter.h:63
AverageFilter< uint32_t, float, 2 > AverageFilterUInt32_Size2
Definition: AverageFilter.h:69
AverageFilter< uint8_t, uint16_t, 5 > AverageFilterUInt8_Size5
Definition: AverageFilter.h:54
A pure virtual interface class.
AverageFilter< uint8_t, uint16_t, 4 > AverageFilterUInt8_Size4
Definition: AverageFilter.h:53
AverageFilter< uint16_t, uint32_t, 3 > AverageFilterUInt16_Size3
Definition: AverageFilter.h:61
virtual float getf()
AverageFilter< uint8_t, uint16_t, 2 > AverageFilterUInt8_Size2
Definition: AverageFilter.h:51
virtual void reset()