StatisticAccumulator.cti
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
14 /** \file
15  \brief StatisticAccumulator inline template implementation */
16 
17 // Custom includes
18 
19 #define prefix_ inline
20 ///////////////////////////////cti.p///////////////////////////////////////
21 
22 ///////////////////////////////////////////////////////////////////////////
23 // senf::StatisticAccumulator<T>
24 
25 template <class T>
26 prefix_ T senf::StatisticAccumulator<T>::min()
27  const
28 {
29  return min_;
30 }
31 
32 template <class T>
33 prefix_ T senf::StatisticAccumulator<T>::max()
34  const
35 {
36  return max_;
37 }
38 
39 template <class T>
40 prefix_ T senf::StatisticAccumulator<T>::sum()
41  const
42 {
43  return sum_;
44 }
45 
46 template <class T>
47 prefix_ T senf::StatisticAccumulator<T>::sum2()
48  const
49 {
50  return sum_squared_;
51 }
52 
53 template <class T>
54 prefix_ float senf::StatisticAccumulator<T>::avg()
55  const
56 {
57  return cnt_ == 0 ? NAN : float(sum_) / float(cnt_);
58 }
59 
60 template <class T>
61 prefix_ float senf::StatisticAccumulator<T>::rms()
62  const
63 {
64  return cnt_ == 0 ? NAN : sqrtf(float(sum_squared_) / float(cnt_));
65 }
66 
67 template <class T>
68 prefix_ unsigned senf::StatisticAccumulator<T>::count()
69  const
70 {
71  return cnt_;
72 }
73 
74 template <class T>
75 prefix_ void senf::StatisticAccumulator<T>::accumulate(T const & value)
76 {
77  sum_ += value;
78  sum_squared_ += value * value;
79  cnt_++;
80  min_ = std::min(min_, value);
81  max_ = std::max(max_, value);
82 }
83 
84 template <class T>
85 prefix_ senf::StatisticAccumulator<T>::operator bool()
86  const
87 {
88  return cnt_ > 0;
89 }
90 
91 //
92 // EWMA
93 //
94 
95 template <class T>
96 prefix_ void senf::StatisticsEWMA<T>::accumulate(T const & value)
97 {
98  ewma_ = (ewma_ * (1.0f - alpha_)) + (value * alpha_);
99  cnt_++;
100 }
101 
102 template <class T>
103 prefix_ void senf::StatisticsEWMA<T>::accumulateWithLoss(T const & value, unsigned numLost)
104 {
105  // we substitute the lost data with a avg of the last ewma and the new data
106  T avg ((ewma_ + value) * 0.5f);
107 
108  // account for lost data with avg
109  for (unsigned n = 0; n < numLost; n++) {
110  accumulate(avg);
111  }
112 
113  // feed in new value
114  accumulate(value);
115 }
116 
117 template <class T>
118 prefix_ senf::StatisticsEWMA<T>::operator bool()
119  const
120 {
121  return cnt_ > 0;
122 }
123 
124 template <class T>
125 prefix_ bool senf::StatisticsEWMA<T>::operator<(senf::StatisticsEWMA<T> const & other)
126  const
127 {
128  return ewma_ < other.ewma_;
129 }
130 
131 template <class T>
132 prefix_ unsigned senf::StatisticsEWMA<T>::count()
133  const
134 {
135  return cnt_;
136 }
137 
138 template <class T>
139 prefix_ T const & senf::StatisticsEWMA<T>::ewma()
140  const
141 {
142  return ewma_;
143 }
144 
145 template <class T>
146 prefix_ float const & senf::StatisticsEWMA<T>::alpha()
147  const
148 {
149  return alpha_;
150 }
151 
152 
153 ///////////////////////////////cti.e///////////////////////////////////////
154 #undef prefix_
155 
156 
157 // Local Variables:
158 // mode: c++
159 // fill-column: 100
160 // comment-column: 40
161 // c-file-style: "senf"
162 // indent-tabs-mode: nil
163 // ispell-local-dictionary: "american"
164 // compile-command: "scons -U"
165 // End: