RTC Toolkit  1.0.0
computation.hpp
Go to the documentation of this file.
1 
12 #ifndef RTCTK_EXAMPLEDATATASK_MEASURETEL_COMPUTATION_HPP
13 #define RTCTK_EXAMPLEDATATASK_MEASURETEL_COMPUTATION_HPP
14 
17 
18 #include <vector>
19 
20 class Computation {
21 public:
23 
24  enum class Algorithm { Mean, Median };
25 
27  // get number of slopes from array
28  constexpr size_t NUM_SLOPES =
29  std::tuple_size<decltype(decltype(ScaoLoopTopic::wfs)::slopes)>::value;
30  m_result.resize(NUM_SLOPES);
31  Reset();
32  }
33 
34  void Reset() {
35  m_sample_idx = 0;
36  }
37 
38  void SetDynamicConfig(unsigned to_read) {
39  // pre-allocate memory
40  m_samples.resize(to_read);
41 
42  m_samples_to_read = to_read;
43  Reset();
44  }
45 
46  void OnDataAvailable(ScaoLoopTopic const& sample) {
47  m_samples[m_sample_idx++] = sample.wfs.slopes;
48  }
49 
50  struct Result {
51  std::vector<float> const& avg_values;
52  };
53 
54  Result Compute(Algorithm algorithm) {
56 
57  if (m_sample_idx != m_samples_to_read) {
58  CII_THROW(RtctkException, "not all data available");
59  }
60 
61  if (algorithm == Algorithm::Mean) {
62  ComputeMean();
63  } else {
64  CII_THROW(RtctkException, "algorithm not implemented");
65  }
66 
67  Reset();
68  return {m_result};
69  }
70 
71  void ComputeMean() {
72  for (auto& slope : m_result) {
73  slope = 0;
74  }
75 
76  for (auto const& sample : m_samples) {
77  size_t idx = 0;
78  for (auto const& slope : sample) {
79  m_result[idx++] += slope;
80  }
81  }
82 
83  for (auto& slope : m_result) {
84  slope = slope / m_samples.size();
85  }
86  }
87 
88 private:
89  unsigned m_sample_idx;
90  unsigned m_samples_to_read;
91  std::vector<decltype(decltype(ScaoLoopTopic::wfs)::slopes)> m_samples;
92  std::vector<float> m_result;
93 };
94 
95 #endif // RTCTK_EXAMPLEDATATASK_MEASURETEL_COMPUTATION_HPP
Computation::Reset
void Reset()
Definition: computation.hpp:79
rtctk::exampleTopic::ScaoLoopTopic::wfs
WfsLoopBaseTopic< N_SUBAPS > wfs
Definition: topics.hpp:29
exceptions.hpp
Provides macros and utilities for exception handling.
Computation::Algorithm::Mean
@ Mean
Computation::SetDynamicConfig
void SetDynamicConfig(unsigned to_read)
Definition: computation.hpp:38
rtctk::componentFramework::RtctkException
The RtctkException class is the base class for all Rtctk exceptions.
Definition: exceptions.hpp:207
Computation::Result
Definition: computation.hpp:97
Computation::Algorithm::Median
@ Median
rtctk::exampleTopic::WfsLoopBaseTopic::slopes
std::array< float, 2 *NSUBAPS > slopes
Definition: topics.hpp:23
Computation::OnDataAvailable
void OnDataAvailable(ScaoLoopTopic const &sample)
Definition: computation.hpp:46
rtctk::exampleTopic::ScaoLoopTopic
Definition: topics.hpp:27
Computation::Algorithm
Algorithm
Definition: computation.hpp:24
rtctkExampleDataTaskGenFitsData.slopes
slopes
Definition: rtctkExampleDataTaskGenFitsData.py:9
Computation::ComputeMean
void ComputeMean()
Definition: computation.hpp:71
Computation
Definition: computation.hpp:28
Computation::Compute
Result Compute(Algorithm algorithm)
Definition: computation.hpp:54
Computation::Result::avg_values
std::vector< float > const & avg_values
Definition: computation.hpp:51
Computation::Computation
Computation()
Definition: computation.hpp:26
topics.hpp
Common topic definitions used in various examples.