GestureRecognitionToolkit  Version: 1.0 Revision: 04-03-15
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 namespace GRT{
24 
25 //Register the HighPassFilter module with the PreProcessing base class
26 RegisterPreProcessingModule< HighPassFilter > HighPassFilter::registerModule("HighPassFilter");
27 
28 HighPassFilter::HighPassFilter(double filterFactor,double gain,UINT numDimensions,double cutoffFrequency,double 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!" << endl;
91 
92  return false;
93 }
94 
95 bool HighPassFilter::process(const VectorDouble &inputVector){
96 
97  if( !initialized ){
98  errorLog << "process(const VectorDouble &inputVector) - Not initialized!" << endl;
99  return false;
100  }
101 
102  if( inputVector.size() != numInputDimensions ){
103  errorLog << "process(const VectorDouble &inputVector) - The size of the inputVector (" << inputVector.size() << ") does not match that of the filter (" << numInputDimensions << ")!" << 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(string filename) const{
120 
121  if( !initialized ){
122  errorLog << "saveModelToFile(string filename) - The HighPassFilter has not been initialized" << 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(fstream &file) const{
140 
141  if( !file.is_open() ){
142  errorLog << "saveModelToFile(fstream &file) - The file is not open!" << endl;
143  return false;
144  }
145 
146  file << "GRT_HIGH_PASS_FILTER_FILE_V1.0" << endl;
147 
148  file << "NumInputDimensions: " << numInputDimensions << endl;
149  file << "NumOutputDimensions: " << numOutputDimensions << endl;
150  file << "FilterFactor: " << filterFactor << endl;
151  file << "Gain: " << gain << endl;
152 
153  return true;
154 }
155 
156 bool HighPassFilter::loadModelFromFile(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 
173 
174  if( !file.is_open() ){
175  errorLog << "loadModelFromFile(fstream &file) - The file is not open!" << endl;
176  return false;
177  }
178 
179  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!" << 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!" << 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!" << 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!" << 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!" << 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(double filterFactor,double gain,UINT numDimensions){
226 
227  initialized = false;
228 
229  if( numDimensions == 0 ){
230  errorLog << "init(double filterFactor,double gain,UINT numDimensions) - NumDimensions must be greater than 0!" << endl;
231  return false;
232  }
233 
234  if( filterFactor <= 0 ){
235  errorLog << "init(double filterFactor,double gain,UINT numDimensions) - FilterFactor must be greater than 0!" << endl;
236  return false;
237  }
238 
239  if( gain <= 0 ){
240  errorLog << "init(double filterFactor,double gain,UINT numDimensions) - Gain must be greater than 0!" << 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 double HighPassFilter::filter(const double 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 double x) - The filter has not been initialized!" << endl;
264  return 0;
265  }
266 
267  VectorDouble y = filter(VectorDouble(1,x));
268 
269  if( y.size() == 0 ) return 0;
270  return y[0];
271 
272 }
273 
274 VectorDouble HighPassFilter::filter(const VectorDouble &x){
275 
276  if( !initialized ){
277  errorLog << "filter(const VectorDouble &x) - Not Initialized!" << endl;
278  return VectorDouble();
279  }
280 
281  if( x.size() != numInputDimensions ){
282  errorLog << "filter(const VectorDouble &x) - The Number Of Input Dimensions (" << numInputDimensions << ") does not match the size of the input vector (" << x.size() << ")!" << endl;
283  return VectorDouble();
284  }
285 
286  for(UINT n=0; n<numInputDimensions; n++){
287  //Compute the new output
288  processedData[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
294  yy[n] = processedData[n];
295  }
296  return processedData;
297 }
298 
299 bool HighPassFilter::setGain(double gain){
300  if( gain > 0 ){
301  this->gain = gain;
302  return true;
303  }
304  errorLog << "setGain(double gain) - Gain value must be greater than 0!" << endl;
305  return false;
306 }
307 
308 bool HighPassFilter::setFilterFactor(double filterFactor){
309  if( filterFactor > 0 ){
310  this->filterFactor = filterFactor;
311  return true;
312  }
313  errorLog << "setFilterFactor(double filterFactor) - FilterFactor value must be greater than 0!" << endl;
314  return false;
315 }
316 
317 bool HighPassFilter::setCutoffFrequency(double cutoffFrequency,double delta){
318  if( cutoffFrequency > 0 && delta > 0 ){
319  double RC = (1.0/TWO_PI) / cutoffFrequency;
320  filterFactor = RC / (RC+delta);
321  return true;
322  }
323  return false;
324 }
325 
326 }//End of namespace GRT
double filterFactor
The filter factor (alpha) of the filter.
virtual bool process(const VectorDouble &inputVector)
virtual bool loadModelFromFile(string filename)
VectorDouble yy
The previous output value(s)
HighPassFilter & operator=(const HighPassFilter &rhs)
Definition: AdaBoost.cpp:25
double filter(const double x)
bool setGain(double gain)
bool setCutoffFrequency(double cutoffFrequency, double delta)
string getPreProcessingType() const
HighPassFilter(double filterFactor=0.1, double gain=1, UINT numDimensions=1, double cutoffFrequency=-1, double delta=-1)
This class implements a High Pass Filter.
bool setFilterFactor(double filterFactor)
virtual bool saveModelToFile(string filename) const
bool copyBaseVariables(const PreProcessing *preProcessingModule)
virtual bool deepCopyFrom(const PreProcessing *preProcessing)
virtual bool reset()
VectorDouble xx
The previous input value(s)
double gain
The gain factor of the filter.