MCPcopy Create free account
hub / github.com/CopterExpress/clover / AverageStat

Class AverageStat

clover_simulation/src/throttling_camera.cpp:17–99  ·  view source on GitHub ↗

* Simple statistics-collecting class. */

Source from the content-addressed store, hash-verified

15 * Simple statistics-collecting class.
16 */
17class AverageStat
18{
19private:
20 /** Currently collected samples. */
21 std::deque<double> samples;
22 /** Number of samples to store (also, the number of samples considered "adequate") */
23 size_t maxSamples;
24 /** Currently stored average value */
25 double average = 0;
26 /** Currently stored standard deviation value */
27 double stdev = 0;
28 /** Largest standard deviation that is considered adequate */
29 double maxStDev;
30
31public:
32 AverageStat(size_t numSamples, double validStDev) :
33 samples(),
34 maxSamples(numSamples),
35 maxStDev(validStDev)
36 {}
37
38 /**
39 * Add a sample and recalculate average and standard deviation.
40 *
41 * @param sample New sampled value.
42 * @return New average value.
43 */
44 double update(double sample)
45 {
46 samples.push_back(sample);
47 if (samples.size() > maxSamples)
48 {
49 samples.pop_front();
50 }
51 average = std::accumulate(samples.begin(), samples.end(), 0.0) / samples.size();
52 double stdevSquared = std::accumulate(samples.begin(), samples.end(), 0.0,
53 [&](double sum, double xi) {
54 return sum + (xi - average) * (xi - average);
55 }) / samples.size();
56 stdev = std::sqrt(stdevSquared);
57 return average;
58 }
59
60 /**
61 * Get current average value of all samples.
62 *
63 * @note This function will return a result even if it is not considered valid.
64 */
65 double getAverage() const
66 {
67 return average;
68 }
69
70 /**
71 * Get current standard deviation of all samples.
72 *
73 * @note This function will return a result even if it is not considered valid.
74 */

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected