Skip to content

Instantly share code, notes, and snippets.

@ptweir
Created March 22, 2014 00:41
Show Gist options
  • Save ptweir/9699392 to your computer and use it in GitHub Desktop.
Save ptweir/9699392 to your computer and use it in GitHub Desktop.
scripts to deal with data acquired at different (or non-uniform) sample rates.
import numpy as np
import matplotlib.pyplot as plt
plt.ion()
import scipy.interpolate
def interpolate_to_common_samplerate(Y, X=None, t=None):
if t is None:
if X is None:
t = np.arange(len(Y[0]),dtype='float')
else:
t = X[0]
interpolatedY = np.zeros((len(t),len(Y)),dtype='float')
for i, y in enumerate(Y):
if X is None:
x = np.linspace(t[0],t[-1],len(y),endpoint=True)
else:
x = X[i]
sortedInds = np.argsort(x)
x = x[sortedInds]
y = y[sortedInds]
f = scipy.interpolate.interp1d(x, y, bounds_error=False)
interpolatedY[:,i] = f(t)
return interpolatedY, t
T=1000
n1=100
t1=np.linspace(0,T,n1,endpoint=True)+np.random.rand(n1)*10
t1[-1]=T
y1=np.random.rand(n1)+t1/20.
plt.plot(t1,y1,'.k')
n2=90
t2=np.linspace(0,T,n2,endpoint=True)
y2=np.random.rand(n2)+t2/20.+1
plt.plot(t2,y2,'.b')
n3=80
t3=np.linspace(0,T,n3,endpoint=True)
y3=np.random.rand(n3)+t3/20.+2
plt.plot(t3,y3,'.g')
#ys, t = interpolate_to_common_samplerate([y1, y2, y3])
#plt.plot(t,ys)
#ys, t = interpolate_to_common_samplerate([y1, y2, y3],t=np.arange(0,1000))
#plt.plot(t,ys)
ys, t = interpolate_to_common_samplerate([y1, y2, y3],[t1, t2, t3],t=np.linspace(0,T,10000,endpoint=True))
plt.plot(t,ys)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment