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.
LeakyIntegrator.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 "LeakyIntegrator.h"
23 
24 GRT_BEGIN_NAMESPACE
25 
26 //Register the LeakyIntegrator module with the PreProcessing base class
27 RegisterPreProcessingModule< LeakyIntegrator > LeakyIntegrator::registerModule("LeakyIntegrator");
28 
29 LeakyIntegrator::LeakyIntegrator(const Float leakRate,const UINT numDimensions){
30  classType = "LeakyIntegrator";
31  preProcessingType = classType;
32  debugLog.setProceedingText("[DEBUG LeakyIntegrator]");
33  errorLog.setProceedingText("[ERROR LeakyIntegrator]");
34  warningLog.setProceedingText("[WARNING LeakyIntegrator]");
35  init(leakRate,numDimensions);
36 }
37 
39 
40  classType = "LeakyIntegrator";
41  preProcessingType = classType;
42  debugLog.setProceedingText("[DEBUG LeakyIntegrator]");
43  errorLog.setProceedingText("[ERROR LeakyIntegrator]");
44  warningLog.setProceedingText("[WARNING LeakyIntegrator]");
45 
46  this->leakRate = rhs.leakRate;
47  this->y = rhs.y;
48 
50 }
51 
53 
54 }
55 
57  if( this != &rhs ){
58  this->leakRate = rhs.leakRate;
59  this->y = rhs.y;
61  }
62  return *this;
63 }
64 
65 bool LeakyIntegrator::deepCopyFrom(const PreProcessing *preProcessing){
66 
67  if( preProcessing == NULL ) return false;
68 
69  if( this->getPreProcessingType() == preProcessing->getPreProcessingType() ){
70 
71  LeakyIntegrator *ptr = (LeakyIntegrator*)preProcessing;
72 
73  //Clone the LeakyIntegrator values
74  this->leakRate = ptr->leakRate;
75  this->y = ptr->y;
76 
77  //Clone the base class variables
78  return copyBaseVariables( preProcessing );
79  }
80 
81  errorLog << "clone(const PreProcessing *preProcessing) - PreProcessing Types Do Not Match!" << std::endl;
82 
83  return false;
84 }
85 
86 bool LeakyIntegrator::process(const VectorFloat &inputVector){
87 
88  if( !initialized ){
89  errorLog << "process(const VectorFloat &inputVector) - Not initialized!" << std::endl;
90  return false;
91  }
92 
93  if( inputVector.size() != numInputDimensions ){
94  errorLog << "process(const VectorFloat &inputVector) - The size of the inputVector (" << inputVector.size() << ") does not match that of the filter (" << numInputDimensions << ")!" << std::endl;
95  return false;
96  }
97 
98  update( inputVector );
99 
100  if( processedData.size() == numOutputDimensions ) return true;
101  return false;
102 }
103 
105  if( initialized ) return init(leakRate, numInputDimensions);
106  return false;
107 }
108 
109 bool LeakyIntegrator::save( std::fstream &file ) const{
110 
111  if( !file.is_open() ){
112  errorLog << "save(fstream &file) - The file is not open!" << std::endl;
113  return false;
114  }
115 
116  file << "GRT_LEAKY_INTEGRATOR_FILE_V1.0" << std::endl;
117 
119  errorLog << "savePreProcessingSettingsToFile(fstream &file) - Failed to save preprocessing settings to file!" << std::endl;
120  return false;
121  }
122 
123  file << "LeakRate: " << leakRate << std::endl;
124 
125  return true;
126 }
127 
128 bool LeakyIntegrator::load( std::fstream &file ){
129 
130  if( !file.is_open() ){
131  errorLog << "load(fstream &file) - The file is not open!" << std::endl;
132  return false;
133  }
134 
135  std::string word;
136 
137  //Load the header
138  file >> word;
139 
140  if( word != "GRT_LEAKY_INTEGRATOR_FILE_V1.0" ){
141  errorLog << "load(fstream &file) - Invalid file format!" << std::endl;
142  return false;
143  }
144 
146  errorLog << "loadPreProcessingSettingsFromFile(fstream &file) - Failed to load preprocessing settings from file!" << std::endl;
147  return false;
148  }
149 
150  //Load the LeakRate
151  file >> word;
152  if( word != "LeakRate:" ){
153  errorLog << "load(fstream &file) - Failed to read LeakRate header!" << std::endl;
154  return false;
155  }
156  file >> leakRate;
157 
158  //Init the LeakyIntegrator module to ensure everything is initialized correctly
159  return init(leakRate,numInputDimensions);
160 }
161 
162 bool LeakyIntegrator::init(const Float leakRate,const UINT numDimensions){
163 
164  initialized = false;
165 
166  if( leakRate < 0 || leakRate > 1 ){
167  errorLog << "init(const Float leakRate,const UINT numDimensions) - leakRate must be between [0 1]!" << std::endl;
168  return false;
169  }
170 
171  if( numDimensions == 0 ){
172  errorLog << "init(const Float leakRate,const UINT numDimensions) - NumDimensions must be greater than 0!" << std::endl;
173  return false;
174  }
175 
176  this->leakRate = leakRate;
177  this->numInputDimensions = numDimensions;
178  this->numOutputDimensions = numDimensions;
179  y.clear();
180  y.resize(numDimensions,0);
181  processedData.clear();
182  processedData.resize(numDimensions,0);
183  initialized = true;
184  return true;
185 }
186 
187 Float LeakyIntegrator::update(const Float x){
188 
189  if( numInputDimensions != 1 ){
190  errorLog << "update(const Float x) - The Number Of Input Dimensions is not 1! NumInputDimensions: " << numInputDimensions << std::endl;
191  return 0;
192  }
193 
194  y = update( VectorFloat(1,x) );
195 
196  if( y.size() == 0 ) return 0 ;
197 
198  return y[0];
199 }
200 
202 
203  if( !initialized ){
204  errorLog << "update(const VectorFloat &x) - Not Initialized!" << std::endl;
205  return VectorFloat();
206  }
207 
208  if( x.size() != numInputDimensions ){
209  errorLog << "update(const VectorFloat &x) - The Number Of Input Dimensions (" << numInputDimensions << ") does not match the size of the input vector (" << x.size() << ")!" << std::endl;
210  return VectorFloat();
211  }
212 
213  for(UINT n=0; n<numInputDimensions; n++){
214  y[n] = y[n]*leakRate + x[n];
215  }
216  processedData = y;
217 
218  return processedData;
219 }
220 
221 bool LeakyIntegrator::setLeakRate(const Float leakRate){
222  if( leakRate >= 0 && leakRate <= 1 ){
223  this->leakRate = leakRate;
224  if( initialized ) init(leakRate, numInputDimensions);
225  return true;
226  }
227  errorLog << "setLeakRate(const Float leakRate) - The leak rate must be between [0 1]!" << std::endl;
228  return false;
229 }
230 
232  return leakRate;
233 }
234 
235 GRT_END_NAMESPACE
virtual bool reset()
LeakyIntegrator & operator=(const LeakyIntegrator &rhs)
virtual ~LeakyIntegrator()
bool savePreProcessingSettingsToFile(std::fstream &file) const
bool loadPreProcessingSettingsFromFile(std::fstream &file)
virtual bool save(std::fstream &file) const
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
Float update(const Float x)
LeakyIntegrator(const Float leakRate=0.99, const UINT numDimensions=1)
VectorFloat y
A buffer holding the previous input value(s)
virtual bool deepCopyFrom(const PreProcessing *preProcessing)
bool setLeakRate(const Float leakRate)
std::string getPreProcessingType() const
bool copyBaseVariables(const PreProcessing *preProcessingModule)
The LeakyIntegrator class computes the following signal: y = y*z + x, where x is the input...
virtual bool process(const VectorFloat &inputVector)
virtual bool load(std::fstream &file)
Float leakRate
The current leak rate.