Wolfram Mathematica

Approximate Signal Derivative

Use MovingMap to approximate the derivative of a signal coming from irregularly sampled continuous time series.

In:= ts = TimeSeries[ Table[{t, EllipticTheta[1, t, 0.3]}, {t, Join[{0.}, RandomReal[{0, 2 Pi}, 254], {2. Pi}]}]]
Out= In:= RegularlySampledQ[ts]
Out= In:= ListPlot[ts, PlotTheme -> "Detailed"]
Out= Use the values and the times at the boundaries of each sliding window to compute the difference quotients.

In:= quotient[values_, times_] := First[Differences[values]/Differences[times]]
In:= mm = MovingMap[quotient[#BoundaryValues, #BoundaryTimes] &, ts, {.01, Right}]
Out= Compare to the theoretical derivative.

In:= prime = D[EllipticTheta[1, t, 0.3], t]
Out= In:= Show[Plot[prime, {t, 0, 2 \[Pi]}, PlotStyle -> Thick, PlotTheme -> "Detailed", PlotLegends -> None], ListPlot[mm, PlotStyle -> Red]]
Out= Use MovingMap with Line in place of the quotient function to create a plot of secant lines that approximate the original time series.

In:= line[yvals_, xvals_] := Line[Transpose[{xvals, yvals}]];
In:= lines = MovingMap[ line[#BoundaryValues, #BoundaryTimes] &, ts, {1.3, Right, {0, 2. \[Pi], .1}}];
In:= Graphics[{Black, lines["Values"]}]
Out= 