JayBeams  0.1
Another project to have fun coding.
delay_timeseries.hpp
Go to the documentation of this file.
1 #ifndef jb_testing_delay_timeseries_hpp
2 #define jb_testing_delay_timeseries_hpp
3 
5 
6 #include <cstddef>
7 #include <cstdint>
8 #include <utility>
9 
10 namespace jb {
11 namespace testing {
12 
13 /**
14  * A functor to extrapolate with zeroes.
15  */
16 template <typename sample_t>
18  std::pair<std::ptrdiff_t, sample_t>
19  operator()(std::intmax_t index, std::size_t size) const {
20  if (index < 0 or size <= std::size_t(index)) {
21  return std::make_pair(static_cast<std::ptrdiff_t>(-1), sample_t(0));
22  }
23  return std::make_pair(static_cast<std::ptrdiff_t>(index), sample_t(0));
24  }
25 };
26 
27 /**
28  * A functor to extrapolate a periodic timseries.
29  */
30 template <typename sample_t>
32  std::pair<std::ptrdiff_t, sample_t>
33  operator()(std::intmax_t index, std::size_t size) const {
34  if (size == 0) {
35  return std::make_pair(static_cast<std::ptrdiff_t>(0), sample_t(0));
36  }
37  if (index < 0) {
38  index = size - (-index % size);
39  }
40  index = index % size;
41  return std::make_pair(static_cast<std::ptrdiff_t>(index), sample_t(0));
42  }
43 };
44 
45 /**
46  * A function to get the extrapolated value of a timeseries.
47  *
48  * @param ts a timeseries, typically a std::vector-like data structure
49  * @param t the timestamp at which we want to extrapolate the
50  * timeseries
51  * @param sampling_period the sampling period of the timeseries
52  * @param extrapolation implement the extrapolation
53  *
54  * @tparam timeseries_t the type of the timeseries
55  * @tparam duration_t the type used to represent timestamps, typically
56  * a std::chrono::duration<> instantiation
57  * @tparam extrapolation_functor the type of the extrapolation
58  */
59 template <
60  typename timeseries_t, typename duration_t, typename extrapolation_functor>
63  timeseries_t const& ts, duration_t t, duration_t sampling_period,
64  extrapolation_functor const& extrapolation) {
65  auto ticks = t / sampling_period;
66  auto r = extrapolation(ticks, jb::detail::element_count(ts));
67  if (r.first == -1) {
68  return r.second;
69  }
70  return *(ts.data() + static_cast<std::size_t>(r.first));
71 }
72 
73 /**
74  * Delay a timeseries using a user-provided extrapolation policy.
75  */
76 template <
77  typename timeseries_t, typename duration_t, typename extrapolation_functor>
78 timeseries_t delay_timeseries(
79  timeseries_t const& ts, duration_t delay, duration_t sampling_period,
80  extrapolation_functor const& extrapolation) {
81  timeseries_t a(jb::detail::array_shape(ts));
82 
83  /// The values stored in the input timeseries ts
84  using element_type =
86 
87  element_type* it_a = a.data();
88  for (std::size_t i = 0; i != jb::detail::element_count(a); ++i, ++it_a) {
89  duration_t stamp = i * sampling_period - delay;
90  *it_a = extrapolate_timeseries(ts, stamp, sampling_period, extrapolation);
91  }
92  return std::move(a);
93 }
94 
95 /**
96  * Delay a timeseries using a periodic extension for early values.
97  */
98 template <typename timeseries_t, typename duration_t>
100  timeseries_t const& ts, duration_t delay, duration_t sampling_period) {
101  /// The values stored in the input timeseries ts
102  using element_type =
104  return delay_timeseries(
105  ts, delay, sampling_period, extrapolate_periodic<element_type>());
106 }
107 
108 /**
109  * Delay a timeseries using zeroes for early values.
110  */
111 template <typename timeseries_t, typename duration_t>
113  timeseries_t const& ts, duration_t delay, duration_t sampling_period) {
114  /// The values stored in the input timeseries ts
115  using element_type =
117  return delay_timeseries(
118  ts, delay, sampling_period, extrapolate_with_zeroes<element_type>());
119 }
120 
121 } // namespace testing
122 } // namespace jb
123 
124 #endif // jb_testing_delay_timeseries_hpp
timeseries_t delay_timeseries_periodic(timeseries_t const &ts, duration_t delay, duration_t sampling_period)
Delay a timeseries using a periodic extension for early values.
typename container_type::value_type element_type
Define the type of the elements in the container.
A functor to extrapolate with zeroes.
std::pair< std::ptrdiff_t, sample_t > operator()(std::intmax_t index, std::size_t size) const
A functor to extrapolate a periodic timseries.
std::pair< std::ptrdiff_t, sample_t > operator()(std::intmax_t index, std::size_t size) const
std::size_t array_shape(container_type const &a)
Return the shape of the container in a form suitable for construction of a vector-like container...
timeseries_t delay_timeseries(timeseries_t const &ts, duration_t delay, duration_t sampling_period, extrapolation_functor const &extrapolation)
Delay a timeseries using a user-provided extrapolation policy.
jb::detail::array_traits< timeseries_t >::element_type extrapolate_timeseries(timeseries_t const &ts, duration_t t, duration_t sampling_period, extrapolation_functor const &extrapolation)
A function to get the extrapolated value of a timeseries.
timeseries_t delay_timeseries_zeroes(timeseries_t const &ts, duration_t delay, duration_t sampling_period)
Delay a timeseries using zeroes for early values.
std::size_t element_count(container_type const &a)
Count the number of elements for a vector-like container.
The top-level namespace for the JayBeams library.
Definition: as_hhmmss.hpp:7