OpenShot Library | libopenshot-audio  0.2.0
juce_StatisticsAccumulator.h
1 
2 /** @weakgroup juce_core-maths
3  * @{
4  */
5 /*
6  ==============================================================================
7 
8  This file is part of the JUCE library.
9  Copyright (c) 2017 - ROLI Ltd.
10 
11  JUCE is an open source library subject to commercial or open-source
12  licensing.
13 
14  The code included in this file is provided under the terms of the ISC license
15  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
16  To use, copy, modify, and/or distribute this software for any purpose with or
17  without fee is hereby granted provided that the above copyright notice and
18  this permission notice appear in all copies.
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 
30 //==============================================================================
31 /**
32  A class that measures various statistics about a series of floating point
33  values that it is given.
34 
35  @tags{Core}
36 */
37 template <typename FloatType>
39 {
40 public:
41  //==============================================================================
42  /** Constructs a new StatisticsAccumulator. */
43  StatisticsAccumulator() = default;
44 
45  //==============================================================================
46  /** Add a new value to the accumulator.
47  This will update all running statistics accordingly.
48  */
49  void addValue (FloatType v) noexcept
50  {
51  jassert (juce_isfinite (v));
52 
53  sum += v;
54  sumSquares += v * v;
55  ++count;
56 
57  if (v > maximum) maximum = v;
58  if (v < minimum) minimum = v;
59  }
60 
61  /** Reset the accumulator.
62  This will reset all currently saved statistcs.
63  */
64  void reset() noexcept { *this = StatisticsAccumulator<FloatType>(); }
65 
66  //==============================================================================
67  /** Returns the average (arithmetic mean) of all previously added values.
68  If no values have been added yet, this will return zero.
69  */
70  FloatType getAverage() const noexcept
71  {
72  return count > 0 ? sum / (FloatType) count
73  : FloatType();
74  }
75 
76  /** Returns the variance of all previously added values.
77  If no values have been added yet, this will return zero.
78  */
79  FloatType getVariance() const noexcept
80  {
81  return count > 0 ? (sumSquares - sum * sum / (FloatType) count) / (FloatType) count
82  : FloatType();
83  }
84 
85  /** Returns the standard deviation of all previously added values.
86  If no values have been added yet, this will return zero.
87  */
88  FloatType getStandardDeviation() const noexcept
89  {
90  return std::sqrt (getVariance());
91  }
92 
93  /** Returns the smallest of all previously added values.
94  If no values have been added yet, this will return positive infinity.
95  */
96  FloatType getMinValue() const noexcept
97  {
98  return minimum;
99  }
100 
101  /** Returns the largest of all previously added values.
102  If no values have been added yet, this will return negative infinity.
103  */
104  FloatType getMaxValue() const noexcept
105  {
106  return maximum;
107  }
108 
109  /** Returns how many values have been added to this accumulator. */
110  size_t getCount() const noexcept
111  {
112  return count;
113  }
114 
115 private:
116  //==============================================================================
117  struct KahanSum
118  {
119  KahanSum() = default;
120  operator FloatType() const noexcept { return sum; }
121 
122  void JUCE_NO_ASSOCIATIVE_MATH_OPTIMISATIONS operator+= (FloatType value) noexcept
123  {
124  FloatType correctedValue = value - error;
125  FloatType newSum = sum + correctedValue;
126  error = (newSum - sum) - correctedValue;
127  sum = newSum;
128  }
129 
130  FloatType sum{}, error{};
131  };
132 
133  //==============================================================================
134  size_t count { 0 };
135  KahanSum sum, sumSquares;
136  FloatType minimum { std::numeric_limits<FloatType>::infinity() },
137  maximum { -std::numeric_limits<FloatType>::infinity() };
138 };
139 
140 } // namespace juce
141 
142 /** @}*/
FloatType getStandardDeviation() const noexcept
Returns the standard deviation of all previously added values.
FloatType getMinValue() const noexcept
Returns the smallest of all previously added values.
void addValue(FloatType v) noexcept
Add a new value to the accumulator.
FloatType getVariance() const noexcept
Returns the variance of all previously added values.
A class that measures various statistics about a series of floating point values that it is given...
void reset() noexcept
Reset the accumulator.
FloatType getAverage() const noexcept
Returns the average (arithmetic mean) of all previously added values.
StatisticsAccumulator()=default
Constructs a new StatisticsAccumulator.
size_t getCount() const noexcept
Returns how many values have been added to this accumulator.
FloatType getMaxValue() const noexcept
Returns the largest of all previously added values.