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

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

最佳答案

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]

关于python - 将数据重新采样到目标网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31988862/

相关文章:

python - 在 python 中加速插值

python - 使用内插新值将 python numpy 数组的长度加倍

python - 如何使用python按特定顺序编号圆圈?

Python - 循环遍历列表并在替换之前保存状态

python - 如何仅对列表的 int 部分执行操作?

Python:为什么执行时会出现这样的输出?

c++ - std::list 与 std::map 属性?

c - 递归函数在C中返回0

python - 在 20 个随机数字列表中找到最常见的一对、三重奏等,玩过 100 次

python - 插值 lambda 难以理解