APM:Libraries
FilterWithBuffer.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 //
22 #pragma once
23 
24 #include "FilterClass.h"
25 
26 template <class T, uint8_t FILTER_SIZE>
27 class FilterWithBuffer : public Filter<T>
28 {
29 public:
30  // constructor
32 
33  // apply - Add a new raw value to the filter, retrieve the filtered result
34  virtual T apply(T sample);
35 
36  // reset - clear the filter
37  virtual void reset();
38 
39  // get filter size
40  uint8_t get_filter_size() const {
41  return FILTER_SIZE;
42  };
43 
44  T get_sample(uint8_t i) const {
45  return samples[i];
46  }
47 
48 protected:
49  T samples[FILTER_SIZE]; // buffer of samples
50  uint8_t sample_index; // pointer to the next empty slot in the buffer
51 };
52 
53 // Typedef for convenience
66 
79 
80 // Constructor
81 template <class T, uint8_t FILTER_SIZE>
83  sample_index(0)
84 {
85  // clear sample buffer
86  reset();
87 }
88 
89 // reset - clear all samples from the buffer
90 template <class T, uint8_t FILTER_SIZE>
92 {
93  // clear samples buffer
94  for( int8_t i=0; i<FILTER_SIZE; i++ ) {
95  samples[i] = 0;
96  }
97 
98  // reset index back to beginning of the array
99  sample_index = 0;
100 }
101 
102 // apply - take in a new raw sample, and return the filtered results
103 template <class T, uint8_t FILTER_SIZE>
105 {
106  // add sample to array
107  samples[sample_index++] = sample;
108 
109  // wrap index if necessary
110  if( sample_index >= FILTER_SIZE )
111  sample_index = 0;
112 
113  // base class doesn't know what filtering to do so we just return the raw sample
114  return sample;
115 }
FilterWithBuffer< uint16_t, 2 > FilterWithBufferUInt16_Size2
FilterWithBuffer< int32_t, 4 > FilterWithBufferInt32_Size4
FilterWithBuffer< int16_t, 3 > FilterWithBufferInt16_Size3
FilterWithBuffer< int32_t, 6 > FilterWithBufferInt32_Size6
FilterWithBuffer< uint32_t, 7 > FilterWithBufferUInt32_Size7
virtual T apply(T sample)
FilterWithBuffer< int16_t, 2 > FilterWithBufferInt16_Size2
FilterWithBuffer< uint32_t, 5 > FilterWithBufferUInt32_Size5
FilterWithBuffer< uint16_t, 6 > FilterWithBufferUInt16_Size6
FilterWithBuffer< uint32_t, 2 > FilterWithBufferUInt32_Size2
T get_sample(uint8_t i) const
T samples[FILTER_SIZE]
FilterWithBuffer< int16_t, 4 > FilterWithBufferInt16_Size4
FilterWithBuffer< int16_t, 7 > FilterWithBufferInt16_Size7
FilterWithBuffer< int16_t, 6 > FilterWithBufferInt16_Size6
FilterWithBuffer< uint16_t, 3 > FilterWithBufferUInt16_Size3
FilterWithBuffer< int32_t, 7 > FilterWithBufferInt32_Size7
FilterWithBuffer< uint32_t, 4 > FilterWithBufferUInt32_Size4
FilterWithBuffer< uint32_t, 6 > FilterWithBufferUInt32_Size6
uint8_t get_filter_size() const
FilterWithBuffer< uint16_t, 5 > FilterWithBufferUInt16_Size5
A pure virtual interface class.
FilterWithBuffer< int32_t, 3 > FilterWithBufferInt32_Size3
FilterWithBuffer< int32_t, 5 > FilterWithBufferInt32_Size5
FilterWithBuffer< int32_t, 2 > FilterWithBufferInt32_Size2
FilterWithBuffer< uint32_t, 3 > FilterWithBufferUInt32_Size3
FilterWithBuffer< uint16_t, 4 > FilterWithBufferUInt16_Size4
FilterWithBuffer< uint16_t, 7 > FilterWithBufferUInt16_Size7
FilterWithBuffer< int16_t, 5 > FilterWithBufferInt16_Size5
virtual void reset()