This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
filers.h
196 lines (177 loc) · 4.97 KB
/
filers.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#ifndef _fillers_h_
#define _fillers_h_
#endif
template<typename dataType>
void normalize(std::deque<dataType> & values)
{
dataType max_value = values[0];
for (size_t index = 1; index < values.size(); ++index)
if (values[index] > max_value) max_value = values[index];
for (size_t index = 0; index < values.size(); ++index)
values[i] /= max_value;
}
// DC filter
template<typename dataType>
std::deque<dataType> dcFilter(const std::deque<dataType> & signal)
{
std::deque<dataType> result;
for (size_t index = 0; index < signal.size(); ++index)
{
dataType value = 0;
if (index >= 1) value = signal[index] - signal[index - 1] + 0.995 * result[index - 1];
result.push_back(value);
}
return result;
}
// Low Pass filter
// Implemented as proposed by the original paper.
// y(nT) = 2y(nT - T) - y(nT - 2T) + x(nT) - 2x(nT - 6T) + x(nT - 12T)
template<typename dataType>
std::deque<dataType> lowPassFilter(const std::deque<dataType> & signal)
{
std::deque<dataType> result;
for (size_t index = 0; index < signal.size(); ++index)
{
dataType value = signal[index];
if (index >= 1) value += 2 * result[index - 1];
if (index >= 2) value -= result[index - 2];
if (index >= 6) value -= 2 * signal[index - 6];
if (index >= 12) value += signal[index - 12];
result.push_back(value);
}
return result;
}
// High Pass filter
// Implemented as proposed by the original paper.
// y(nT) = 32x(nT - 16T) - [y(nT - T) + x(nT) - x(nT - 32T)]
template<typename dataType>
std::deque<dataType> highPassFilter(const std::deque<dataType> & signal)
{
std::deque<dataType> result;
for (size_t index = 0; index < signal.size(); ++index)
{
dataType value = -signal[index];
if (index >= 1) value -= result[index - 1];
if (index >= 16) value += 32 * signal[index - 16];
if (index >= 32) value += signal[index - 32];
result.push_back(value);
}
return result;
}
template<typename T = double>
struct BandFilter
{
double b0, b1, b2,
a1, a2;
T x[2], y[2];
enum Type {LOWPASS,HIGHPASS,BANDPASS}; //TODO expand a new
reset()
{
x[0] = 0; x[1] = 0;
y[0] = 0; y[1] = 0;
}
BandFilter(double cutoff, Type type)
{
reset();
const double B = tan(cutoff * M_PI);
const double BB = B * B;
const double S = 1.0 + M_SQRT2 * B + BB;
if (type == HIGHPASS) {
b0 = 1.0 / S;
b1 = -2.0 * b0;
} else
if (type == LOWPASS) {
b0 = BB / S;
b1 = 2.0 * b0;
}
b2 = b0;
a1 = 2.0 * (BB - 1.0) / S;
a2 = (1.0 - M_SQRT2 * B + BB) / S;
}
T process(T input)
{
//IIR
double out = b0*input + b1*x[0] + b2*x[1] - a1*y[0] - a2*y[1];
//add input
x[1] = x[0];
x[0] = input;
//add outut
y[1] = y[0];
y[0] = out;
return out;
}
void filter(std::deque<dataType> & signal)
{
for (size_t i = 0; i < signal.size(); ++i)
{
signal[i] = process(signal[i]);
}
}
};
template<typename dataType>
std::deque<dataType> bandPassFilter(const std::deque<dataType> & signal, double lowcut, double highcut, double rate)
{
std::deque<dataType> result = signal;
BandFilter lowPass(lowcut/rate, BandFilter::LOWPASS);
lowPass.filter(result);
BandFilter highPass(highcut/rate, BandFilter::HIGHPASS);
highPass.filter(result);
return result;
}
// Derivative filter
// Implemented as proposed by the original paper.
// y(nT) = (1/8T)[-x(nT - 2T) - 2x(nT - T) + 2x(nT + T) + x(nT + 2T)]
template<typename dataType>
std::deque<dataType> derivativeFilter(const std::deque<dataType> & signal, size_t fs = 1)
{
double T = 1.0/fs; //???
std::deque<dataType> result;
for (size_t index = 2; index < signal.size() - 2; ++index)
{
dataType value = -signal[index - 2] - 2 * signal[index - 1] +
2 * signal[index + 1] + signal[index + 2];
value /= (8.0 * T);
result.push_back(value);
}
dataType value;
value = result.front();
result.push_front(value);
result.push_front(value);
value = result.back();
result.push_back(value);
result.push_back(value);
return result;
}
// Squared filter
// Implemented as proposed by the original paper.
// y(nT) = [x(nT)]^2.
template<typename dataType>
std::deque<dataType> squaredFilter(const std::deque<dataType> & signal)
{
std::deque<dataType> result;
for (size_t index = 0; index < signal.size(); ++index)
{
dataType value = signal[index]*signal[index];
result.push_back(value);
}
return result;
}
// Moving-Window Integration, delay N/2 ???
// Implemented as proposed by the original paper.
// y(nT) = (1/N)[x(nT - (N - 1)T) + x(nT - (N - 2)T) + ... x(nT)]
// N, in samples, must be defined so that the window is ~150ms.
template<typename dataType>
std::deque<dataType> MWI(const std::deque<dataType> & signal, size_t N)
{
std::deque<dataType> result;
dataType value = 0;
for (size_t index = 0; index < signal.size(); ++index)
{
int first = index - (N - 1); //???
value += signal[index] / N;
if (first > 0) value -= signal[first - 1] / N;
result.push_back(value);
}
return result;
}
#endif