mad-location-manager-lib 1.0
Library for fusing GPS and ENU accelerometer data
Loading...
Searching...
No Matches
low_pass.h
Go to the documentation of this file.
1#ifndef LOW_PASS_H
2#define LOW_PASS_H
3
4#include <math.h>
5
6#include <cstddef>
7#include <cstring>
8
14template <class T, size_t N>
16{
17 private:
18 double fc;
19 T y[N];
20 bool has_init;
21 double last_ts;
22
23 public:
26 {
27 for (size_t i = 0; i < N; ++i) {
28 y[i] = T();
29 }
30 }
31
32 T* filter(const T (&src)[N], double ts)
33 {
34 if (!has_init) {
35 std::memcpy(y, src, sizeof(T) * N);
36 has_init = true;
37 } else {
38 double dt = ts - last_ts;
39 double alpha = dt / ((1.0 / (2.0 * M_PI * fc)) + dt);
40
41 for (size_t i = 0; i < N; ++i) {
42 y[i] += alpha * (src[i] - y[i]);
43 }
44 }
45 last_ts = ts;
46 return y;
47 }
48};
49
50#endif
Digital low-pass filter for arrays of data.
Definition low_pass.h:16
bool has_init
Initialization flag.
Definition low_pass.h:20
double fc
Cutoff frequency [Hz] T y[N];.
Definition low_pass.h:18
double last_ts
Previous timestamp.
Definition low_pass.h:21
low_pass_filter(double cutoff_hz)
Definition low_pass.h:24
T y[N]
Filtered output buffer.
Definition low_pass.h:19
T * filter(const T(&src)[N], double ts)
Definition low_pass.h:32
#define mtx_t
Definition kalman.h:20