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