2013-08-29 02:34:34 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2012-07-04 19:07:57 -03:00
|
|
|
|
2013-08-29 02:34:34 -03:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//
|
2012-07-04 19:07:57 -03:00
|
|
|
/// @file Derivative.h
|
|
|
|
/// @brief A class to implement a derivative (slope) filter
|
|
|
|
/// See http://www.holoborodko.com/pavel/numerical-methods/numerical-derivative/smooth-low-noise-differentiators/
|
2016-02-17 21:25:53 -04:00
|
|
|
#pragma once
|
2012-07-04 19:07:57 -03:00
|
|
|
|
2012-10-09 21:07:25 -03:00
|
|
|
#include "FilterClass.h"
|
|
|
|
#include "FilterWithBuffer.h"
|
2012-07-04 19:07:57 -03:00
|
|
|
|
2012-08-17 03:22:10 -03:00
|
|
|
// 1st parameter <T> is the type of data being filtered.
|
2012-07-05 00:00:08 -03:00
|
|
|
// 2nd parameter <FILTER_SIZE> is the number of elements in the filter
|
|
|
|
template <class T, uint8_t FILTER_SIZE>
|
2012-07-04 19:07:57 -03:00
|
|
|
class DerivativeFilter : public FilterWithBuffer<T,FILTER_SIZE>
|
|
|
|
{
|
2012-08-17 03:22:10 -03:00
|
|
|
public:
|
|
|
|
// constructor
|
|
|
|
DerivativeFilter() : FilterWithBuffer<T,FILTER_SIZE>() {
|
|
|
|
};
|
2012-07-04 19:07:57 -03:00
|
|
|
|
2012-08-17 03:22:10 -03:00
|
|
|
// update - Add a new raw value to the filter, but don't recalculate
|
2014-02-10 07:38:59 -04:00
|
|
|
void update(T sample, uint32_t timestamp);
|
2012-07-06 02:04:54 -03:00
|
|
|
|
2012-08-17 03:22:10 -03:00
|
|
|
// return the derivative value
|
2014-02-10 07:38:59 -04:00
|
|
|
float slope(void);
|
2012-07-04 19:07:57 -03:00
|
|
|
|
2012-08-17 03:22:10 -03:00
|
|
|
// reset - clear the filter
|
2018-11-07 07:26:57 -04:00
|
|
|
virtual void reset() override;
|
2012-07-04 19:07:57 -03:00
|
|
|
|
2012-08-17 03:22:10 -03:00
|
|
|
private:
|
|
|
|
bool _new_data;
|
|
|
|
float _last_slope;
|
2012-07-05 00:00:08 -03:00
|
|
|
|
|
|
|
// microsecond timestamps for samples. This is needed
|
|
|
|
// to cope with non-uniform time spacing of the data
|
2012-08-17 03:22:10 -03:00
|
|
|
uint32_t _timestamps[FILTER_SIZE];
|
2012-07-04 19:07:57 -03:00
|
|
|
};
|
|
|
|
|
2012-07-05 00:00:08 -03:00
|
|
|
typedef DerivativeFilter<float,5> DerivativeFilterFloat_Size5;
|
|
|
|
typedef DerivativeFilter<float,7> DerivativeFilterFloat_Size7;
|
|
|
|
typedef DerivativeFilter<float,9> DerivativeFilterFloat_Size9;
|