GestureRecognitionToolkit  Version: 0.2.0
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
MovingAverageFilter.cpp
1 /*
2 GRT MIT License
3 Copyright (c) <2012> <Nicholas Gillian, Media Lab, MIT>
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
6 and associated documentation files (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all copies or substantial
12 portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
15 LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
18 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 */
20 
21 #define GRT_DLL_EXPORTS
22 #include "MovingAverageFilter.h"
23 
24 GRT_BEGIN_NAMESPACE
25 
26 //Register the MovingAverageFilter module with the PreProcessing base class
27 RegisterPreProcessingModule< MovingAverageFilter > MovingAverageFilter::registerModule("MovingAverageFilter");
28 
29 MovingAverageFilter::MovingAverageFilter(UINT filterSize,UINT numDimensions){
30 
31  classType = "MovingAverageFilter";
32  preProcessingType = classType;
33  debugLog.setProceedingText("[DEBUG MovingAverageFilter]");
34  errorLog.setProceedingText("[ERROR MovingAverageFilter]");
35  warningLog.setProceedingText("[WARNING MovingAverageFilter]");
36  init(filterSize,numDimensions);
37 }
38 
40 
41  classType = "MovingAverageFilter";
42  preProcessingType = classType;
43  debugLog.setProceedingText("[DEBUG MovingAverageFilter]");
44  errorLog.setProceedingText("[ERROR MovingAverageFilter]");
45  warningLog.setProceedingText("[WARNING MovingAverageFilter]");
46 
47  //Zero this instance
48  this->filterSize = 0;
49  this->inputSampleCounter = 0;
50 
51  //Copy the settings from the rhs instance
52  *this = rhs;
53 }
54 
56 
57 }
58 
60  if(this!=&rhs){
61  //Clear this instance
62  this->filterSize = 0;
63  this->inputSampleCounter = 0;
64  this->dataBuffer.clear();
65 
66  //Copy from the rhs instance
67  if( rhs.initialized ){
68  this->init( rhs.filterSize, rhs.numInputDimensions );
69  this->dataBuffer = rhs.dataBuffer;
70  }
71 
72  //Copy the preprocessing base variables
74  }
75  return *this;
76 }
77 
79 
80  if( preProcessing == NULL ) return false;
81 
82  if( this->getPreProcessingType() == preProcessing->getPreProcessingType() ){
83 
84  //Call the equals operator
85  *this = *(MovingAverageFilter*)preProcessing;
86 
87  return true;
88  }
89 
90  errorLog << "clone(const PreProcessing *preProcessing) - PreProcessing Types Do Not Match!" << std::endl;
91 
92  return false;
93 }
94 
95 
96 bool MovingAverageFilter::process(const VectorFloat &inputVector){
97 
98  if( !initialized ){
99  errorLog << "process(const VectorFloat &inputVector) - The filter has not been initialized!" << std::endl;
100  return false;
101  }
102 
103  if( inputVector.getSize() != numInputDimensions ){
104  errorLog << "process(const VectorFloat &inputVector) - The size of the inputVector (" << inputVector.getSize() << ") does not match that of the filter (" << numInputDimensions << ")!" << std::endl;
105  return false;
106  }
107 
108  filter( inputVector );
109 
110  if( processedData.getSize() == numOutputDimensions ) return true;
111 
112  return false;
113 }
114 
116  if( initialized ) return init(filterSize,numInputDimensions);
117  return false;
118 }
119 
120 bool MovingAverageFilter::save(std::fstream &file) const{
121 
122  if( !file.is_open() ){
123  errorLog << "save(fstream &file) - The file is not open!" << std::endl;
124  return false;
125  }
126 
127  file << "GRT_MOVING_AVERAGE_FILTER_FILE_V1.0" << std::endl;
128 
129  file << "NumInputDimensions: " << numInputDimensions << std::endl;
130  file << "NumOutputDimensions: " << numOutputDimensions << std::endl;
131  file << "FilterSize: " << filterSize << std::endl;
132 
133  return true;
134 }
135 
136 bool MovingAverageFilter::load(std::fstream &file){
137 
138  if( !file.is_open() ){
139  errorLog << "load(fstream &file) - The file is not open!" << std::endl;
140  return false;
141  }
142 
143  std::string word;
144 
145  //Load the header
146  file >> word;
147 
148  if( word != "GRT_MOVING_AVERAGE_FILTER_FILE_V1.0" ){
149  errorLog << "load(fstream &file) - Invalid file format!" << std::endl;
150  return false;
151  }
152 
153  //Load the number of input dimensions
154  file >> word;
155  if( word != "NumInputDimensions:" ){
156  errorLog << "load(fstream &file) - Failed to read NumInputDimensions header!" << std::endl;
157  return false;
158  }
159  file >> numInputDimensions;
160 
161  //Load the number of output dimensions
162  file >> word;
163  if( word != "NumOutputDimensions:" ){
164  errorLog << "load(fstream &file) - Failed to read NumOutputDimensions header!" << std::endl;
165  return false;
166  }
167  file >> numOutputDimensions;
168 
169  //Load the filter factor
170  file >> word;
171  if( word != "FilterSize:" ){
172  errorLog << "load(fstream &file) - Failed to read FilterSize header!" << std::endl;
173  return false;
174  }
175  file >> filterSize;
176 
177  //Init the filter module to ensure everything is initialized correctly
178  return init(filterSize,numInputDimensions);
179 }
180 
181 bool MovingAverageFilter::init(UINT filterSize,UINT numDimensions){
182 
183  //Cleanup the old memory
184  initialized = false;
185  inputSampleCounter = 0;
186 
187  if( filterSize == 0 ){
188  errorLog << "init(UINT filterSize,UINT numDimensions) - Filter size can not be zero!" << std::endl;
189  return false;
190  }
191 
192  if( numDimensions == 0 ){
193  errorLog << "init(UINT filterSize,UINT numDimensions) - The number of dimensions must be greater than zero!" << std::endl;
194  return false;
195  }
196 
197  //Resize the filter
198  this->filterSize = filterSize;
199  this->numInputDimensions = numDimensions;
200  this->numOutputDimensions = numDimensions;
201  processedData.clear();
202  processedData.resize(numDimensions,0);
203  initialized = dataBuffer.resize( filterSize, VectorFloat(numInputDimensions,0) );
204 
205  if( !initialized ){
206  errorLog << "init(UINT filterSize,UINT numDimensions) - Failed to resize dataBuffer!" << std::endl;
207  }
208 
209  return initialized;
210 }
211 
212 Float MovingAverageFilter::filter(const Float x){
213 
214  //If the filter has not been initialised then return 0, otherwise filter x and return y
215  if( !initialized ){
216  errorLog << "filter(const Float x) - The filter has not been initialized!" << std::endl;
217  return 0;
218  }
219 
220  VectorFloat y = filter(VectorFloat(1,x));
221 
222  if( y.size() == 0 ) return 0;
223  return y[0];
224 }
225 
227 
228  //If the filter has not been initialised then return 0, otherwise filter x and return y
229  if( !initialized ){
230  errorLog << "filter(const VectorFloat &x) - The filter has not been initialized!" << std::endl;
231  return VectorFloat();
232  }
233 
234  if( x.size() != numInputDimensions ){
235  errorLog << "filter(const VectorFloat &x) - The size of the input vector (" << x.getSize() << ") does not match that of the number of dimensions of the filter (" << numInputDimensions << ")!" << std::endl;
236  return VectorFloat();
237  }
238 
240 
241  //Add the new value to the buffer
242  dataBuffer.push_back( x );
243 
244  for(unsigned int j=0; j<numInputDimensions; j++){
245  processedData[j] = 0;
246  for(unsigned int i=0; i<inputSampleCounter; i++) {
247  processedData[j] += dataBuffer[i][j];
248  }
249  processedData[j] /= Float(inputSampleCounter);
250  }
251 
252  return processedData;
253 }
254 
255 GRT_END_NAMESPACE
virtual bool process(const VectorFloat &inputVector)
bool push_back(const T &value)
virtual bool save(std::fstream &file) const
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
The MovingAverageFilter implements a low pass moving average filter.
UINT getSize() const
Definition: Vector.h:191
CircularBuffer< VectorFloat > dataBuffer
A buffer to store the previous N values, N = filterSize.
virtual bool deepCopyFrom(const PreProcessing *preProcessing)
Float filter(const Float x)
std::string getPreProcessingType() const
UINT filterSize
The size of the filter.
MovingAverageFilter(UINT filterSize=5, UINT numDimensions=1)
MovingAverageFilter & operator=(const MovingAverageFilter &rhs)
virtual bool load(std::fstream &file)
bool copyBaseVariables(const PreProcessing *preProcessingModule)
UINT inputSampleCounter
A counter to keep track of the number of input samples.
bool resize(const unsigned int newBufferSize)