GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
statistics.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_STATISTICS_HPP
5#define GREM_CORE_STATISTICS_HPP
6
8
11#include <GREM/core/math.hpp>
12
13#include <cstddef> // std::size_t
14#include <type_traits> // std::remove_cvref_t
15
16namespace grem {
17
23template <typename T>
25public:
29 constexpr ExponentialMovingAverage() = default;
30
36 constexpr explicit ExponentialMovingAverage(T value)
37 : value(value) {}
38
44 constexpr void reset(T newValue = {}) {
45 value = newValue;
46 }
47
58 template <typename Coefficient>
59 constexpr T update(T newSignalValue, Coefficient alpha) {
60 return value = newSignalValue * alpha + value * (Coefficient{1} - alpha);
61 }
62
68 [[nodiscard]] constexpr T get() const {
69 return value;
70 }
71
72private:
73 T value{};
74};
75
82template <typename T, std::size_t N>
84public:
92
96 void clear() noexcept {
97 sampleBuffer.clear();
98 sampleSum = T{};
99 sampleSumOfSquares = SquaredT{};
100 }
101
108 void update(T newSample) noexcept {
109 if (sampleBuffer.size() >= N) {
110 const T expiredSample = sampleBuffer.front();
111 sampleSum -= expiredSample;
112 sampleSumOfSquares -= expiredSample * expiredSample;
113 sampleBuffer.pop_front();
114 }
115 sampleBuffer.push_back(newSample);
116 sampleSum += newSample;
117 sampleSumOfSquares += newSample * newSample;
118 }
119
125 [[nodiscard]] Statistics getStatistics() const noexcept {
126 const std::size_t sampleCount = sampleBuffer.size();
127 const T mean = (sampleCount <= 1) ? sampleSum : sampleSum / sampleCount;
128 const SquaredT variance = (sampleCount <= 1) ? SquaredT{} : (sampleSumOfSquares - (sampleSum * sampleSum) / sampleCount) / (sampleCount - 1);
129 const T standardDeviation = sqrt(variance);
130 return {.mean = mean, .standardDeviation = standardDeviation};
131 }
132
140 [[nodiscard]] auto getSamples() const noexcept {
141 return Subrange{sampleBuffer};
142 }
143
144private:
145 using SquaredT = std::remove_cvref_t<decltype(T{} * T{})>;
146
147 InplaceDoubleEndedQueue<T, N> sampleBuffer{};
148 T sampleSum{};
149 SquaredT sampleSumOfSquares{};
150};
151
152} // namespace grem
153
154#endif
constexpr ExponentialMovingAverage(T value)
Construct an exponential moving average with a specific initial value.
Definition statistics.hpp:36
constexpr void reset(T newValue={})
Reset the exponential moving average to a specific value.
Definition statistics.hpp:44
constexpr T get() const
Get the current exponential moving average value.
Definition statistics.hpp:68
constexpr ExponentialMovingAverage()=default
Default-construct an exponential moving average.
constexpr T update(T newSignalValue, Coefficient alpha)
Update the exponential moving average given the next discrete input signal sample value.
Definition statistics.hpp:59
Sliding window of a discrete-time input signal.
Definition statistics.hpp:83
void clear() noexcept
Clear the sliding window of all samples.
Definition statistics.hpp:96
Statistics getStatistics() const noexcept
Calculate the statistics of the current samples in the sliding window.
Definition statistics.hpp:125
auto getSamples() const noexcept
Get the samples in the sliding window.
Definition statistics.hpp:140
void update(T newSample) noexcept
Push a new sample into the sliding window, popping the oldest sample first if the window is at its ma...
Definition statistics.hpp:108
Definition Subrange.hpp:88
@ T
't'
Definition keyboard.hpp:90
Definition aliases.hpp:9
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL sqrt(T a)
Get the square root of a value.
Definition math.hpp:1114
Statistics of the sliding window.
Definition statistics.hpp:88
T mean
Mean (average) of all samples in the window.
Definition statistics.hpp:89
T standardDeviation
Standard deviation of all samples in the window.
Definition statistics.hpp:90