SatDump 2.0.0-alpha-520736c72
Loading...
Searching...
No Matches
stats.h
Go to the documentation of this file.
1#pragma once
2
7
8#include <algorithm>
9#include <map>
10#include <vector>
11
12namespace satdump
13{
23 template <class InputIt, class T = typename std::iterator_traits<InputIt>::value_type>
24 T most_common(InputIt begin, InputIt end, T def)
25 {
26 if (begin == end)
27 return def;
28
29 std::map<T, int> counts;
30 for (InputIt it = begin; it != end; ++it)
31 {
32 if (counts.find(*it) != counts.end())
33 ++counts[*it];
34 else
35 counts[*it] = 1;
36 }
37 return std::max_element(counts.begin(), counts.end(), [](const std::pair<T, int> &pair1, const std::pair<T, int> &pair2) { return pair1.second < pair2.second; })->first;
38 }
39
48 template <class InputIt, class T = typename std::iterator_traits<InputIt>::value_type>
49 T average_common(InputIt begin, InputIt end)
50 {
51 T avg = 0;
52 size_t count = 0;
53 for (InputIt it = begin; it != end; ++it)
54 {
55 avg += *it;
56 count++;
57 }
58 return avg / T(count);
59 }
60
69 template <typename T>
70 T percentile(T *array, int size, float percentile)
71 {
72 float number_percent = (size + 1) * percentile / 100.0f;
73 if (number_percent == 1)
74 return array[0];
75 else if (number_percent == size)
76 return array[size - 1];
77 else
78 return array[(int)number_percent - 1] + (number_percent - (int)number_percent) * (array[(int)number_percent] - array[(int)number_percent - 1]);
79 }
80
89 template <typename T>
90 double avg_overflowless(std::vector<T> const &v)
91 {
92 T n = 0;
93 double mean = 0.0;
94 for (auto x : v)
95 {
96 double delta = x - mean;
97 mean += delta / ++n;
98 }
99 return mean;
100 }
101
109 double get_median(std::vector<double> values);
110} // namespace satdump
T average_common(InputIt begin, InputIt end)
Returns the average of the provided elements.
Definition stats.h:49
T percentile(T *array, int size, float percentile)
Returns a percentile of the provided array.
Definition stats.h:70
T most_common(InputIt begin, InputIt end, T def)
Return the most common element within the provided sequence.
Definition stats.h:24
double avg_overflowless(std::vector< T > const &v)
Returns the average of the provided elements. Version that won't overflow with very large numbers and...
Definition stats.h:90