需要使用不同类型的插值? numpy interp1d

时间:2016-04-23 00:57:50

标签: python numpy interpolation linear-interpolation

我有两列信息。第二列是以秒为单位的时间。第一列是当时的错误。我需要制作一个包含2.5秒间隔的错误值的向量,以秒为单位。应该有172个。这是我的数据: col 0 =错误,col 1 =以秒为单位的时间

 array([[0.00, 0.01],
   [1.91, 9.60],
   [0.00, 19.08],
   [2.05, 28.64],
   [1.04, 38.19],
   [1.89, 47.73],
   [1.69, 57.27],
   [2.24, 66.79],
   [1.89, 76.33],
   [1.86, 85.88],
   [2.37, 95.39],
   [2.29, 104.93],
   [2.03, 114.45],
   [2.16, 123.99],
   [1.34, 133.52],
   [2.40, 143.03],
   [2.17, 152.54],
   [0.00, 162.03],
   [1.61, 171.59],
   [2.31, 181.13],
   [2.15, 190.67],
   [2.22, 200.19],
   [2.16, 209.72],
   [0.00, 219.20],
   [2.65, 228.76],
   [1.74, 238.33],
   [0.00, 247.85],
   [2.33, 257.42],
   [1.85, 266.94],
   [0.00, 276.50],
   [2.27, 286.06],
   [1.67, 295.62],
   [2.41, 305.15],
   [0.00, 314.65],
   [1.32, 324.21],
   [2.39, 333.74],
   [2.19, 343.27],
   [2.51, 352.81],
   [2.41, 362.33],
   [1.79, 371.86],
   [0.00, 381.36],
   [3.07, 390.93],
   [2.30, 400.47],
   [0.00, 409.98],
   [2.41, 419.54],
   [2.22, 0.05],
   [1.75, 9.59],
   [2.18, 19.14],
   [1.99, 28.64],
   [1.80, 38.16],
   [1.45, 47.68],
   [1.57, 57.21],
   [2.24, 66.74],
   [0.00, 76.24],
   [2.31, 85.80],
   [0.00, 95.29],
   [2.39, 104.85],
   [0.00, 114.34],
   [0.95, 123.89],
   [2.35, 133.42],
   [2.43, 142.98],
   [1.66, 152.48],
   [1.08, 162.01],
   [0.00, 171.53],
   [1.20, 181.08],
   [2.43, 190.64],
   [2.42, 200.16],
   [2.59, 209.69],
   [1.98, 219.22],
   [1.75, 228.76],
   [2.28, 238.28],
   [1.98, 247.80],
   [1.08, 257.33],
   [2.08, 266.84],
   [2.30, 276.37],
   [0.00, 285.84],
   [1.38, 295.40],
   [2.19, 304.95],
   [0.00, 314.44],
   [1.54, 324.01],
   [2.19, 333.52],
   [0.00, 343.02],
   [2.13, 352.59],
   [2.31, 362.13],
   [0.00, 371.61],
   [2.36, 381.18],
   [2.02, 390.71],
   [2.68, 400.24],
   [0.00, 409.71],
   [2.19, 419.28]])

我尝试使用以下代码使用线性插值器,但得到错误ValueError: A value in x_new is below the interpolation range.

import numpy as np
#import scipy
#import matplotlib.pyplot as plt 
from scipy import interpolate 
float_formatter = lambda x: "%.2f" % x
#np.set_printoptions(formatter={'float_kind':float_formatter})

# Read the text file with the errors - error,time format
orig=np.genfromtxt('Error_Onsets.csv',delimiter=',')
print repr(orig)
# Build a linear interpolator, giving it the known time (X) and error (Y)
interpf = interpolate.interp1d(orig[:,1],orig[:,0],kind='linear')

# What's the TR?
TR=2.5

# Setup the new vector of times, spaced by TRs
new_times=np.arange(0,172*TR,TR)

# Interpolate using the func defined above to get the error at any TR
new_err = interpf(new_times)

我读到这可能是因为x值需要稳定增加才能使线性插值合适。我很感激任何建议。

1 个答案:

答案 0 :(得分:2)

我通常会在没有插值的情况下执行此操作,只使用最新值(因此不会对未来数据进行采样):

times = np.arange(orig[0,1], orig[-1,1], 2.5)
indexes = np.searchsorted(orig[:,1], times, side='right') - 1
np.column_stack((orig[indexes,0], times))

这会给你两列:相隔2.5s的新时间,以及那些时间的最新错误值。