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.
TimeseriesBuffer.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 "TimeseriesBuffer.h"
23 
24 GRT_BEGIN_NAMESPACE
25 
26 //Define the string that will be used to identify the object
27 std::string TimeseriesBuffer::id = "TimeseriesBuffer";
28 std::string TimeseriesBuffer::getId() { return TimeseriesBuffer::id; }
29 
30 //Register the TimeseriesBuffer module with the FeatureExtraction base class
32 
33 TimeseriesBuffer::TimeseriesBuffer(const UINT bufferSize,const UINT numDimensions) : FeatureExtraction( TimeseriesBuffer::getId() )
34 {
35  init(bufferSize,numDimensions);
36 }
37 
39 {
40  //Invoke the equals operator to copy the data from the rhs instance to this instance
41  *this = rhs;
42 }
43 
45 
46 }
47 
49  if(this!=&rhs){
50  this->bufferSize = rhs.bufferSize;
51  this->dataBuffer = rhs.dataBuffer;
52 
54  }
55  return *this;
56 }
57 
58 bool TimeseriesBuffer::deepCopyFrom(const FeatureExtraction *featureExtraction){
59 
60  if( featureExtraction == NULL ) return false;
61 
62  if( this->getId() == featureExtraction->getId() ){
63 
64  //Invoke the equals operator to copy the data from the rhs instance to this instance
65  *this = *dynamic_cast<const TimeseriesBuffer*>(featureExtraction);
66 
67  return true;
68  }
69 
70  errorLog << "deepCopyFrom(FeatureExtraction *featureExtraction) - FeatureExtraction Types Do Not Match!" << std::endl;
71 
72  return false;
73 }
74 
76 
77  if( !initialized ){
78  errorLog << "computeFeatures(const VectorFloat &inputVector) - Not initialized!" << std::endl;
79  return false;
80  }
81 
82  if( inputVector.size() != numInputDimensions ){
83  errorLog << "computeFeatures(const VectorFloat &inputVector) - The size of the inputVector (" << inputVector.size() << ") does not match that of the filter (" << numInputDimensions << ")!" << std::endl;
84  return false;
85  }
86 
87  update( inputVector );
88 
89  return true;
90 }
91 
93  if( initialized ){
94  return init( bufferSize, numInputDimensions );
95  }
96  return false;
97 }
98 
99 bool TimeseriesBuffer::save( std::fstream &file ) const{
100 
101  if( !file.is_open() ){
102  errorLog << "save(fstream &file) - The file is not open!" << std::endl;
103  return false;
104  }
105 
106  //Write the file header
107  file << "GRT_TIMESERIES_BUFFER_FILE_V1.0" << std::endl;
108 
109  //Save the base settings to the file
111  errorLog << "saveFeatureExtractionSettingsToFile(fstream &file) - Failed to save base feature extraction settings to file!" << std::endl;
112  return false;
113  }
114 
115  //Write the zero crossing counter settings
116  file << "BufferSize: " << dataBuffer.getSize() << std::endl;
117 
118  return true;
119 }
120 
121 bool TimeseriesBuffer::load( std::fstream &file ){
122 
123  if( !file.is_open() ){
124  errorLog << "load(fstream &file) - The file is not open!" << std::endl;
125  return false;
126  }
127 
128  std::string word;
129 
130  //Load the header
131  file >> word;
132 
133  if( word != "GRT_TIMESERIES_BUFFER_FILE_V1.0" ){
134  errorLog << "load(fstream &file) - Invalid file format!" << std::endl;
135  return false;
136  }
137 
139  errorLog << "loadFeatureExtractionSettingsFromFile(fstream &file) - Failed to load base feature extraction settings from file!" << std::endl;
140  return false;
141  }
142 
143  file >> word;
144  if( word != "BufferSize:" ){
145  errorLog << "load(fstream &file) - Failed to read BufferSize header!" << std::endl;
146  return false;
147  }
148  file >> bufferSize;
149 
150  //Init the TimeseriesBuffer module to ensure everything is initialized correctly
151  return init(bufferSize,numInputDimensions);
152 }
153 
154 bool TimeseriesBuffer::init(const UINT bufferSize,const UINT numDimensions){
155 
156  initialized = false;
157  featureDataReady = false;
158 
159  if( bufferSize == 0 ){
160  errorLog << "init(UINT bufferSize,UINT numDimensions) - The bufferSize must be greater than zero!" << std::endl;
161  return false;
162  }
163 
164  if( numDimensions == 0 ){
165  errorLog << "init(UINT bufferSize,UINT numDimensions) - The numDimensions must be greater than zero!" << std::endl;
166  return false;
167  }
168 
169  //Setup the databuffer
170  numInputDimensions = numDimensions;
171  numOutputDimensions = bufferSize * numInputDimensions;
172  this->bufferSize = bufferSize;
173  dataBuffer.resize( bufferSize, VectorFloat(numInputDimensions,0) );
174  featureVector.resize(numOutputDimensions,0);
175 
176  //Flag that the timeseries buffer has been initialized
177  initialized = true;
178 
179  return true;
180 }
181 
182 
184  return update(VectorFloat(1,x));
185 }
186 
188 
189  if( !initialized ){
190  errorLog << "update(const VectorFloat &x) - Not Initialized!" << std::endl;
191  return VectorFloat();
192  }
193 
194  if( x.getSize() != numInputDimensions ){
195  errorLog << "update(const VectorFloat &x)- The Number Of Input Dimensions (" << numInputDimensions << ") does not match the size of the input vector (" << x.getSize() << ")!" << std::endl;
196  return VectorFloat();
197  }
198 
199  //Add the new data to the buffer
200  dataBuffer.push_back( x );
201 
202  //Search the buffer for the zero crossing features
203  UINT colIndex = 0;
204  for(UINT j=0; j<numInputDimensions; j++){
205  for(UINT i=0; i<dataBuffer.getSize(); i++){
206  featureVector[ colIndex++ ] = dataBuffer[i][j];
207  }
208  }
209 
210  //Flag that the feature data has been computed
211  if( dataBuffer.getBufferFilled() ){
212  featureDataReady = true;
213  }else featureDataReady = false;
214 
215  return featureVector;
216 }
217 
218 bool TimeseriesBuffer::setBufferSize(UINT bufferSize){
219  if( bufferSize > 0 ){
220  this->bufferSize = bufferSize;
221  if( initialized ) return init(bufferSize, numInputDimensions);
222  return true;
223  }
224  errorLog << "setBufferSize(UINT bufferSize) - The bufferSize must be larger than zero!" << std::endl;
225  return false;
226 }
227 
229  if( initialized ) return bufferSize;
230  return 0;
231 }
232 
234  if( initialized ) return dataBuffer.getData();
235  return Vector< VectorFloat >();
236 }
237 
238 GRT_END_NAMESPACE
bool push_back(const T &value)
TimeseriesBuffer & operator=(const TimeseriesBuffer &rhs)
std::string getId() const
Definition: GRTBase.cpp:85
virtual bool reset()
bool setBufferSize(const UINT bufferSize)
virtual bool load(std::fstream &file)
bool saveFeatureExtractionSettingsToFile(std::fstream &file) const
bool getBufferFilled() const
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
UINT getSize() const
Definition: Vector.h:201
virtual bool save(std::fstream &file) const
VectorFloat update(const Float x)
TimeseriesBuffer(const UINT bufferSize=5, const UINT numDimensions=1)
static std::string getId()
virtual ~TimeseriesBuffer()
bool loadFeatureExtractionSettingsFromFile(std::fstream &file)
virtual bool deepCopyFrom(const FeatureExtraction *featureExtraction)
UINT getBufferSize() const
bool copyBaseVariables(const FeatureExtraction *featureExtractionModule)
virtual bool computeFeatures(const VectorFloat &inputVector)
Vector< T > getData(const bool rawBuffer=false) const
Vector< VectorFloat > getDataBuffer() const
This class implements the TimeseriesBuffer feature extraction module.
unsigned int getSize() const
CircularBuffer< VectorFloat > dataBuffer
A buffer used to store the timeseries data.
bool resize(const unsigned int newBufferSize)