JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_StatisticsAccumulator.h
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26//==============================================================================
33template <typename FloatType>
35{
36public:
37 //==============================================================================
40
41 //==============================================================================
45 void addValue (FloatType v) noexcept
46 {
48
49 sum += v;
50 sumSquares += v * v;
51 ++count;
52
53 if (v > maximum) maximum = v;
54 if (v < minimum) minimum = v;
55 }
56
60 void reset() noexcept { *this = StatisticsAccumulator<FloatType>(); }
61
62 //==============================================================================
66 FloatType getAverage() const noexcept
67 {
68 return count > 0 ? sum / (FloatType) count
69 : FloatType();
70 }
71
75 FloatType getVariance() const noexcept
76 {
77 return count > 0 ? (sumSquares - sum * sum / (FloatType) count) / (FloatType) count
78 : FloatType();
79 }
80
84 FloatType getStandardDeviation() const noexcept
85 {
86 return std::sqrt (getVariance());
87 }
88
92 FloatType getMinValue() const noexcept
93 {
94 return minimum;
95 }
96
100 FloatType getMaxValue() const noexcept
101 {
102 return maximum;
103 }
104
106 size_t getCount() const noexcept
107 {
108 return count;
109 }
110
111private:
112 //==============================================================================
113 struct KahanSum
114 {
115 KahanSum() = default;
116 operator FloatType() const noexcept { return sum; }
117
118 void JUCE_NO_ASSOCIATIVE_MATH_OPTIMISATIONS operator+= (FloatType value) noexcept
119 {
120 FloatType correctedValue = value - error;
121 FloatType newSum = sum + correctedValue;
122 error = (newSum - sum) - correctedValue;
123 sum = newSum;
124 }
125
126 FloatType sum{}, error{};
127 };
128
129 //==============================================================================
130 size_t count { 0 };
131 KahanSum sum, sumSquares;
132 FloatType minimum { std::numeric_limits<FloatType>::infinity() },
134};
135
136} // namespace juce
A class that measures various statistics about a series of floating point values that it is given.
StatisticsAccumulator()=default
Constructs a new StatisticsAccumulator.
FloatType getMinValue() const noexcept
Returns the smallest of all previously added values.
FloatType getAverage() const noexcept
Returns the average (arithmetic mean) of all previously added values.
void reset() noexcept
Reset the accumulator.
FloatType getVariance() const noexcept
Returns the variance of all previously added values.
size_t getCount() const noexcept
Returns how many values have been added to this accumulator.
void addValue(FloatType v) noexcept
Add a new value to the accumulator.
FloatType getStandardDeviation() const noexcept
Returns the standard deviation of all previously added values.
FloatType getMaxValue() const noexcept
Returns the largest of all previously added values.
T infinity(T... args)
#define jassert(expression)
Platform-independent assertion macro.
#define JUCE_NO_ASSOCIATIVE_MATH_OPTIMISATIONS
This can be appended to a function declaration to tell gcc to disable associative math optimisations ...
JUCE Namespace.
bool juce_isfinite(NumericType value) noexcept
The isfinite() method seems to vary between platforms, so this is a platform-independent function for...
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
T sqrt(T... args)