代码之家  ›  专栏  ›  技术社区  ›  Gabriel

平滑数据样本

  •  4
  • Gabriel  · 技术社区  · 7 年前

    How to smooth a curve in the right way? )使用 scipy.signal.savgol_filter . 原始数据和平滑数据如下所示(分别为蓝色和红色):

    enter image description here

    问题是我需要 从平滑后的数据来看,也就是说:我需要在任意点上计算红色曲线 x 价值观。这个 savgol_filter 函数只返回一个值数组,而不是我可以计算的函数。

    什么是 最快的 如何做到这一点(它将被抽样数百万次)?


    MVCE公司

    import numpy as np
    from scipy.signal import savgol_filter
    import matplotlib.pyplot as plt
    
    # Noisy data
    y = np.array([-5715.75, -5592.3 , -5548.33, -5638.97, -5586.43, -5703.21,
           -5660.6 , -5714.96, -5637.59, -5599.72, -5631.14, -5684.31,
           -5586.08, -5617.43, -5629.58, -5530.08, -5540.53, -5475.53,
           -5505.21, -5500.96, -5500.58, -5474.65, -5462.45, -5443.82,
           -5441.77, -5463.53, -5512.18, -5395.85, -5389.87, -5432.94,
           -5366.31, -5284.45, -5176.52, -5221.89, -5182.52, -5084.92,
           -5084.3 , -4972.78, -4968.32, -4818.19, -4789.56, -4872.02,
           -4809.45, -4855.06, -4806.77, -4717.93, -4741.29, -4822.45,
           -4760.51, -4698.31, -4744.1 , -4797.08, -4777.43, -4785.02,
           -4687.61, -4820.73, -4753.5 , -4777.99, -4812.5 , -4856.53,
           -4859.69, -4905.37, -4838.71, -5058.49, -5053.58, -5057.  ,
           -5159.58, -5155.03, -5079.21, -5228.57, -5257.26, -5409.64,
           -5505.87, -5511.82, -5471.4 , -5478.47, -5530.9 , -5578.88,
           -5705.87, -5633.66, -5740.72, -5760.05, -5801.39, -5808.52,
           -5803.22, -5832.76, -5867.51, -5837.56, -5923.97, -5933.75,
           -5945.04, -5932.16, -5909.68, -5951.29, -5958.6 , -5958.07,
           -5970.75, -5931.93, -5947.53, -5956.36])
    x = np.linspace(0., 6, 100)
    
    # Smoothed data
    yhat = savgol_filter(y, 51, 3)
    
    plt.plot(x, y)
    plt.plot(x, yhat, color='r')
    plt.show()
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Alexander    7 年前

    使用scipy的 interp1d

    from scipy.interpolate import interp1d
    
    y_ = interp1d(x, yhat)
    
    new_x_vals = np.array([0.0001, 1.011, 2.022, 3.033, 4.044])
    >>> y_(new_x_vals)
    array([-5590.20368685, -5576.9338028 , -5140.41553793, -4749.82520031,
           -5153.81189525])
    
    推荐文章