Monado OpenXR Runtime
t_lowpass_vector.hpp
Go to the documentation of this file.
1 // Copyright 2019, Collabora, Ltd.
2 // SPDX-License-Identifier: BSL-1.0
3 /*!
4  * @file
5  * @brief Low-pass IIR filter on vectors
6  * @author Ryan Pavlik <ryan.pavlik@collabora.com>
7  * @ingroup aux_tracking
8  */
9 
10 #pragma once
11 
12 #ifndef __cplusplus
13 #error "This header is C++-only."
14 #endif
15 
16 #include "tracking/t_lowpass.hpp"
17 
18 #include <Eigen/Core>
19 
20 
21 namespace xrt_fusion {
22 
23 /*!
24  * A very simple low-pass filter, using a "one-pole infinite impulse response"
25  * design (one-pole IIR).
26  *
27  * Configurable in dimension and scalar type.
28  */
29 template <size_t Dim, typename Scalar> class LowPassIIRVectorFilter
30 {
31 public:
32  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
33 
34  using Vector = Eigen::Matrix<Scalar, Dim, 1>;
35 
36  /*!
37  * Constructor
38  *
39  * @param cutoff_hz A cutoff frequency in Hertz: signal changes much
40  * lower in frequency will be passed through the filter, while signal
41  * changes much higher in frequency will be blocked.
42  */
43  explicit LowPassIIRVectorFilter(Scalar cutoff_hz) noexcept
44  : impl_(cutoff_hz, Vector::Zero())
45  {}
46 
47 
48  /*!
49  * Reset the filter to just-created state.
50  */
51  void
52  reset() noexcept
53  {
54  impl_.reset(Vector::Zero());
55  }
56 
57  /*!
58  * Filter a sample, with an optional weight.
59  *
60  * @param sample The value to filter
61  * @param timestamp_ns The time that this sample was measured.
62  * @param weight An optional value between 0 and 1. The smaller this
63  * value, the less the current sample influences the filter state. For
64  * the first call, this is always assumed to be 1.
65  */
66  void
67  addSample(Vector const &sample,
68  std::uint64_t timestamp_ns,
69  Scalar weight = 1)
70  {
71  impl_.addSample(sample, timestamp_ns, weight);
72  }
73 
74  /*!
75  * Access the filtered value.
76  */
77  Vector const &
78  getState() const noexcept
79  {
80  return impl_.state;
81  }
82 
83  /*!
84  * Access the time of last update.
85  */
86  std::uint64_t
87  getTimestampNs() const noexcept
88  {
89  return impl_.filter_timestamp_ns;
90  }
91 
92  /*!
93  * Access whether we have initialized state.
94  */
95  bool
96  isInitialized() const noexcept
97  {
98  return impl_.initialized;
99  }
100 
101 private:
103 };
104 
105 } // namespace xrt_fusion
void addSample(Vector const &sample, std::uint64_t timestamp_ns, Scalar weight=1)
Filter a sample, with an optional weight.
Definition: t_lowpass_vector.hpp:67
Low-pass IIR filter.
void addSample(Value const &sample, timepoint_ns timestamp_ns, Scalar weight=1)
Filter a sample, with an optional weight.
Definition: t_lowpass.hpp:75
LowPassIIRVectorFilter(Scalar cutoff_hz) noexcept
Constructor.
Definition: t_lowpass_vector.hpp:43
std::uint64_t getTimestampNs() const noexcept
Access the time of last update.
Definition: t_lowpass_vector.hpp:87
void reset(Value const &val) noexcept
Reset the filter to just-created state.
Definition: t_lowpass.hpp:58
bool isInitialized() const noexcept
Access whether we have initialized state.
Definition: t_lowpass_vector.hpp:96
void reset() noexcept
Reset the filter to just-created state.
Definition: t_lowpass_vector.hpp:52
Vector const & getState() const noexcept
Access the filtered value.
Definition: t_lowpass_vector.hpp:78
Definition: t_fusion.hpp:25
A very simple low-pass filter, using a "one-pole infinite impulse response" design (one-pole IIR)...
Definition: t_lowpass_vector.hpp:29