GestureRecognitionToolkit  Version: 0.2.5
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
RMSFilter.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 "RMSFilter.h"
23 
24 GRT_BEGIN_NAMESPACE
25 
26 //Define the string that will be used to identify the object
27 const std::string RMSFilter::id = "RMSFilter";
28 std::string RMSFilter::getId() { return RMSFilter::id; }
29 
30 //Register the RMSFiltermodule with the PreProcessing base class
31 RegisterPreProcessingModule< RMSFilter> RMSFilter::registerModule( RMSFilter::getId() );
32 
33 RMSFilter::RMSFilter(const UINT filterSize,const UINT numDimensions) : PreProcessing( RMSFilter::getId() )
34 {
35  init(filterSize,numDimensions);
36 }
37 
39 {
40  //Zero this instance
41  this->filterSize = 0;
42  this->inputSampleCounter = 0;
43 
44  //Copy the settings from the rhs instance
45  *this = rhs;
46 }
47 
49 
50 }
51 
53  if(this!=&rhs){
54  //Clear this instance
55  this->filterSize = 0;
56  this->inputSampleCounter = 0;
57  this->dataBuffer.clear();
58 
59  //Copy from the rhs instance
60  if( rhs.initialized ){
61  this->init( rhs.filterSize, rhs.numInputDimensions );
62  this->dataBuffer = rhs.dataBuffer;
63  }
64 
65  //Copy the preprocessing base variables
67  }
68  return *this;
69 }
70 
71 bool RMSFilter::deepCopyFrom(const PreProcessing *preProcessing){
72 
73  if( preProcessing == NULL ) return false;
74 
75  if( this->getId() == preProcessing->getId() ){
76 
77  //Call the equals operator
78  *this = *dynamic_cast<const RMSFilter*>(preProcessing);
79 
80  return true;
81  }
82 
83  errorLog << "deepCopyFrom(const PreProcessing *preProcessing) - PreProcessing Types Do Not Match!" << std::endl;
84 
85  return false;
86 }
87 
88 
89 bool RMSFilter::process(const VectorFloat &inputVector){
90 
91  if( !initialized ){
92  errorLog << "process(const VectorFloat &inputVector) - The filter has not been initialized!" << std::endl;
93  return false;
94  }
95 
96  if( inputVector.getSize() != numInputDimensions ){
97  errorLog << "process(const VectorFloat &inputVector) - The size of the inputVector (" << inputVector.getSize() << ") does not match that of the filter (" << numInputDimensions << ")!" << std::endl;
98  return false;
99  }
100 
101  filter( inputVector );
102 
103  if( processedData.getSize() == numOutputDimensions ) return true;
104 
105  return false;
106 }
107 
109  if( initialized ) return init(filterSize,numInputDimensions);
110  return false;
111 }
112 
113 bool RMSFilter::save(std::fstream &file) const{
114 
115  if( !file.is_open() ){
116  errorLog << "save(fstream &file) - The file is not open!" << std::endl;
117  return false;
118  }
119 
120  file << "GRT_RMS_FILTER_FILE_V1.0" << std::endl;
121 
122  file << "NumInputDimensions: " << numInputDimensions << std::endl;
123  file << "NumOutputDimensions: " << numOutputDimensions << std::endl;
124  file << "FilterSize: " << filterSize << std::endl;
125 
126  return true;
127 }
128 
129 bool RMSFilter::load(std::fstream &file){
130 
131  if( !file.is_open() ){
132  errorLog << "load(fstream &file) - The file is not open!" << std::endl;
133  return false;
134  }
135 
136  std::string word;
137 
138  //Load the header
139  file >> word;
140 
141  if( word != "GRT_RMS_FILTER_FILE_V1.0" ){
142  errorLog << "load(fstream &file) - Invalid file format!" << std::endl;
143  return false;
144  }
145 
146  //Load the number of input dimensions
147  file >> word;
148  if( word != "NumInputDimensions:" ){
149  errorLog << "load(fstream &file) - Failed to read NumInputDimensions header!" << std::endl;
150  return false;
151  }
152  file >> numInputDimensions;
153 
154  //Load the number of output dimensions
155  file >> word;
156  if( word != "NumOutputDimensions:" ){
157  errorLog << "load(fstream &file) - Failed to read NumOutputDimensions header!" << std::endl;
158  return false;
159  }
160  file >> numOutputDimensions;
161 
162  //Load the filter factor
163  file >> word;
164  if( word != "FilterSize:" ){
165  errorLog << "load(fstream &file) - Failed to read FilterSize header!" << std::endl;
166  return false;
167  }
168  file >> filterSize;
169 
170  //Init the filter module to ensure everything is initialized correctly
171  return init(filterSize,numInputDimensions);
172 }
173 
174 bool RMSFilter::init(UINT filterSize,UINT numDimensions){
175 
176  //Cleanup the old memory
177  initialized = false;
178  inputSampleCounter = 0;
179 
180  if( filterSize == 0 ){
181  errorLog << "init(UINT filterSize,UINT numDimensions) - Filter size can not be zero!" << std::endl;
182  return false;
183  }
184 
185  if( numDimensions == 0 ){
186  errorLog << "init(UINT filterSize,UINT numDimensions) - The number of dimensions must be greater than zero!" << std::endl;
187  return false;
188  }
189 
190  //Resize the filter
191  this->filterSize = filterSize;
192  this->numInputDimensions = numDimensions;
193  this->numOutputDimensions = numDimensions;
194  processedData.clear();
195  processedData.resize(numDimensions,0);
196  initialized = dataBuffer.resize( filterSize, VectorFloat(numInputDimensions,0) );
197 
198  if( !initialized ){
199  errorLog << "init(UINT filterSize,UINT numDimensions) - Failed to resize dataBuffer!" << std::endl;
200  }
201 
202  return initialized;
203 }
204 
205 Float RMSFilter::filter(const Float x){
206 
207  //If the filter has not been initialised then return 0, otherwise filter x and return y
208  if( !initialized ){
209  errorLog << "filter(const Float x) - The filter has not been initialized!" << std::endl;
210  return 0;
211  }
212 
213  VectorFloat y = filter(VectorFloat(1,x));
214 
215  if( y.size() == 0 ) return 0;
216  return y[0];
217 }
218 
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 VectorFloat &x) - The filter has not been initialized!" << std::endl;
224  return VectorFloat();
225  }
226 
227  if( x.size() != numInputDimensions ){
228  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;
229  return VectorFloat();
230  }
231 
233 
234  //Add the new value to the buffer
235  dataBuffer.push_back( x );
236 
237  for(unsigned int j=0; j<numInputDimensions; j++){
238  processedData[j] = 0;
239  for(unsigned int i=0; i<inputSampleCounter; i++) {
240  processedData[j] += dataBuffer[i][j] * dataBuffer[i][j];
241  }
242  processedData[j] = sqrt( processedData[j] / Float(inputSampleCounter) );
243  }
244 
245  return processedData;
246 }
247 
248 GRT_END_NAMESPACE
bool push_back(const T &value)
UINT inputSampleCounter
A counter to keep track of the number of input samples.
Definition: RMSFilter.h:166
RMSFilter & operator=(const RMSFilter &rhs)
Definition: RMSFilter.cpp:52
std::string getId() const
Definition: GRTBase.cpp:85
virtual bool reset()
Definition: RMSFilter.cpp:108
virtual bool deepCopyFrom(const PreProcessing *preProcessing)
Definition: RMSFilter.cpp:71
static std::string getId()
Definition: RMSFilter.cpp:28
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
UINT getSize() const
Definition: Vector.h:201
Float filter(const Float x)
Definition: RMSFilter.cpp:205
virtual bool load(std::fstream &file)
Definition: RMSFilter.cpp:129
virtual bool process(const VectorFloat &inputVector)
Definition: RMSFilter.cpp:89
UINT filterSize
The size of the filter.
Definition: RMSFilter.h:165
virtual bool save(std::fstream &file) const
Definition: RMSFilter.cpp:113
bool copyBaseVariables(const PreProcessing *preProcessingModule)
virtual ~RMSFilter()
Definition: RMSFilter.cpp:48
CircularBuffer< VectorFloat > dataBuffer
A buffer to store the previous N values, N = filterSize.
Definition: RMSFilter.h:167
The RMSFilter implements a root mean squared (RMS) filter.
Definition: RMSFilter.h:37
RMSFilter(const UINT filterSize=5, const UINT numDimensions=1)
Definition: RMSFilter.cpp:33
bool resize(const unsigned int newBufferSize)