OpenShot Audio Library | OpenShotAudio 0.3.2
Loading...
Searching...
No Matches
juce_StatisticsAccumulator.h
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2017 - ROLI Ltd.
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 {
47 jassert (juce_isfinite (v));
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
61
62 //==============================================================================
67 {
68 return count > 0 ? sum / (FloatType) count
69 : FloatType();
70 }
71
76 {
77 return count > 0 ? (sumSquares - sum * sum / (FloatType) count) / (FloatType) count
78 : FloatType();
79 }
80
85 {
86 return std::sqrt (getVariance());
87 }
88
93 {
94 return minimum;
95 }
96
101 {
102 return maximum;
103 }
104
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() },
133 maximum { -std::numeric_limits<FloatType>::infinity() };
134};
135
136} // namespace juce
FloatType getMinValue() const noexcept
FloatType getAverage() const noexcept
FloatType getVariance() const noexcept
void addValue(FloatType v) noexcept
FloatType getStandardDeviation() const noexcept
FloatType getMaxValue() const noexcept