GestureRecognitionToolkit  Version: 0.1.0
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
WeightedAverageFilter.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 #include "WeightedAverageFilter.h"
22 
23 GRT_BEGIN_NAMESPACE
24 
25 //Register the WeightedAverageFilter module with the PreProcessing base class
26 RegisterPreProcessingModule< WeightedAverageFilter > WeightedAverageFilter::registerModule("WeightedAverageFilter");
27 
28 WeightedAverageFilter::WeightedAverageFilter(UINT filterSize,UINT numDimensions){
29 
30  classType = "WeightedAverageFilter";
31  preProcessingType = classType;
32  debugLog.setProceedingText("[DEBUG WeightedAverageFilter]");
33  errorLog.setProceedingText("[ERROR WeightedAverageFilter]");
34  warningLog.setProceedingText("[WARNING WeightedAverageFilter]");
35  init(filterSize,numDimensions);
36 }
37 
39 
40  classType = "WeightedAverageFilter";
41  preProcessingType = classType;
42  debugLog.setProceedingText("[DEBUG WeightedAverageFilter]");
43  errorLog.setProceedingText("[ERROR WeightedAverageFilter]");
44  warningLog.setProceedingText("[WARNING WeightedAverageFilter]");
45 
46  //Zero this instance
47  this->filterSize = 0;
48  this->inputSampleCounter = 0;
49 
50  //Copy the settings from the rhs instance
51  *this = rhs;
52 }
53 
55 
56 }
57 
59  if(this!=&rhs){
60  //Clear this instance
61  this->filterSize = 0;
62  this->inputSampleCounter = 0;
63  this->dataBuffer.clear();
64  this->weights = rhs.weights;
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 = *(WeightedAverageFilter*)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 
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.size() != numInputDimensions ){
104  errorLog << "process(const VectorFloat &inputVector) - The size of the inputVector (" << inputVector.size() << ") does not match that of the filter (" << numInputDimensions << ")!" << std::endl;
105  return false;
106  }
107 
108  filter( inputVector );
109 
110  if( processedData.size() == numOutputDimensions ) return true;
111 
112  return false;
113 }
114 
116  if( initialized ) return init(filterSize,numInputDimensions);
117  return false;
118 }
119 
120 bool WeightedAverageFilter::saveModelToFile( std::fstream &file ) const{
121 
122  if( !file.is_open() ){
123  errorLog << "saveModelToFile(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 WeightedAverageFilter::loadModelFromFile( std::fstream &file ){
137 
138  if( !file.is_open() ){
139  errorLog << "loadModelFromFile(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 << "loadModelFromFile(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 << "loadModelFromFile(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 << "loadModelFromFile(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 << "loadModelFromFile(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 WeightedAverageFilter::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  weights.clear();
203  processedData.resize(numDimensions,0);
204  weights.resize(filterSize);
205  initialized = dataBuffer.resize( filterSize, VectorFloat(numInputDimensions,0) );
206 
207  const Float norm = 1.0 / filterSize;
208  for(UINT i=0; i<filterSize; i++){
209  weights[i] = (i+1)*norm;
210  }
211 
212  if( !initialized ){
213  errorLog << "init(UINT filterSize,UINT numDimensions) - Failed to resize dataBuffer!" << std::endl;
214  }
215 
216  return initialized;
217 }
218 
219 Float WeightedAverageFilter::filter(const Float x){
220 
221  //If the filter has not been initialised then return 0, otherwise filter x and return y
222  if( !initialized ){
223  errorLog << "filter(const Float x) - The filter has not been initialized!" << std::endl;
224  return 0;
225  }
226 
227  VectorFloat y = filter(VectorFloat(1,x));
228 
229  if( y.size() == 0 ) return 0;
230  return y[0];
231 }
232 
234 
235  //If the filter has not been initialised then return 0, otherwise filter x and return y
236  if( !initialized ){
237  errorLog << "filter(const VectorFloat &x) - The filter has not been initialized!" << std::endl;
238  return VectorFloat();
239  }
240 
241  if( x.size() != numInputDimensions ){
242  errorLog << "filter(const VectorFloat &x) - The size of the input vector (" << x.size() << ") does not match that of the number of dimensions of the filter (" << numInputDimensions << ")!" << std::endl;
243  return VectorFloat();
244  }
245 
247 
248  //Add the new value to the buffer
249  dataBuffer.push_back( x );
250 
251  Float weightSum = 0;
252  for(unsigned int j=0; j<numInputDimensions; j++){
253  processedData[j] = 0;
254  weightSum = 0;
255  for(unsigned int i=0; i<inputSampleCounter; i++) {
256  processedData[j] += dataBuffer[i][j] * weights[i];
257  weightSum += weights[i];
258  }
259  if( weightSum != 0.0 ) processedData[j] /= weightSum;
260  }
261 
262  return processedData;
263 }
264 
265 GRT_END_NAMESPACE
266 
bool push_back(const T &value)
UINT inputSampleCounter
A counter to keep track of the number of input samples.
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
WeightedAverageFilter & operator=(const WeightedAverageFilter &rhs)
virtual bool loadModelFromFile(std::fstream &file)
CircularBuffer< VectorFloat > dataBuffer
A buffer to store the previous N values, N = filterSize.
virtual bool deepCopyFrom(const PreProcessing *preProcessing)
The WeightedAverageFilter implements a weighted average filter that gives a larger weight to more rec...
WeightedAverageFilter(UINT filterSize=5, UINT numDimensions=1)
std::string getPreProcessingType() const
virtual bool process(const VectorFloat &inputVector)
UINT filterSize
The size of the filter.
virtual bool saveModelToFile(std::fstream &file) const
Float filter(const Float x)
bool copyBaseVariables(const PreProcessing *preProcessingModule)
VectorFloat weights
Stores the weights for each sample in the buffer, the size of this vector will match the filterSize...
bool resize(const unsigned int newBufferSize)