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