Skyward boardcore
Loading...
Searching...
No Matches
Stats.cpp
Go to the documentation of this file.
1/* Copyright (c) 2015-2016 Skyward Experimental Rocketry
2 * Author: Federico Terraneo
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#include "Stats.h"
24
25#include <algorithm>
26#include <cmath>
27#include <limits>
28
29using namespace std;
30
31namespace Boardcore
32{
33
34ostream& operator<<(ostream& os, const StatsResult& sr)
35{
36 os << "min=" << sr.minValue << " max=" << sr.maxValue << " mean=" << sr.mean
37 << " stdDev=" << sr.stdDev;
38 return os;
39}
40
42 : minValue(numeric_limits<float>::max()),
43 maxValue(numeric_limits<float>::lowest()), mean(0.f), m2(0.f), n(0)
44{
45}
46
47void Stats::add(float data)
48{
49 if (isnan(data))
50 return;
51
52 minValue = min(minValue, data);
53 maxValue = max(maxValue, data);
54
55 // Stable algorithm for computing variance, see wikipedia
56 n++;
57 float delta = data - mean;
58 mean += delta / n;
59 m2 += delta * (data - mean);
60}
61
63{
64 minValue = numeric_limits<float>::max();
65 maxValue = numeric_limits<float>::lowest();
66 mean = 0.f;
67 m2 = 0.f;
68 n = 0;
69}
70
72{
73 switch (n)
74 {
75 case 0:
76 return {NAN, NAN, NAN, NAN, n};
77 case 1:
78 return {minValue, maxValue, mean, NAN, n};
79 default:
80 return {minValue, maxValue, mean, sqrtf(m2 / (n - 1)), n};
81 }
82}
83
84} // namespace Boardcore
StatsResult getStats() const
Return statistics of the elements added so far.
Definition Stats.cpp:71
void add(float data)
Definition Stats.cpp:47
This file includes all the types the logdecoder script will decode.
std::ostream & operator<<(std::ostream &o, const GammaConf &conf)
Definition GammaTypes.h:98
Definition WIZ5500.h:318
Statistics computed by the Stats class.
Definition Stats.h:35