-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHighPassFilter.h
51 lines (44 loc) · 1.12 KB
/
HighPassFilter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* HighPassFilter.h
*
* Created on: Nov 6, 2016
* Author: mlaakso
*/
#ifndef HIGHPASSFILTER_H_
#define HIGHPASSFILTER_H_
namespace PowerMonitor
{
//! Simple IIR high-pass filter.
template<class T> class HighPassFilter
{
public:
//! Constructor.
//! \param f crossover frequency in Hz.
//! \param dt time step in s.
HighPassFilter(T dt, T f) :
_alpha(1.0 / (1.0 + 6.283185307 * f * dt)),
_initialized(false)
{
}
T operator()(const T sample)
{
if (!_initialized)
{
_previousResult = sample;
_previousSample = sample;
_initialized = true;
return sample;
}
T result = _alpha * (_previousResult + sample - _previousSample);
_previousResult = result;
_previousSample = sample;
return result;
}
private:
T _alpha; //!< Filter coefficient.
T _previousSample; //!< Previous sample.
T _previousResult; //!< Previous filtered sample.
bool _initialized; //!< Previous samples initialized?
};
}
#endif /* HIGHPASSFILTER_H_ */