OpenShot Audio Library | OpenShotAudio 0.4.0

Loading...
Searching...
No Matches
juce_StatisticsAccumulator.h
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
23 namespace juce
24{
25
26//==============================================================================
33 template <typename FloatType>
34 class StatisticsAccumulator
35{
36 public:
37 //==============================================================================
39 StatisticsAccumulator() = default;
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
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
111 private:
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
juce::StatisticsAccumulator
Definition juce_StatisticsAccumulator.h:35
juce::StatisticsAccumulator::StatisticsAccumulator
StatisticsAccumulator()=default
juce::StatisticsAccumulator::getMinValue
FloatType getMinValue() const noexcept
Definition juce_StatisticsAccumulator.h:92
juce::StatisticsAccumulator::getAverage
FloatType getAverage() const noexcept
Definition juce_StatisticsAccumulator.h:66
juce::StatisticsAccumulator::reset
void reset() noexcept
Definition juce_StatisticsAccumulator.h:60
juce::StatisticsAccumulator::getVariance
FloatType getVariance() const noexcept
Definition juce_StatisticsAccumulator.h:75
juce::StatisticsAccumulator::getCount
size_t getCount() const noexcept
Definition juce_StatisticsAccumulator.h:106
juce::StatisticsAccumulator::addValue
void addValue(FloatType v) noexcept
Definition juce_StatisticsAccumulator.h:45
juce::StatisticsAccumulator::getStandardDeviation
FloatType getStandardDeviation() const noexcept
Definition juce_StatisticsAccumulator.h:84
juce::StatisticsAccumulator::getMaxValue
FloatType getMaxValue() const noexcept
Definition juce_StatisticsAccumulator.h:100