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

时间:2015-08-13 12:54:53

标签: 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

2 个答案:

答案 0 :(得分:2)

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]

答案 1 :(得分:0)

我认为有时你会提供错误的数据(5和10)

守则

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