JayBeams  0.1
Another project to have fun coding.
create_triangle_timeseries.hpp
Go to the documentation of this file.
1 #ifndef jb_testing_create_triangle_timeseries_hpp
2 #define jb_testing_create_triangle_timeseries_hpp
3 
7 #include <boost/multi_array.hpp>
8 
9 namespace jb {
10 namespace testing {
11 
12 /**
13  * Create a simple timeseries where the values look like a triangle.
14  */
15 template <typename timeseries>
16 void create_triangle_timeseries(int nsamples, timeseries& ts) {
17  resize_if_applicable(ts, nsamples);
18  using value_type = typename timeseries::value_type;
19  float p4 = nsamples / 4.0F;
20  for (int i = 0; i != nsamples / 2; ++i) {
21  ts[i] = value_type((i - p4) / p4);
22  ts[i + nsamples / 2] = value_type((p4 - i) / p4);
23  }
24 }
25 
26 /**
27  * Create a family of timeseries where the values look like parallel triangles
28  * shifted by a constant value.
29  *
30  * @param nsamples samples on each timeseries
31  * @param ts multi array containing timeseries to be filled as triangle
32  *
33  * @tparam T type of the timeseries field
34  * @tparam K timeseries dimensionality
35  * @tparam A an Allocator type for type T allocator storage
36  */
37 template <typename T, std::size_t K, typename A>
38 void create_triangle_timeseries(int nsamples, boost::multi_array<T, K, A>& ts) {
39  /// The values stored in the input timeseries
40  using value_type = T;
41  assert(jb::detail::nsamples(ts) == static_cast<std::size_t>(nsamples));
42  float p4 = nsamples / 4.0F;
43  int num_timeseries = jb::detail::element_count(ts) / nsamples;
44  // fill the first timeseries
45  int count = 0;
46  for (int i = 0; i != nsamples / 2; ++i, ++count) {
47  ts.data()[count] = value_type((i - p4) / p4);
48  ts.data()[count + nsamples / 2] = value_type((p4 - i) / p4);
49  }
50  // ... the rest of the timeseries are shifted
51  int shift = nsamples / num_timeseries;
52  count = nsamples;
53  for (int j = 1; j != num_timeseries; ++j) {
54  for (int i = 0; i != nsamples; ++i, ++count) {
55  ts.data()[count] = ts.data()[count + shift - nsamples];
56  }
57  }
58 }
59 
60 } // namespace testing
61 } // namespace jb
62 
63 #endif // jb_testing_create_triangle_timeseries_hpp
void resize_if_applicable(timeseries &ts, std::size_t newsize, std::true_type)
Resize a timeseries to a newsize.
void create_triangle_timeseries(int nsamples, timeseries &ts)
Create a simple timeseries where the values look like a triangle.
std::size_t nsamples(container_type const &a)
Count the elements in the last dimension of a vector-like container.
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