Convenience overload of backward_difference<N> over a raw span.
Evaluated directly on the span, newest sample first, without building a HistoryBuffer, so it does not allocate and is safe per sample on the audio thread. Samples beyond the end of a span shorter than N + 1 read as zero, the same initial condition a HistoryBuffer starts from.
Definition at line 621 of file Differential.hpp.
622{
623 static_assert(N >= 1, "backward_difference<N> requires N >= 1; N = 0 is the sample itself");
624
625 using S = scalar_t<T>;
626
627 const auto at = [samples](size_t k) -> T { return k < samples.size() ? samples[k] : T {}; };
628
629 T acc = at(0);
630 double binomial = 1.0;
631
632 for (size_t k = 1; k <= N; ++k) {
633 binomial = binomial * static_cast<double>(N - k + 1) / static_cast<double>(k);
634 const double sign = (k % 2 == 0) ? 1.0 : -1.0;
635 acc = acc + static_cast<S>(sign * binomial) * at(k);
636 }
637
638 double denom = 1.0;
639 for (
size_t i = 0; i <
N; ++i)
640 denom *= dt;
641
642 return acc /
static_cast<S>(denom);
643}