I want to get delta coefficients calculated. The definition can be found here. However, I don't know what to do with the minus indices. I have tried computing it in a slightly different way, namely:

delta[data_] :=
        nData = Most[Prepend[data, 0]],
        l = Length[data],

        Plus @@ Times[data - nData, Range[l]],
        Plus @@ (data ^ 2)

My question is, whether there's other, easier way, of computing the delta coefficients?

  • $\begingroup$ Would you please also provide an example dataset data. I presume that data is a vector of numbers but who knows? (Right, it is only you.) $\endgroup$ – Henrik Schumacher Mar 19 at 10:39
  • 1
    $\begingroup$ Hi, data is a vector of numbers. E.g. data ={0.0995563,-0.002073,-0.0541243,0.0662532,0.153794,0.0654639,0.158421,0.0362688,0.113608,0.156917,0.114574,0.0606766,0.0106301} $\endgroup$ – Curtis Mar 19 at 10:46

You may use the following; it is concise, accurate, and fast.

delta1[data_] := With[{a = Developer`ToPackedArray[N[data]]},
  ListConvolve[{1., -1.}, a, 1, 0.].Range[N[Length[a]]]/Total[a^2]

n = 10^5;
data = RandomReal[{-10, 10}, n];
r1 = delta[data]; // RepeatedTiming // First
r2 = delta1[data]; // RepeatedTiming // First
Max[Abs[r1 - r2]]




A general suggestion for performance: Better apply N before any actual computations are performed. (This rule holds only as long as the computations are free of any issues about severe precision loss.)

Should you decide to use cyclic differences, then replace

ListConvolve[{1., -1.}, a, 1, 0.]


ListConvolve[{1., -1.}, a, 1]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.