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