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

◆ forward_difference() [1/2]

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

N-th order forward finite difference, expressed on a HistoryBuffer.

Template Parameters
NDifference order
TSample type
Parameters
historyBuffer with capacity >= N + 1
dtElapsed time between consecutive samples
Returns
Forward-difference formula evaluated with the newest sample treated as the base point and older samples as the forward taps

Mathematically the forward and backward difference formulas are the same stencil read in opposite temporal direction. Since a HistoryBuffer only ever exposes past samples relative to [0], this computes the forward-difference coefficients but applied to the same available data as backward_difference. The two differ only for even N in sign convention on alternating terms; provided for callers whose downstream math was derived against forward-difference tables.

Definition at line 82 of file Differential.hpp.

83{
84 static_assert(N >= 1, "forward_difference<N> requires N >= 1");
85
86 using S = scalar_t<T>;
87
88 T acc = T {};
89 double binomial = 1.0;
90
91 for (size_t k = 0; k <= N; ++k) {
92 if (k > 0)
93 binomial = binomial * static_cast<double>(N - k + 1) / static_cast<double>(k);
94 const double sign = ((N - k) % 2 == 0) ? 1.0 : -1.0;
95 acc = acc + static_cast<S>(sign * binomial) * history[k];
96 }
97
98 double denom = 1.0;
99 for (size_t i = 0; i < N; ++i)
100 denom *= dt;
101
102 return acc / static_cast<S>(denom);
103}
#define N(method_name, full_type_name)
Definition Creator.hpp:106
float k

References k, and N.