从平滑数据中采样

时间:2018-09-05 17:49:10

标签: python random smoothing

我有一个非常嘈杂的功能,可以使用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 个答案:

答案 0 :(得分:2)

使用scipy的interp1d函数:

  

x和y是用于近似某些函数f的值的数组:y = f(x)。此类返回一个函数,该函数的调用方法使用插值法查找新点的值。

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])