-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathKickFilters.h
427 lines (346 loc) · 15.2 KB
/
KickFilters.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
FILENAME: KickFilters.h
AUTHOR: Orlando S. Hoilett, Benjamin D. Walters, and Akio K. Fujita
EMAIL: [email protected]
VERSION: 3.0.0
AFFILIATIONS
Linnes Lab, Weldon School of Biomedical Engineering,
Purdue University, West Lafayette, IN 47907
DESCRIPTION
This is a static, templated class, so funciton calls must be preceeded with
"KickFilters<variable_type>::" where variable_type should be replaced with
int16_t, int, float, etc.
This class includes a few basic digital filter funcions.
UPDATES
Version 0.0.0
2020/02/19:1200>
- Outlining
Version 1.0.0
2020/03/04:1200>
- Initial release.
Version 1.0.1
2020/03/12:1748>
- Updated descriptions and comments. Also reorganized folder
structures to comply wit Arduino library guidelines by using
"extras" folder to store supplementary info for the class.
2020/03/12:1813>
- Fixed moving average filter by resetting the value of sum.
- Made change to lowpass filer function to multiply the first value of
the output by alpha in accordance with the definition of the function.
- Fixed highpass filter function to add tau to dt not multiply
2020/03/23:1032>
- Updated comments.
- Added future updates section.
Version 1.1.0
2020/04/23:1200>
- Added some peak detector functions
Version 1.2.0
2020/05/21:1200>
- Added a bandpass filter function that simply calls the lowpass
filter and highpass filter functions.
Version 1.2.1
2020/06/14:1500> (UTC-5)
- fixed an error in the bandpass filter function by adding an
additional tempArray input that allows going from the lowpass to the
highpass filter functions.
Version 1.3.0
2020/06/15:1616> (UTC-5)
- added a non-functioning median filter function.
2020/07/11:0707> (UTC-)
- updated comments.
Version 2.0.0
2020/08/21:1542> (UTC-5)
- Added a notch filter.
- Moved to a templated class.
Version 2.1.0
2020/08/22:1726> (UTC-5)
- Added a median filter
Version 3.0.0
2020/09/28:1803> (UTC-5)
- Changed dt parameter to fs in highpass, lowpass, and bandpass
filter functions
FUTURE UPDATES TO INCLUDE
(CHECK) 1. Making this a templated class, meaning it will accept any data type
for the data to be filtered.
2. A moving average filter function that takes cut-off frequency as an input.
3. Adjusting moving average filter such that it appropriately deals with
samples on the trailing end of the filter (or leading end). Right now, the
filter works by averaging samples in front of the current index, which leaves
a few samples hanging at the end of the input data array.
4. Considering returning a bode plot, phase lag, or something with the functions.
5. Look into making bandpass filter function more efficient.
DISCLAIMER
Linnes Lab code, firmware, and software is released under the
MIT License (http://opensource.org/licenses/MIT).
The MIT License (MIT)
Copyright (c) 2020 Linnes Lab, Purdue University
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef KickFilters_h
#define KickFilters_h
//Standard Arduino libraries
#include <Arduino.h>
//Kick LL Libraries
#include "KickMath.h"
template<typename Type>
class KickFilters
{
public:
static void highpass(const Type input[], Type output[], uint16_t samples, float fc, float fs);
static void lowpass(const Type input[], Type output[], uint16_t samples, float fc, float fs);
static void bandpass(const Type input[], Type output[], Type tmpArray[], const uint16_t samples, float fc1, float fc2, float fs);
static void movingAverage(const Type input[], Type output[], uint16_t samples, uint16_t order);
static void notch(const Type input[], Type output[], uint16_t samples, float fc, float fs);
static void notch(const Type input[], Type output[], uint16_t samples, float fc, float fs, float r_coeff);
static void median(const Type input[], Type output[], Type tempArray[], Type tempArray2[],
const uint16_t samples, const uint16_t order, const uint16_t window);
};
//void KickFilters::highpass(const int16_t input[], int16_t output[], uint16_t samples, float fc, uint16_t dt)
//input data array...declared as const so it's read-only
//output result of filter gets stored in this array. Not declared as
// const so it is eligible for both read and write
//samples number of samples in array
//fc desired cut-off frequency of the filter (in Hertz)
//dt sampling period (ms)...time between samples in milliseconds
//
//Implements a simple first-order high pass filter according to the algorithm
//here: https://en.wikipedia.org/wiki/High-pass_filter
//The Wikipedia article is also located in the library in the "extras/references/" folder.
template<typename Type>
void KickFilters<Type>::highpass(const Type input[], Type output[], uint16_t samples, float fc, float fs)
{
//Tau = Resistance(R)*Capacitance(C)
//re-arranging the cut-off frequency equaion [1/(2*pi*R*C)] to solve for R*C
float tau = 1/(2.0*PI*fc);
float alpha = tau / (tau + (1.0/fs));
output[0] = input[0];
for (uint16_t i = 1; i < samples; i++)
{
output[i] = alpha*(output[i-1] + input[i] - input[i-1]);
}
}
//void KickFilters::lowpass(const int16_t input[], int16_t output[], uint16_t samples, float fc, uint16_t dt)
//input data array...declared as const so it's read-only
//output result of filter gets stored in this array. Not declared as
// const so it is eligible for both read and write
//samples number of samples in array
//fc desired cut-off frequency of the filter (in Hertz)
//dt sampling period (ms)...time between samples in milliseconds
//
//Implements a simple first-order low pass filter according to the algorithm
//here: https://en.wikipedia.org/wiki/Low-pass_filter
//The Wikipedia article is also located in the library in the "extras/references/" folder.
template<typename Type>
void KickFilters<Type>::lowpass(const Type input[], Type output[], uint16_t samples, float fc, float fs)
{
//Tau = Resistance*Capacitance
//re-arranging the cut-off frequency equaion [1/(2*pi*R*C)] to solve for R*C
float tau = 1/(2.0*PI*fc);
float alpha = (1.0/fs) / (tau + (1.0/fs));
output[0] = alpha*input[0];
for (uint16_t i = 1; i < samples; i++)
{
output[i] = output[i-1] + alpha*(input[i] - output[i-1]);
}
}
//void KickFilters::bandpass(const int16_t input[], int16_t output[], int16_t tmpArray[],
// const uint16_t samples, float fc1, float fc2, uint16_t dt)
//input data array...declared as const so it's read-only
//output result of filter gets stored in this array. Not declared as
// const so it is eligible for both read and write
//tmpArray tmpArray for transitioning from low pass to high pass filters
//samples number of samples in array
//fc1 cutoff frequency of high pass filter (in Hertz)
//fc2 cutoff frequency of low pass filter (in Hertz)
//dt sampling period (ms)...time between samples in milliseconds
//
//Implements a simple first-order bandpass filter by combing a lowpass and
//highpass filters described here and implemented in this class
//here: https://en.wikipedia.org/wiki/Low-pass_filter
//here: https://en.wikipedia.org/wiki/High-pass_filter
//The Wikipedia articles are also located in the library in the "extras/references/" folder.
template<typename Type>
void KickFilters<Type>::bandpass(const Type input[], Type output[], Type tmpArray[],
const uint16_t samples, float fc1, float fc2, float fs)
{
lowpass(input, tmpArray, samples, fc2, fs);
highpass(tmpArray, output, samples, fc1, fs);
}
//void KickFilters::movingAverage(const int16_t input[], int16_t output[], uint16_t samples, uint16_t order)
//input data array...declared as const so it's read-only
//output result of filter gets stored in this array. Not declared as
// const so it is eligible for both read and write
//samples number of samples in array
//order filter order...how many samples should be averaged
//
//Implements a simple moving average filter. Implemented such that the last few
//outputs of the filter (equal to the order of the filter) are not usable.
template<typename Type>
void KickFilters<Type>::movingAverage(const Type input[], Type output[], uint16_t samples, uint16_t order)
{
float sum = 0;
for(uint16_t i = 0; i < samples-order; i++)
{
sum = 0;
for(uint16_t j = i; j < i+order; j++)
{
sum += input[j];
}
output[i] = (Type)(sum / order);
}
}
//void KickFilters<Type>::notch(const Type input[], Type output[], uint16_t samples, float fc, float fs)
//input data array...declared as const so it's read-only
//output result of filter gets stored in this array. Not declared as
// const so it is eligible for both read and write
//samples number of samples in array
//fc desired center frequency of the notch filter (the frequency to
// be filtered)
//fs sampling frequency (Hz) of signal being filtered
//
//Implements a simple notch filter. Adapted from Wang and Xiao - 2013 - Second-Order
//IIR Notch Filter Design and Implementation of Digital Signal Processing System.
//Ppaer is also stored in extras folder.
template<typename Type>
void KickFilters<Type>::notch(const Type input[], Type output[], uint16_t samples, float fc, float fs)
{
//Notch filter parameters & constants
float r = 0.8;
float b0 = 1;
float b1 = -2*cos(2*PI*fc/fs); //{Equation: -2.0*cos(2*PI*fc/float(fs));}
float b2 = 1;
float a1 = 2*r*cos(2*PI*fc/fs); //{Equation: 2*r*cos(2*PI*fc/float(fs));}
float a2 = -(r*r);//{Equation: -1.0 * pow(r,2);}
float bs_filter[2] = {0,0}; //stores previous filter outputs
float bs_Val = 0; //Variable to hold most recent digital bandstop filter value
//calculate filter output
bs_Val = input[0] + (b1 * 0) + (b2 * 0) + (a1 * bs_filter[1]) + (a2 * bs_filter[0]);
//update filter output values
bs_filter[0] = 0;
bs_filter[1] = bs_Val;
//update output array
output[0] = bs_Val;
//calculate filter output
bs_Val = input[1] + (b1 * input[0]) + (b2 * 0) + (a1 * bs_filter[1]) + (a2 * bs_filter[0]);
//update filter output values
bs_filter[0] = bs_filter[1];
bs_filter[1] = bs_Val;
//update output array
output[1] = bs_Val;
for(uint16_t i = 2; i < samples; i++)
{
//calculate filter output
bs_Val = input[i] + (b1 * input[i-1]) + (b2 * input[i-2]) + (a1 * bs_filter[1]) + (a2 * bs_filter[0]);
//update filter output values
bs_filter[0] = bs_filter[1];
bs_filter[1] = bs_Val;
//update output array
output[i] = bs_Val;
}
}
//void KickFilters<Type>::notch(const Type input[], Type output[], uint16_t samples, float fc, float fs, float r_coeff)
//input data array...declared as const so it's read-only
//output result of filter gets stored in this array. Not declared as
// const so it is eligible for both read and write
//samples number of samples in array
//fc desired center frequency of the notch filter (the frequency to
// be filtered)
//fs sampling frequency (Hz) of signal being filtered
//r_coeff controls filter bandwith (wideness and steepness of the notch)
// 0.8 appears be most ideal, but the user may experiment
// with the value if they so choose
//
//Implements a simple notch filter. Adapted from Wang and Xiao - 2013 - Second-Order
//IIR Notch Filter Design and Implementation of Digital Signal Processing System.
//Ppaer is also stored in extras folder.
template<typename Type>
void KickFilters<Type>::notch(const Type input[], Type output[], uint16_t samples, float fc, float fs, float r_coeff)
{
//Notch filter parameters & constants
float r = r_coeff;
float b0 = 1;
float b1 = -2*cos(2*PI*fc/fs); //{Equation: -2.0*cos(2*PI*fc/float(fs));}
float b2 = 1;
float a1 = 2*r*cos(2*PI*fc/fs); //{Equation: 2*r*cos(2*PI*fc/float(fs));}
float a2 = -(r*r);//{Equation: -1.0 * pow(r,2);}
float bs_filter[2] = {0,0}; //stores previous filter outputs
float bs_Val = 0; //Variable to hold most recent digital bandstop filter value
//calculate filter output
bs_Val = input[0] + (b1 * 0) + (b2 * 0) + (a1 * bs_filter[1]) + (a2 * bs_filter[0]);
//update filter output values
bs_filter[0] = 0;
bs_filter[1] = bs_Val;
//update output array
output[0] = bs_Val;
//calculate filter output
bs_Val = input[1] + (b1 * input[0]) + (b2 * 0) + (a1 * bs_filter[1]) + (a2 * bs_filter[0]);
//update filter output values
bs_filter[0] = bs_filter[1];
bs_filter[1] = bs_Val;
//update output array
output[1] = bs_Val;
for(uint16_t i = 2; i < samples; i++)
{
//calculate filter output
bs_Val = input[i] + (b1 * input[i-1]) + (b2 * input[i-2]) + (a1 * bs_filter[1]) + (a2 * bs_filter[0]);
//update filter output values
bs_filter[0] = bs_filter[1];
bs_filter[1] = bs_Val;
//update output array
output[i] = bs_Val;
}
}
//void KickFilters<Type>::median(const Type input[], Type output[], Type tempArray[], Type tempArray2[],
// const uint16_t samples, const uint16_t order, const uint16_t window = 1)
//input data array...declared as const so it's read-only
//output result of filter gets stored in this array. Not declared as
// const so it is eligible for both read and write
//tmpArray since the calcMedian function rearranges the original array, we
// feed it a tmpArray to prevent editing the original data array
//tmpArray2 since the calcMedian function rearranges the original array, we
// feed it a tmpArray to prevent editing the original data array
//samples number of samples in array
//order filter order...how many samples should rearrange at a time for
// finding the median
//window number of samples to skip when moving the calculation across the
// entire data array; default value = 1
//
//Applies a median filter on an input dataset. Similar to MATLAB's medfilt1
//except for the indexing of the array.
//reference: https://www.mathworks.com/help/signal/ref/medfilt1.html
template<typename Type>
void KickFilters<Type>::median(const Type input[], Type output[], Type tempArray[], Type tempArray2[],
const uint16_t samples, const uint16_t order, const uint16_t window = 1)
{
uint16_t outputIndex = 0;
//Explicilty set empty spots in output array to 0
for(uint16_t i = 0; i < order; i++)
{
output[i] = 0;
}
for(uint16_t i = order; i < samples; i += window)
{
//moved data to tmpArray to avoid editing original data array
for(uint16_t j = i; j < i+order; j++)
{
tempArray[j-i] = input[j];
}
output[outputIndex] = KickMath<Type>::calcMedian(order, tempArray, tempArray2);
outputIndex++;
}
}
#endif /* KickFilters_h */