Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

resample time series on uniform interval in numpy/scipy?

Tags:

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: enter image description here

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?

like image 570
kevinkayaks Avatar asked Sep 18 '19 20:09

kevinkayaks


1 Answers

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)
like image 117
Kyle Pena Avatar answered Oct 08 '22 16:10

Kyle Pena