【问题标题】:Cubic spline interpolation factors required to pass through original datapoints (scipy, python)通过原始数据点所需的三次样条插值因子(scipy,python)
【发布时间】:2020-09-19 06:55:17
【问题描述】:

我正在使用 scipy.interpolate.interp1d 对信号进行三次样条插值。虽然我认为插值信号应该通过所有原始数据点,但在使用某些因素进行插值时情况并非如此。

例如如果有 N 个样本,样本之间有 N-1 个空格,插值因子为 f,我们可以在样本之间插入 x 个点 N*f == (N-1)*x + N。如果 x 不是整数,插值信号不能通过原始数据点。正如预期的那样,代码使用下面的 scipy,N = 4,插值因子 f 为 3 或 4。

我的问题是 A) 这是正确的还是我做错了什么?和 B) 上面的公式中 x 是一个整数是否足以检查原始数据样本是否会出现在插值信号中(或者可能存在边缘情况)。

非常感谢

import scipy.interpolate
import numpy as np

# produce random data and interp
x = np.linspace(0, 2, 4)
np.random.seed(123)
y = np.random.random(4)

interp_f = scipy.interpolate.interp1d(x, y, kind='cubic')

# upsample factor 4
x_f4 = np.linspace(0, 2, 16)
y_f4 = interp_f(x_f4)

# upsample factor 3
x_f3 = np.linspace(0, 2, 12)
y_f3 = interp_f(x_f3)

print("Sample 2 in raw data: {0:.10f}, Sample 6 in interp f4: {1:.10f}, Sample 4 in interp f3: {2:.10f}".format(y[1], y_f4[5], y_f3[4]))
# Sample 2 in raw data: 0.2861393350, Sample 6 in interp f4: 0.2861393350, Sample 5 in interp f3: 0.2657521625

【问题讨论】:

    标签: python interpolation sampling spline cubic


    【解决方案1】:

    首先,正如您所写,三次插值确实通过其原始点。您可以通过以下方式验证这一点:

    all(y == interp_f(x))  # gives True
    

    您的上采样公式似乎有点混乱。如果我们通过一个例子很容易看出:

    • 假设我们有区间[0, w],其中w = 2
    • 拥有n = 5 样本给出(n-1) 宽度间隔d = w/(n-1) = .5
    • 为了通过因子f=4 上采样,我们将新的区间宽度改为d_4 = d / f = 0.125
    • 因此d_4 = w / (n_4 - 1) 也需要保持不变,其中n_4 是上采样信号的样本数。
    • 利用1 / d_4 = f * (n-1) / w 应该等于(n_4 - 1) / w 导致n_4 = f * (n-1) + 1 = 17

    只要f 是一个正整数,原始样本就会包含在上采样信号中(由于d_4 = d / f)。我们可以通过以下方式验证我们的公式:

    n, f = 5, 4
    n_4 = f * (n-1) + 1
    x = np.linspace(0, 2, n)
    x_4 = np.linspace(0, 2, n_4)
    if all(x_4[::f] == x):  # Every fourth sample is equal to the original
            print("Up-sampling works.")
    

    【讨论】:

      猜你喜欢
      • 2018-12-27
      • 2016-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-31
      相关资源
      最近更新 更多