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.
HighPassFilter.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 "HighPassFilter.h"
22 
23 GRT_BEGIN_NAMESPACE
24 
25 //Register the HighPassFilter module with the PreProcessing base class
26 RegisterPreProcessingModule< HighPassFilter > HighPassFilter::registerModule("HighPassFilter");
27 
28 HighPassFilter::HighPassFilter(Float filterFactor,Float gain,UINT numDimensions,Float cutoffFrequency,Float delta){
29 
30  classType = "HighPassFilter";
31  preProcessingType = classType;
32  debugLog.setProceedingText("[DEBUG HighPassFilter]");
33  errorLog.setProceedingText("[ERROR HighPassFilter]");
34  warningLog.setProceedingText("[WARNING HighPassFilter]");
35  init(filterFactor,gain,numDimensions);
36 
37  if( cutoffFrequency != -1 && delta != -1 ){
38  setCutoffFrequency(cutoffFrequency, delta);
39  }
40 }
41 
43 
44  preProcessingType = classType;
45  preProcessingType = "HighPassFilter";
46  debugLog.setProceedingText("[DEBUG HighPassFilter]");
47  errorLog.setProceedingText("[ERROR HighPassFilter]");
48  warningLog.setProceedingText("[WARNING HighPassFilter]");
49 
50  this->filterFactor = rhs.filterFactor;
51  this->gain = rhs.gain;
52  this->xx = rhs.xx;
53  this->yy = rhs.yy;
55 }
56 
58 
59 }
60 
62  if(this!=&rhs){
63  this->filterFactor = rhs.filterFactor;
64  this->gain = rhs.gain;
65  this->xx = rhs.xx;
66  this->yy = rhs.yy;
68  }
69  return *this;
70 }
71 
72 bool HighPassFilter::deepCopyFrom(const PreProcessing *preProcessing){
73 
74  if( preProcessing == NULL ) return false;
75 
76  if( this->getPreProcessingType() == preProcessing->getPreProcessingType() ){
77 
78  HighPassFilter *ptr = (HighPassFilter*)preProcessing;
79 
80  //Clone the HighPassFilter values
81  this->filterFactor = ptr->filterFactor;
82  this->gain = ptr->gain;
83  this->xx = ptr->xx;
84  this->yy = ptr->yy;
85 
86  //Clone the base class variables
87  return copyBaseVariables( preProcessing );
88  }
89 
90  errorLog << "clone(const PreProcessing *preProcessing) - PreProcessing Types Do Not Match!" << std::endl;
91 
92  return false;
93 }
94 
95 bool HighPassFilter::process(const VectorFloat &inputVector){
96 
97  if( !initialized ){
98  errorLog << "process(const VectorFloat &inputVector) - Not initialized!" << std::endl;
99  return false;
100  }
101 
102  if( inputVector.size() != numInputDimensions ){
103  errorLog << "process(const VectorFloat &inputVector) - The size of the inputVector (" << inputVector.size() << ") does not match that of the filter (" << numInputDimensions << ")!" << std::endl;
104  return false;
105  }
106 
107  processedData = filter( inputVector );
108 
109  if( processedData.size() == numOutputDimensions ) return true;
110  return false;
111 
112 }
113 
115  if( initialized ) return init(filterFactor,gain,numInputDimensions);
116  return false;
117 }
118 
119 bool HighPassFilter::saveModelToFile( std::string filename ) const{
120 
121  if( !initialized ){
122  errorLog << "saveModelToFile(string filename) - The HighPassFilter has not been initialized" << std::endl;
123  return false;
124  }
125 
126  std::fstream file;
127  file.open(filename.c_str(), std::ios::out);
128 
129  if( !saveModelToFile( file ) ){
130  file.close();
131  return false;
132  }
133 
134  file.close();
135 
136  return true;
137 }
138 
139 bool HighPassFilter::saveModelToFile( std::fstream &file ) const{
140 
141  if( !file.is_open() ){
142  errorLog << "saveModelToFile(fstream &file) - The file is not open!" << std::endl;
143  return false;
144  }
145 
146  file << "GRT_HIGH_PASS_FILTER_FILE_V1.0" << std::endl;
147 
148  file << "NumInputDimensions: " << numInputDimensions << std::endl;
149  file << "NumOutputDimensions: " << numOutputDimensions << std::endl;
150  file << "FilterFactor: " << filterFactor << std::endl;
151  file << "Gain: " << gain << std::endl;
152 
153  return true;
154 }
155 
156 bool HighPassFilter::loadModelFromFile( std::string filename ){
157 
158  std::fstream file;
159  file.open(filename.c_str(), std::ios::in);
160 
161  if( !loadModelFromFile( file ) ){
162  file.close();
163  initialized = false;
164  return false;
165  }
166 
167  file.close();
168 
169  return true;
170 }
171 
172 bool HighPassFilter::loadModelFromFile( std::fstream &file ){
173 
174  if( !file.is_open() ){
175  errorLog << "loadModelFromFile(fstream &file) - The file is not open!" << std::endl;
176  return false;
177  }
178 
179  std::string word;
180 
181  //Load the header
182  file >> word;
183 
184  if( word != "GRT_HIGH_PASS_FILTER_FILE_V1.0" ){
185  errorLog << "loadModelFromFile(fstream &file) - Invalid file format!" << std::endl;
186  return false;
187  }
188 
189  //Load the number of input dimensions
190  file >> word;
191  if( word != "NumInputDimensions:" ){
192  errorLog << "loadModelFromFile(fstream &file) - Failed to read NumInputDimensions header!" << std::endl;
193  return false;
194  }
195  file >> numInputDimensions;
196 
197  //Load the number of output dimensions
198  file >> word;
199  if( word != "NumOutputDimensions:" ){
200  errorLog << "loadModelFromFile(fstream &file) - Failed to read NumOutputDimensions header!" << std::endl;
201  return false;
202  }
203  file >> numOutputDimensions;
204 
205  //Load the filter factor
206  file >> word;
207  if( word != "FilterFactor:" ){
208  errorLog << "loadModelFromFile(fstream &file) - Failed to read FilterFactor header!" << std::endl;
209  return false;
210  }
211  file >> filterFactor;
212 
213  //Load the number of output dimensions
214  file >> word;
215  if( word != "Gain:" ){
216  errorLog << "loadModelFromFile(fstream &file) - Failed to read Gain header!" << std::endl;
217  return false;
218  }
219  file >> gain;
220 
221  //Init the filter module to ensure everything is initialized correctly
222  return init(filterFactor,gain,numInputDimensions);
223 }
224 
225 bool HighPassFilter::init(Float filterFactor,Float gain,UINT numDimensions){
226 
227  initialized = false;
228 
229  if( numDimensions == 0 ){
230  errorLog << "init(Float filterFactor,Float gain,UINT numDimensions) - NumDimensions must be greater than 0!" << std::endl;
231  return false;
232  }
233 
234  if( filterFactor <= 0 ){
235  errorLog << "init(Float filterFactor,Float gain,UINT numDimensions) - FilterFactor must be greater than 0!" << std::endl;
236  return false;
237  }
238 
239  if( gain <= 0 ){
240  errorLog << "init(Float filterFactor,Float gain,UINT numDimensions) - Gain must be greater than 0!" << std::endl;
241  return false;
242  }
243 
244  this->filterFactor = filterFactor;
245  this->gain = gain;
246  this->numInputDimensions = numDimensions;
247  this->numOutputDimensions = numDimensions;
248  xx.clear();
249  xx.resize(numDimensions,0);
250  yy.clear();
251  yy.resize(numDimensions,0);
252  processedData.clear();
253  processedData.resize(numDimensions,0);
254  initialized = true;
255 
256  return true;
257 }
258 
259 Float HighPassFilter::filter(const Float x){
260 
261  //If the filter has not been initialised then return 0, otherwise filter x and return y
262  if( !initialized ){
263  errorLog << "filter(const Float x) - The filter has not been initialized!" << std::endl;
264  return 0;
265  }
266 
267  VectorFloat y = filter(VectorFloat(1,x));
268 
269  if( y.size() == 0 ) return 0;
270  return y[0];
271 
272 }
273 
275 
276  if( !initialized ){
277  errorLog << "filter(const VectorFloat &x) - Not Initialized!" << std::endl;
278  return VectorFloat();
279  }
280 
281  if( x.size() != numInputDimensions ){
282  errorLog << "filter(const VectorFloat &x) - The Number Of Input Dimensions (" << numInputDimensions << ") does not match the size of the input vector (" << x.size() << ")!" << std::endl;
283  return VectorFloat();
284  }
285 
286  for(UINT n=0; n<numInputDimensions; n++){
287  //Compute the new output
288  yy[n] = filterFactor * (yy[n] + x[n] - xx[n]) * gain;
289 
290  //Store the current input
291  xx[n] = x[n];
292 
293  //Store the current output in processed data so it can be accessed by the base class
294  processedData[n] = processedData[n];
295  }
296  return processedData;
297 }
298 
299 bool HighPassFilter::setGain(Float gain){
300  if( gain > 0 ){
301  this->gain = gain;
302  return true;
303  }
304  errorLog << "setGain(Float gain) - Gain value must be greater than 0!" << std::endl;
305  return false;
306 }
307 
308 bool HighPassFilter::setFilterFactor(Float filterFactor){
309  if( filterFactor > 0 ){
310  this->filterFactor = filterFactor;
311  return true;
312  }
313  errorLog << "setFilterFactor(Float filterFactor) - FilterFactor value must be greater than 0!" << std::endl;
314  return false;
315 }
316 
317 bool HighPassFilter::setCutoffFrequency(Float cutoffFrequency,Float delta){
318  if( cutoffFrequency > 0 && delta > 0 ){
319  Float RC = (1.0/TWO_PI) / cutoffFrequency;
320  filterFactor = RC / (RC+delta);
321  return true;
322  }
323  return false;
324 }
325 
326 GRT_END_NAMESPACE
virtual bool deepCopyFrom(const PreProcessing *preProcessing)
bool setFilterFactor(Float filterFactor)
Float filterFactor
The filter factor (alpha) of the filter.
virtual bool loadModelFromFile(std::string filename)
HighPassFilter(Float filterFactor=0.1, Float gain=1, UINT numDimensions=1, Float cutoffFrequency=-1, Float delta=-1)
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
VectorFloat yy
The previous output value(s)
HighPassFilter & operator=(const HighPassFilter &rhs)
virtual bool process(const VectorFloat &inputVector)
Float gain
The gain factor of the filter.
This class implements a High Pass Filter.
virtual ~HighPassFilter()
std::string getPreProcessingType() const
VectorFloat xx
The previous input value(s)
bool setGain(Float gain)
bool copyBaseVariables(const PreProcessing *preProcessingModule)
Float filter(const Float x)
virtual bool reset()
virtual bool saveModelToFile(std::string filename) const
bool setCutoffFrequency(Float cutoffFrequency, Float delta)