21 #define GRT_DLL_EXPORTS 27 const std::string SavitzkyGolayFilter::id =
"SavitzkyGolayFilter";
35 init(numLeftHandPoints,numRightHandPoints,derivativeOrder,smoothingPolynomialOrder,numDimensions);
41 this->numPoints = rhs.numPoints;
42 this->numLeftHandPoints = rhs.numLeftHandPoints;
43 this->numRightHandPoints = rhs.numRightHandPoints;
44 this->derivativeOrder = rhs.derivativeOrder;
45 this->smoothingPolynomialOrder = rhs.smoothingPolynomialOrder;
46 this->data = rhs.data;
48 this->coeff = rhs.coeff;
59 this->numPoints = rhs.numPoints;
60 this->numLeftHandPoints = rhs.numLeftHandPoints;
61 this->numRightHandPoints = rhs.numRightHandPoints;
62 this->derivativeOrder = rhs.derivativeOrder;
63 this->smoothingPolynomialOrder = rhs.smoothingPolynomialOrder;
64 this->data = rhs.data;
66 this->coeff = rhs.coeff;
74 if( preProcessing == NULL )
return false;
81 this->numPoints = ptr->numPoints;
82 this->numLeftHandPoints = ptr->numLeftHandPoints;
83 this->numRightHandPoints = ptr->numRightHandPoints;
84 this->derivativeOrder = ptr->derivativeOrder;
85 this->smoothingPolynomialOrder = ptr->smoothingPolynomialOrder;
86 this->data = ptr->data;
88 this->coeff = ptr->coeff;
94 errorLog <<
"deepCopyFrom(PreProcessing *preProcessing) - PreProcessing Types Do Not Match!" << std::endl;
102 errorLog <<
"process(const VectorFloat &inputVector) - Not initialized!" << std::endl;
106 if( inputVector.size() != numInputDimensions ){
107 errorLog <<
"process(const VectorFloat &inputVector) - The size of the inputVector (" << inputVector.size() <<
") does not match that of the filter (" << numInputDimensions <<
")!" << std::endl;
111 processedData =
filter( inputVector );
113 if( processedData.size() == numOutputDimensions )
return true;
122 yy.
resize(numInputDimensions,0);
123 processedData.clear();
124 processedData.
resize(numInputDimensions,0);
132 if( !file.is_open() ){
133 errorLog <<
"save(std::fstream &file) - The file is not open!" << std::endl;
137 file <<
"GRT_SAVITZKY_GOLAY_FILTER_FILE_V1.0" << std::endl;
139 file <<
"NumInputDimensions: " << numInputDimensions << std::endl;
140 file <<
"NumOutputDimensions: " << numOutputDimensions << std::endl;
141 file <<
"NumPoints: " << numPoints << std::endl;
142 file <<
"NumLeftHandPoints: " << numLeftHandPoints << std::endl;
143 file <<
"NumRightHandPoints: " << numRightHandPoints << std::endl;
144 file <<
"DerivativeOrder: " << derivativeOrder << std::endl;
145 file <<
"SmoothingPolynomialOrder: " << smoothingPolynomialOrder << std::endl;
152 if( !file.is_open() ){
153 errorLog <<
"load(std::fstream &file) - The file is not open!" << std::endl;
162 if( word !=
"GRT_SAVITZKY_GOLAY_FILTER_FILE_V1.0" ){
163 errorLog <<
"load(std::fstream &file) - Invalid file format!" << std::endl;
169 if( word !=
"NumInputDimensions:" ){
170 errorLog <<
"load(std::fstream &file) - Failed to read NumInputDimensions header!" << std::endl;
173 file >> numInputDimensions;
177 if( word !=
"NumOutputDimensions:" ){
178 errorLog <<
"load(std::fstream &file) - Failed to read NumOutputDimensions header!" << std::endl;
181 file >> numOutputDimensions;
185 if( word !=
"NumPoints:" ){
186 errorLog <<
"load(std::fstream &file) - Failed to read NumPoints header!" << std::endl;
193 if( word !=
"NumLeftHandPoints:" ){
194 errorLog <<
"load(std::fstream &file) - Failed to read NumLeftHandPoints header!" << std::endl;
197 file >> numLeftHandPoints;
201 if( word !=
"NumRightHandPoints:" ){
202 errorLog <<
"load(std::fstream &file) - Failed to read numRightHandPoints header!" << std::endl;
205 file >> numRightHandPoints;
209 if( word !=
"DerivativeOrder:" ){
210 errorLog <<
"load(std::fstream &file) - Failed to read DerivativeOrder header!" << std::endl;
213 file >> derivativeOrder;
217 if( word !=
"SmoothingPolynomialOrder:" ){
218 errorLog <<
"load(std::fstream &file) - Failed to read SmoothingPolynomialOrder header!" << std::endl;
221 file >> smoothingPolynomialOrder;
224 return init(numLeftHandPoints,numRightHandPoints,derivativeOrder,smoothingPolynomialOrder,numInputDimensions);
227 bool SavitzkyGolayFilter::init(UINT numLeftHandPoints,UINT numRightHandPoints,UINT derivativeOrder,UINT smoothingPolynomialOrder,UINT numDimensions){
231 if( numDimensions == 0 ){
232 errorLog <<
"init(Float filterFactor,Float gain,UINT numDimensions) - NumDimensions must be greater than 0!" << std::endl;
236 this->numPoints = numLeftHandPoints+numRightHandPoints+1;
237 this->numLeftHandPoints = numLeftHandPoints;
238 this->numRightHandPoints = numRightHandPoints;
239 this->derivativeOrder = derivativeOrder;
240 this->smoothingPolynomialOrder = smoothingPolynomialOrder;
242 this->numInputDimensions = numDimensions;
243 this->numOutputDimensions = numDimensions;
245 yy.
resize(numDimensions,0);
246 processedData.clear();
247 processedData.
resize(numDimensions,0);
251 errorLog <<
"init(UINT NL,UINT NR,UINT LD,UINT M,UINT numDimensions) - Failed to compute filter coefficents!" << std::endl;
264 errorLog <<
"filter(Float x) - The filter has not been initialized!" << std::endl;
270 if( y.size() > 0 )
return y[0];
277 errorLog <<
"filter(const VectorFloat &x) - Not Initialized!" << std::endl;
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;
290 for(UINT j=0; j<x.size(); j++){
291 processedData[j] = 0;
292 for(UINT i=0; i<numPoints; i++)
293 processedData[j] += data[i][j] * coeff[i];
296 return processedData;
299 bool SavitzkyGolayFilter::calCoeff(){
301 int np = (int)numPoints;
302 int nl = (int)numLeftHandPoints;
303 int nr = (int)numRightHandPoints;
304 int ld = (int)derivativeOrder;
305 int m = (int)smoothingPolynomialOrder;
306 int i,j,k,imj,ipj,kk,mm,pos;
313 for (ipj=0; ipj<=(m << 1); ipj++) {
314 sum=(ipj ? 0.0 : 1.0);
316 for (k=1; k<=nr; k++) sum += pow(Float(k),Float(ipj));
317 for (k=1; k<=nl; k++) sum += pow(Float(-k),Float(ipj));
319 mm = min_(ipj,2*m-ipj);
321 for (imj = -mm; imj<=mm; imj+=2) a[(ipj+imj)/2][(ipj-imj)/2] = sum;
325 for (j=0;j<m+1;j++) b[j]=0.0;
327 if( !alud.solve_vector(b,b) ){
331 for (kk=0; kk<np; kk++) c[kk]=0.0;
332 for (k = -nl; k<=nr; k++) {
336 for(mm=1; mm<=m; mm++)
337 sum += b[mm]*(fac *= k);
bool push_back(const T &value)
std::string getId() const
Float filter(const Float x)
virtual ~SavitzkyGolayFilter()
virtual bool resize(const unsigned int size)
VectorFloat getFilteredData() const
virtual bool load(std::fstream &file)
This implements a Savitzky-Golay filter. This code is based on the Savitzky Golay filter code from Nu...
virtual bool deepCopyFrom(const PreProcessing *preProcessing)
SavitzkyGolayFilter & operator=(const SavitzkyGolayFilter &rhs)
bool copyBaseVariables(const PreProcessing *preProcessingModule)
bool setAllValues(const T &value)
static std::string getId()
SavitzkyGolayFilter(const UINT numLeftHandPoints=10, const UINT numRightHandPoints=10, const UINT derivativeOrder=0, const UINT smoothingPolynomialOrder=2, const UINT numDimensions=1)
virtual bool process(const VectorFloat &inputVector)
virtual bool save(std::fstream &file) const
bool resize(const unsigned int newBufferSize)