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

◆ backward_difference() [1/2]

template<size_t N, typename T >
T MayaFlux::Kinesis::backward_difference ( const Memory::HistoryBuffer< T > &  history,
double  dt 
)
inlinenoexcept

N-th order backward finite difference of a HistoryBuffer, scaled by dt^N.

Template Parameters
NDifference order. 1 velocity, 2 acceleration, 3 jerk, 4 jounce, 5 crackle, 6 pop.
TSample type. Must support operator+, operator-, and multiplication/division by scalar_t<T>.
Parameters
historyBuffer with capacity >= N + 1, [0] newest through [N] oldest used
dtElapsed time between consecutive samples, assumed uniform across all N intervals
Returns
sum_{k=0}^{N} (-1)^k * C(N,k) * history[k], divided by dt^N

Named wrappers below (velocity, acceleration, jerk, jounce, crackle, pop) are instantiations of this at fixed N. A caller needing N = 7 or higher calls backward_difference<7>(history, dt) directly.

dt == 0 is not guarded: the correct response depends on the caller's domain, so this stays a pure computation.

Uniform dt across all N intervals is an assumption, not a check. HistoryBuffer carries no per-sample timestamps.

HistoryBuffer::operator[] indexes modulo capacity, not modulo the number of samples actually pushed. Querying history[k] before k samples have been pushed reads the zero initial condition.

Definition at line 42 of file Differential.hpp.

43{
44 static_assert(N >= 1, "backward_difference<N> requires N >= 1; N = 0 is the sample itself");
45
46 using S = scalar_t<T>;
47
48 T acc = history[0];
49 double binomial = 1.0;
50
51 for (size_t k = 1; k <= N; ++k) {
52 binomial = binomial * static_cast<double>(N - k + 1) / static_cast<double>(k);
53 const double sign = (k % 2 == 0) ? 1.0 : -1.0;
54 acc = acc + static_cast<S>(sign * binomial) * history[k];
55 }
56
57 double denom = 1.0;
58 for (size_t i = 0; i < N; ++i)
59 denom *= dt;
60
61 return acc / static_cast<S>(denom);
62}
#define N(method_name, full_type_name)
Definition Creator.hpp:106
float k

References k, and N.