forked from cculianu/SpikeGL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHPFilter.cpp
61 lines (54 loc) · 1.33 KB
/
HPFilter.cpp
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
52
53
54
55
56
57
58
59
60
61
#include "HPFilter.h"
#include <math.h>
#ifndef M_PI
# define M_PI 3.14159265358979323846
#endif
HPFilter::HPFilter(unsigned ssize, double coff)
{
setScanSize(ssize);
setCutoffFreqHz(coff);
}
void HPFilter::setScanSize(unsigned ss)
{
state.clear();
state.resize(ss, 0.);
lastDt = 0.;
virgin = true;
}
void HPFilter::setCutoffFreqHz(double f)
{
if (f <= 0.) return; // error!
cutoffHz = f;
virgin = true; // force a recompute of coeffs on next call to apply()
}
void HPFilter::apply(short *scan, double dt)
{
std::vector<bool> v;
v.resize(scanSize(), true);
apply(scan, dt, v);
}
void HPFilter::apply(short *scan, double dt, const std::vector<bool> & chans)
{
if (chans.size() < scanSize()) return;
if (dt <= 0.) return; // error!
if (lastDt != dt || virgin) {
lastDt = dt;
recomputeCoeffs();
}
const int ss = scanSize();
for (int i = 0; i < ss; ++i) {
const double in = static_cast<double>(scan[i])/32768.;
state[i] = B * in + A * state[i];
double out = in - state[i];
if (out > 1.0) out = 1.0;
else if (out < -1.0) out = -1.0;
if (chans[i])
scan[i] = static_cast<short>(out*32767.);
}
virgin = false;
}
void HPFilter::recomputeCoeffs()
{
A = exp(-2.0 * M_PI * cutoffHz * lastDt);
B = 1.0 - A;
}