I have a random variable X
sampled at random times T
similar to this toy data:
import numpy as np
T = np.random.exponential(size=1000).cumsum()
X = np.random.normal(size=1000)
This timeseries looks like this:
A key point is that the sampling interval is non-uniform: by this I mean that all elements of np.diff(T)
are not equal. I need to resample the timeseries T,X
on uniform intervals with a specified width dt
, meaning (np.diff(T)==dt).all()
should return True
.
I can resample the timeseries on uniform intervals using scipy.interpolate.interp1d
, but this method does not allow me to specify the interval size dt
:
from scipy.interpolate import interp1d
T = np.linspace(T.min(),T.max(),T.size) # same range and size with a uniform interval
F = interp1d(T,X,fill_value='extrapolate') # resample the series on uniform interval
X = F(T) # Now it's resampled.
The essential issue is that interp1d
does not accept an array T
unless T.size==X.size
.
Is there another method I can try to resample the time series T,X
on uniform intervals of width dt
?
dt = ...
from scipy.interpolate import interp1d
Told = np.arange(T.min(),T.max(),T.size)
F = interp1d(Told,X,fill_value='extrapolate')
Tnew = np.linspace(T.min(), T.max(), dt)
Xnew = F(Tnew)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With