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

◆ relation_lag()

LagRelation MayaFlux::Kinesis::relation_lag ( std::span< const double >  a,
std::span< const double >  b,
long  max_lag 
)
inlinenoexcept

Search a bounded lag range for the offset at which two streams correlate most strongly.

Parameters
aFirst span
bSecond span
max_lagFurthest offset to test in either direction, in samples
Returns
The lag in [-max_lag, max_lag] with the highest absolute correlation, and that correlation as strength

Answers which of two streams leads and by how much, without the caller specifying the offset in advance. Searches both directions since neither stream is privileged: a positive result lag means b lags a, a negative one means a lags b.

Signed strength is kept rather than always reporting the magnitude, so a caller distinguishes streams moving together (positive) from streams moving in exact opposition at the same offset (negative); the best lag is chosen by absolute value since a strong inverse relationship is as informative as a strong direct one.

Bounded rather than full-length for the same reason estimate_period in TemporalMeasures.hpp bounds its period search: the caller stating the range of offsets that could plausibly matter keeps the search from locking onto a coincidental alignment far outside any physically meaningful lag between two related streams.

Definition at line 155 of file Relation.hpp.

157{
158 LagRelation best;
159 for (long lag = -max_lag; lag <= max_lag; ++lag) {
160 const double r = relation_at_lag(a, b, lag);
161 if (std::abs(r) > std::abs(best.strength)) {
162 best.strength = r;
163 best.lag = lag;
164 }
165 }
166 return best;
167}
size_t a
size_t b
double relation_at_lag(std::span< const double > a, std::span< const double > b, long lag) noexcept
Correlation between two spans at a single fixed lag.
Definition Relation.hpp:111
double strength
Correlation at that lag, in -1..1. Near zero means no relation.
Definition Relation.hpp:92
long lag
Samples b is offset from a; positive means b lags a.
Definition Relation.hpp:91
A candidate offset between two streams and how strongly the windows support it.
Definition Relation.hpp:90

References a, b, MayaFlux::Kinesis::LagRelation::lag, relation_at_lag(), and MayaFlux::Kinesis::LagRelation::strength.

+ Here is the call graph for this function: