MayaFlux 0.5.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ correlation()

double MayaFlux::Kinesis::correlation ( std::span< const double >  a,
std::span< const double >  b 
)
inlinenoexcept

Pearson correlation between two equal-length windows.

Parameters
aFirst span
bSecond span, same length as a
Returns
Correlation in -1..1, or zero if either span has near-zero variance or the lengths differ

The base relation measure: do these two streams move together, apart, or independently, over this window, with no notion of one leading the other. Scale-free, so a pressure axis in 0..1 and a tilt axis in degrees compare meaningfully without the caller normalizing first.

Definition at line 45 of file Relation.hpp.

47{
48 if (a.size() != b.size() || a.size() < 2)
49 return 0.0;
50
51 const size_t n = a.size();
52 double mean_a = 0.0;
53 double mean_b = 0.0;
54 for (size_t i = 0; i < n; ++i) {
55 mean_a += a[i];
56 mean_b += b[i];
57 }
58 mean_a /= static_cast<double>(n);
59 mean_b /= static_cast<double>(n);
60
61 double cov = 0.0;
62 double var_a = 0.0;
63 double var_b = 0.0;
64 for (size_t i = 0; i < n; ++i) {
65 const double da = a[i] - mean_a;
66 const double db = b[i] - mean_b;
67 cov += da * db;
68 var_a += da * da;
69 var_b += db * db;
70 }
71
72 const double denom = std::sqrt(var_a * var_b);
73 if (denom < 1e-12)
74 return 0.0;
75
76 return std::clamp(cov / denom, -1.0, 1.0);
77}
size_t a
size_t b

References a, and b.

Referenced by coherence(), estimate_period(), and relation_at_lag().

+ Here is the caller graph for this function: