Python 将数据重新采样到目标网格

Python 将数据重新采样到目标网格,python,list,interpolation,resampling,Python,List,Interpolation,Resampling,有没有有效的方法来解决以下问题 假设我们有这两个列表。它们表示非均匀采样数据: MeasTimes = [0, 2, 4, 6, 8, 11, 12, 14, 18, 20, 22] MeasValues = [-1, 0, 1, 0, -1, 0.5, 1, 0, 0, 1, 0] 如何将测量数据插值到目标网格? 例如: TargetTimes = [0, 5, 10, 15, 18, 20] # given 我认为有时你给出了错误的数据(5和10) 代码是 MeasValuesAtTar

有没有有效的方法来解决以下问题

假设我们有这两个列表。它们表示非均匀采样数据:

MeasTimes = [0, 2, 4, 6, 8, 11, 12, 14, 18, 20, 22]
MeasValues = [-1, 0, 1, 0, -1, 0.5, 1, 0, 0, 1, 0]
如何将测量数据插值到目标网格? 例如:

TargetTimes = [0, 5, 10, 15, 18, 20] # given

我认为有时你给出了错误的数据(5和10)

代码是

MeasValuesAtTargetTimes=[MeasValues[MeasTimes.index(i)] for i in TargetTimes]

您是如何确定“通缉”结果的?通过严格查找,18和20的值没有意义,两个样本之间的值的标准是什么,它们是插值还是使用高ro低?yp jessag,我只是外推。也许我应该删除MeasValuesAtTargetTimes。@dpgomez你插值对吗?例如,从(MeasValues[2]+MeasValues[3])/2中获取MeasValuesAtTargetTimes[2]的值。这就是你正在做的吗?真正的插值将解释时间宽度,而不仅仅是1/2,即10的8-11或15的14-18。Thx funkySayu。这就是我想要的。是的,我注意到我想要的结果实际上是不正确的,这反映了我无法解决问题的事实。因此,为了避免混淆,我把它从问题中删除了。但是,是的,最好是对时间宽度进行更正。NoPy.Inp会考虑“MealValueStaseTime= =(1,0.5,0,0,0,1)”是正确的。@ DPGOMEZ可以更新您的问题,以澄清您正在插值。我没有相同的结果:[-1,0.5,-0.1666666,0.0,0,1]
meas_times = [0, 2, 4, 6, 8, 11, 12, 14, 18, 20, 22]
meas_values = [-1, 0, 1, 0, -1, 0.5, 1, 0, 0, 1, 0]

def get_value(target_value):
    if target_value in meas_times:
        return meas_values[meas_times.index(target_value)]
    else:
        upper_index, upper_time = next((i, e) for i, e in enumerate(meas_times) if e > target_value)
        lower_value, upper_value = meas_values[upper_index - 1], meas_values[upper_index]
        abs_dt = float(abs(lower_value) + abs(upper_value)) / (upper_time - meas_times[upper_index - 1])

        if upper_value > lower_value:
            return lower_value + abs_dt * (target_value - meas_times[upper_index - 1])
        else:
            return lower_value - abs_dt * (target_value - meas_times[upper_index - 1])

target_times = [0, 5, 10, 15, 18, 20]
print map(get_value, target_times)
# [-1, 0.5, 0.0, 0.0, 0, 1]