Monado OpenXR Runtime
m_filter_one_euro.h
Go to the documentation of this file.
1// Copyright 2021-2023, Collabora, Ltd.
2// Copyright 2021, Jan Schmidt
3// SPDX-License-Identifier: BSL-1.0
4/*!
5 * @file
6 * @brief Header for a "One Euro Filter" implementation.
7 * @author Moses Turner <moses@collabora.com>
8 * @author Jan Schmidt <jan@centricular.com>
9 * @author Rylie Pavlik <rylie.pavlik@collabora.com>
10 * @ingroup aux_math
11 *
12 * See the original publication:
13 *
14 * Casiez, G., Roussel, N., and Vogel, D. 2012. 1 € filter: a simple speed-based low-pass filter for noisy input in
15 * interactive systems. In: Proceedings of the SIGCHI Conference on Human Factors in Computing Systems. Association for
16 * Computing Machinery, New York, NY, USA, 2527–2530.
17 *
18 * Available at: https://hal.inria.fr/hal-00670496/document
19 */
20
21#pragma once
22
23#include "xrt/xrt_defines.h"
24#include "math/m_api.h"
25
26// Suggestions. These are suitable for head tracking.
27#define M_EURO_FILTER_HEAD_TRACKING_FCMIN 30.0
28#define M_EURO_FILTER_HEAD_TRACKING_FCMIN_D 25.0
29#define M_EURO_FILTER_HEAD_TRACKING_BETA 0.6
30
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
36/*!
37 * @brief Base data type for One Euro filter instances.
38 *
39 * @ingroup aux_math
40 */
42{
43 /** Minimum frequency cutoff for filter, default = 25.0 */
44 float fc_min;
45
46 /** Minimum frequency cutoff for derivative filter, default = 10.0 */
47 float fc_min_d;
48
49 /** Beta value for "responsiveness" of filter - default = 0.01 */
50 float beta;
51
52 /** true if we have already processed a history sample */
54
55 /** Timestamp of previous sample (nanoseconds) */
56 uint64_t prev_ts;
57};
58
59/*!
60 * @brief One Euro filter for a single float measurement.
61 *
62 * @ingroup aux_math
63 */
65{
66 /** Base/common data */
68
69 /** The most recent measurement, after filtering. */
70 double prev_y;
71
72 /** The most recent sample derivative, after filtering. */
73 double prev_dy;
74};
75
76/*!
77 * @brief One Euro filter for a 2D float measurement.
78 *
79 * @ingroup aux_math
80 */
82{
83 /** Base/common data */
85
86 /** The most recent measurement, after filtering. */
88
89 /** The most recent sample derivative, after filtering. */
91};
92
93/*!
94 * @brief One Euro filter for a 3D float measurement.
95 *
96 * @ingroup aux_math
97 */
99{
100 /** Base/common data */
102
103 /** The most recent measurement, after filtering. */
105
106 /** The most recent sample derivative, after filtering. */
108};
109
110
111/*!
112 * @brief One Euro filter for a unit quaternion (used as 3D rotation).
113 *
114 * @ingroup aux_math
115 */
117{
118 /** Base/common data */
120
121 /** The most recent measurement, after filtering. */
123
124 /** The most recent sample derivative, after filtering. */
126};
127
128/**
129 * @brief Initialize a 1D filter
130 *
131 * @param f self pointer
132 * @param fc_min Minimum frequency cutoff for filter
133 * @param fc_min_d Minimum frequency cutoff for derivative filter
134 * @param beta Beta value for "responsiveness" of filter
135 *
136 * @public @memberof m_filter_euro_f32
137 */
138void
139m_filter_euro_f32_init(struct m_filter_euro_f32 *f, double fc_min, double fc_min_d, double beta);
140
141/**
142 * @brief Filter a measurement and commit changes to filter state
143 *
144 * @param[in,out] f self pointer
145 * @param ts measurement timestamp
146 * @param in_y raw measurement
147 * @param[out] out_y filtered measurement
148 *
149 * @public @memberof m_filter_euro_f32
150 */
151void
152m_filter_euro_f32_run(struct m_filter_euro_f32 *f, uint64_t ts, const float *in_y, float *out_y);
153
154/**
155 * @brief Initialize a 2D filter
156 *
157 * @param f self pointer
158 * @param fc_min Minimum frequency cutoff for filter
159 * @param beta Beta value for "responsiveness" of filter
160 * @param fc_min_d Minimum frequency cutoff for derivative filter
161 *
162 * @public @memberof m_filter_euro_vec2
163 */
164void
165m_filter_euro_vec2_init(struct m_filter_euro_vec2 *f, double fc_min, double fc_min_d, double beta);
166
167/**
168 * @brief Filter a measurement and commit changes to filter state
169 *
170 * @param[in,out] f self pointer
171 * @param ts measurement timestamp
172 * @param in_y raw measurement
173 * @param[out] out_y filtered measurement
174 *
175 * @public @memberof m_filter_euro_vec2
176 */
177void
178m_filter_euro_vec2_run(struct m_filter_euro_vec2 *f, uint64_t ts, const struct xrt_vec2 *in_y, struct xrt_vec2 *out_y);
179
180/**
181 * @brief Filter a measurement **without** committing changes to filter state
182 *
183 * Similar to @ref m_filter_euro_vec2_run but @p f is not modified.
184 *
185 * @param[in] f self pointer
186 * @param ts measurement timestamp
187 * @param in_y raw measurement
188 * @param[out] out_y filtered measurement
189 *
190 * @public @memberof m_filter_euro_vec2
191 */
192void
194 uint64_t ts,
195 const struct xrt_vec2 *in_y,
196 struct xrt_vec2 *out_y);
197
198/**
199 * @brief Initialize a 3D filter
200 *
201 * @param f self pointer
202 * @param fc_min Minimum frequency cutoff for filter
203 * @param fc_min_d Minimum frequency cutoff for derivative filter
204 * @param beta Beta value for "responsiveness" of filter
205 *
206 * @public @memberof m_filter_euro_vec3
207 */
208void
209m_filter_euro_vec3_init(struct m_filter_euro_vec3 *f, double fc_min, double fc_min_d, double beta);
210
211/**
212 * @brief Filter a measurement and commit changes to filter state
213 *
214 * @param[in,out] f self pointer
215 * @param ts measurement timestamp
216 * @param in_y raw measurement
217 * @param[out] out_y filtered measurement
218 *
219 * @public @memberof m_filter_euro_vec3
220 */
221void
222m_filter_euro_vec3_run(struct m_filter_euro_vec3 *f, uint64_t ts, const struct xrt_vec3 *in_y, struct xrt_vec3 *out_y);
223
224/**
225 * @brief Initialize a unit quaternion (3D rotation) filter
226 *
227 * @param f self pointer
228 * @param fc_min Minimum frequency cutoff for filter
229 * @param fc_min_d Minimum frequency cutoff for derivative filter
230 * @param beta Beta value for "responsiveness" of filter
231 *
232 * @public @memberof m_filter_euro_quat
233 */
234void
235m_filter_euro_quat_init(struct m_filter_euro_quat *f, double fc_min, double fc_min_d, double beta);
236
237/**
238 * @brief Filter a measurement and commit changes to filter state
239 *
240 * @param[in,out] f self pointer
241 * @param ts measurement timestamp
242 * @param in_y raw measurement
243 * @param[out] out_y filtered measurement
244 *
245 * @public @memberof m_filter_euro_quat
246 */
247void
248m_filter_euro_quat_run(struct m_filter_euro_quat *f, uint64_t ts, const struct xrt_quat *in_y, struct xrt_quat *out_y);
249
250
251#ifdef __cplusplus
252}
253#endif
C interface to math library.
One Euro filter for a single float measurement.
Definition: m_filter_one_euro.h:65
void m_filter_euro_f32_run(struct m_filter_euro_f32 *f, uint64_t ts, const float *in_y, float *out_y)
Filter a measurement and commit changes to filter state.
Definition: m_filter_one_euro.c:136
double prev_y
The most recent measurement, after filtering.
Definition: m_filter_one_euro.h:70
double prev_dy
The most recent sample derivative, after filtering.
Definition: m_filter_one_euro.h:73
void m_filter_euro_f32_init(struct m_filter_euro_f32 *f, double fc_min, double fc_min_d, double beta)
Initialize a 1D filter.
Definition: m_filter_one_euro.c:130
struct m_filter_one_euro_base base
Base/common data.
Definition: m_filter_one_euro.h:67
One Euro filter for a unit quaternion (used as 3D rotation).
Definition: m_filter_one_euro.h:117
struct m_filter_one_euro_base base
Base/common data.
Definition: m_filter_one_euro.h:119
struct xrt_quat prev_dy
The most recent sample derivative, after filtering.
Definition: m_filter_one_euro.h:125
struct xrt_quat prev_y
The most recent measurement, after filtering.
Definition: m_filter_one_euro.h:122
void m_filter_euro_quat_init(struct m_filter_euro_quat *f, double fc_min, double fc_min_d, double beta)
Initialize a unit quaternion (3D rotation) filter.
Definition: m_filter_one_euro.c:256
void m_filter_euro_quat_run(struct m_filter_euro_quat *f, uint64_t ts, const struct xrt_quat *in_y, struct xrt_quat *out_y)
Filter a measurement and commit changes to filter state.
Definition: m_filter_one_euro.c:262
One Euro filter for a 2D float measurement.
Definition: m_filter_one_euro.h:82
struct xrt_vec2 prev_y
The most recent measurement, after filtering.
Definition: m_filter_one_euro.h:87
void m_filter_euro_vec2_run(struct m_filter_euro_vec2 *f, uint64_t ts, const struct xrt_vec2 *in_y, struct xrt_vec2 *out_y)
Filter a measurement and commit changes to filter state.
Definition: m_filter_one_euro.c:169
struct m_filter_one_euro_base base
Base/common data.
Definition: m_filter_one_euro.h:84
struct xrt_vec2 prev_dy
The most recent sample derivative, after filtering.
Definition: m_filter_one_euro.h:90
void m_filter_euro_vec2_run_no_commit(struct m_filter_euro_vec2 *f, uint64_t ts, const struct xrt_vec2 *in_y, struct xrt_vec2 *out_y)
Filter a measurement without committing changes to filter state.
Definition: m_filter_one_euro.c:195
void m_filter_euro_vec2_init(struct m_filter_euro_vec2 *f, double fc_min, double fc_min_d, double beta)
Initialize a 2D filter.
Definition: m_filter_one_euro.c:163
One Euro filter for a 3D float measurement.
Definition: m_filter_one_euro.h:99
struct xrt_vec3 prev_y
The most recent measurement, after filtering.
Definition: m_filter_one_euro.h:104
void m_filter_euro_vec3_run(struct m_filter_euro_vec3 *f, uint64_t ts, const struct xrt_vec3 *in_y, struct xrt_vec3 *out_y)
Filter a measurement and commit changes to filter state.
Definition: m_filter_one_euro.c:229
struct xrt_vec3 prev_dy
The most recent sample derivative, after filtering.
Definition: m_filter_one_euro.h:107
struct m_filter_one_euro_base base
Base/common data.
Definition: m_filter_one_euro.h:101
void m_filter_euro_vec3_init(struct m_filter_euro_vec3 *f, double fc_min, double fc_min_d, double beta)
Initialize a 3D filter.
Definition: m_filter_one_euro.c:223
Base data type for One Euro filter instances.
Definition: m_filter_one_euro.h:42
float beta
Beta value for "responsiveness" of filter - default = 0.01.
Definition: m_filter_one_euro.h:50
float fc_min
Minimum frequency cutoff for filter, default = 25.0.
Definition: m_filter_one_euro.h:44
bool have_prev_y
true if we have already processed a history sample
Definition: m_filter_one_euro.h:53
uint64_t prev_ts
Timestamp of previous sample (nanoseconds)
Definition: m_filter_one_euro.h:56
float fc_min_d
Minimum frequency cutoff for derivative filter, default = 10.0.
Definition: m_filter_one_euro.h:47
A quaternion with single floats.
Definition: xrt_defines.h:216
A 2 element vector with single floats.
Definition: xrt_defines.h:250
A 3 element vector with single floats.
Definition: xrt_defines.h:271
Common defines and enums for XRT.