ktk.filters.deriv#

deriv(ts, /, n=1)[source]#

Calculate the nth numerical derivative.

Filtering occurs on the first axis (time). The sample rate must be constant.

Parameters
  • ts (TimeSeries) – Input timeseries

  • n (int) – Order of the derivative.

Returns

A copy of the input TimeSeries, which each data being derived. The length of the resulting TimeSeries is one less than ts.

Return type

TimeSeries

Raises

ValueError – If sample rate is not constant, or if there is no data to filter.

Example

>>> ts = ktk.TimeSeries(time=np.arange(0, 0.5, 0.1))
>>> ts = ts.add_data("test", np.array([0.0, 0.0, 1.0, 1.0, 0.0]))
>>> # Source data
>>> ts.time
array([0. , 0.1, 0.2, 0.3, 0.4])
>>> ts.data["test"]
array([0., 0., 1., 1., 0.])
>>> # First derivative
>>> ts1 = ktk.filters.deriv(ts)
>>> ts1.time
array([0.05, 0.15, 0.25, 0.35])
>>> ts1.data["test"]
array([  0.,  10.,   0., -10.])
>>> # Second derivative
>>> ts2 = ktk.filters.deriv(ts, n=2)
>>> ts2.time
array([0.1, 0.2, 0.3])
>>> ts2.data["test"]
array([ 100., -100., -100.])