ktk.TimeSeries.resample

ktk.TimeSeries.resample#

TimeSeries.resample(target, kind='linear', *, extrapolate=False, in_place=False, **kwargs)[source]#

Resample the TimeSeries.

Resample every data of the TimeSeries over a new frequency or new series of times, using the interpolation method provided by parameter kind. This method does not fill missing data. Consequently, time ranges with nans in the original TimeSeries will also contain nans in the resampled TimeSeries.

Parameters:
  • target (ArrayLike | float) – To resample to a target frequency, use a float that represents the sample rate of the output TimeSeries, in Hz. To resample to specific times, use an array of float that will become the time property of the output TimeSeries.

  • kind (str) – Optional. The interpolation method. This input may take any value supported by scipy.interpolate.interp1d, such as “linear”, “nearest”, “zero”, “slinear”, “quadratic”, “cubic”, “previous”, “next”. Additionally, kind can be “pchip”. Default is “linear”.

  • extrapolate (bool) – Optional. True to extrapolate outside the original time range. Default is False.

  • in_place (bool) – Optional. True to modify and return the original TimeSeries. False to return a modified copy of the TimeSeries while leaving the original TimeSeries intact. Default is False.

Returns:

The TimeSeries with a new sample rate.

Return type:

TimeSeries

Caution

Attempting to resample a series of homogeneous matrices would likely produce non-homogeneous matrices, and as a result, transforms would not be rigid anymore. This function can’t detect if you attempt to resample series of homogeneous matrices, and therefore won’t generate an error or warning.

Examples

>>> ts = ktk.TimeSeries(time=np.arange(10.))
>>> ts = ts.add_data("data", ts.time ** 2)
>>> ts.time
array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
>>> ts.data["data"]
array([ 0.,  1.,  4.,  9., 16., 25., 36., 49., 64., 81.])

Example 1: Resampling at 2 Hz

>>> ts1 = ts.resample(2.0)
>>> ts1.time
array([0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. , 5.5, 6. , 6.5, 7. , 7.5, 8. , 8.5, 9. ])
>>> ts1.data["data"]
array([ 0. ,  0.5,  1. ,  2.5,  4. ,  6.5,  9. , 12.5, 16. , 20.5, 25. , 30.5, 36. , 42.5, 49. , 56.5, 64. , 72.5, 81. ])

Example 2: Resampling on new times

>>> ts2 = ts.resample([0.0, 0.5, 1.0, 1.5, 2.0])
>>> ts2.time
array([0. , 0.5, 1. , 1.5, 2. ])
>>> ts2.data["data"]
array([0. , 0.5, 1. , 2.5, 4. ])

Example 3: Resampling at 2 Hz with missing data in the original ts

>>> ts.data["data"][[0, 1, 5, 8, 9]] = np.nan
>>> ts.data["data"]
array([nan, nan,  4.,  9., 16., nan, 36., 49., nan, nan])
>>> ts3 = ts.resample(2.0)
>>> ts3.time
array([0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. , 5.5, 6. , 6.5, 7. , 7.5, 8. , 8.5, 9. ])
>>> ts3.data["data"]
array([ nan,  nan,  nan,  nan,  4. ,  6.5,  9. , 12.5, 16. ,  nan,  nan, nan, 36. , 42.5, 49. ,  nan,  nan,  nan,  nan])