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