JPL Spatial
Sound spatialization and propagation library
Loading...
Searching...
No Matches
Variance.h
Go to the documentation of this file.
1//
2// ██╗██████╗ ██╗ ██╗██████╗ ███████╗
3// ██║██╔══██╗ ██║ ██║██╔══██╗██╔════╝ ** JPLSpatial **
4// ██║██████╔╝ ██║ ██║██████╔╝███████╗
5// ██ ██║██╔═══╝ ██║ ██║██╔══██╗╚════██║ https://github.com/Jaytheway/JPLSpatial
6// ╚█████╔╝██║ ███████╗██║██████╔╝███████║
7// ╚════╝ ╚═╝ ╚══════╝╚═╝╚═════╝ ╚══════╝
8//
9// Copyright 2024 Jaroslav Pevno, JPLSpatial is offered under the terms of the ISC license:
10//
11// Permission to use, copy, modify, and/or distribute this software for any purpose with or
12// without fee is hereby granted, provided that the above copyright notice and this permission
13// notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
14// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
16// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
17// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
18// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20#pragma once
21
22#include <JPLSpatial/Core.h>
25
26#include <span>
27#include <cstdint>
28
29namespace JPL
30{
31 //==========================================================================
33 template<class T>
35 {
36 public:
37 // Welford’s online variance algorithm
38 struct Online
39 {
41 T Mean = T(0.0);
42 T M2 = T(0.0);
43
44 inline constexpr void Add(T x) noexcept
45 {
46 ++Count;
47 const T delta = x - Mean;
48 Mean += delta / Count;
49 const T delta2 = x - Mean;
50 M2 += delta * delta2;
51 }
52
53 inline constexpr T GetVariance() const noexcept
54 {
55 return (Count > 1) ? (M2 / (Count - 1)) : T(0);
56 }
57
58 inline constexpr T GetSNR() const noexcept
59 {
60 if (Count <= 1)
61 return 0.0f; // Not enough data
62
63 const T variance = (M2 / (Count - 1));
64 return variance == T(0) ? T(1) : Mean / Math::Sqrt(variance);
65 }
66
67 inline constexpr T GetSNR_r0() const noexcept
68 {
69 if (Count <= 1)
70 return T(0); // Not enough data
71
72 const T variance = (M2 / (Count - 1));
73 return variance == T(0) ? T(0) : Mean / Math::Sqrt(variance);
74 }
75 };
76
77 static inline constexpr T ComputeFor(std::span<const T> values) noexcept
78 {
79 const auto count = static_cast<uint32_t>(values.size());
80 if (count <= 1)
81 return T(0); // Not enough samples to compute variance
82
83 T sum = T(0);
84 for (T v : values)
85 sum += v;
86
87 const T mean = sum / count;
88
89 T sumSq = T(0);
90 for (T v : values)
91 sumSq += (v - mean) * (v - mean);
92
93 return sumSq / (count - 1);
94 }
95
96 static inline constexpr T ComputeSNRFor(std::span<const T> values) noexcept
97 {
98 const auto count = static_cast<uint32_t>(values.size());
99 if (count <= 1)
100 return T(0);
101
102 T sum = T(0);
103 for (T v : values)
104 sum += v;
105
106 const T mean = sum / count;
107
108 T sumSq = T(0);
109 for (T v : values)
110 sumSq += (v - mean) * (v - mean);
111
112 const T variance = sumSq / (count - 1);
113
114 return variance == T(0) ? T(1) : mean / Math::Sqrt(variance);
115 }
116 };
117
120} // namespace JPL
Variance estimation utility.
Definition Variance.h:35
static constexpr T ComputeSNRFor(std::span< const T > values) noexcept
Definition Variance.h:96
static constexpr T ComputeFor(std::span< const T > values) noexcept
Definition Variance.h:77
JPL_INLINE constexpr T Sqrt(T x) noexcept
Definition Math.h:269
Definition AcousticMaterial.h:36
typename Variance< simd >::Online OnlineVarianceSIMD
Definition Variance.h:119
typename Variance< float >::Online OnlineVariance
Definition Variance.h:118
JPL_INLINE simd min(const simd &a, const simd &b) noexcept
Element-wise min.
Definition SIMD.h:1813
Definition Variance.h:39
T Mean
Definition Variance.h:41
T M2
Definition Variance.h:42
constexpr void Add(T x) noexcept
Definition Variance.h:44
constexpr T GetSNR_r0() const noexcept
Definition Variance.h:67
constexpr T GetSNR() const noexcept
Definition Variance.h:58
uint32_t Count
Definition Variance.h:40
constexpr T GetVariance() const noexcept
Definition Variance.h:53