首页 > 解决方案 > 拟合后如何提取点​​的坐标?

问题描述

我正在使用以下代码来平滑我的数据

a = get_data()
y, x = a.T
t = np.linspace(0, 1, len(x))
t2 = np.linspace(0, 1, len(x))

x2 = np.interp(t2, t, x)
y2 = np.interp(t2, t, y)
sigma = 50
x3 = gaussian_filter1d(x2, sigma)
y3 = gaussian_filter1d(y2, sigma)

x4 = np.interp(t, t2, x3)
y4 = np.interp(t, t2, y3)


plt.plot(x, y, "o-", lw=2)
plt.plot(x3, y3, "r", lw=2)
plt.plot(x4, y4, "o", lw=2)
plt.show()

我在这里找到了这段代码:
python中的线平滑算法?

x我的问题是我需要从新拟合中获得与原始值完全相同x的点(我已经平滑的点)。拟合效果很好,但x新点的值不同。如何从具有相同x值但具有新拟合y值的新拟合中获得积分。点的x值从 0 开始,每个点之间的间距应为 1800。

标签: pythonnumpyscipy

解决方案


我认为您的情况特别需要平滑的数据就像平面中的一条自由线,(x, y) = f(t)而不是一个函数y = f(x)

也许诀窍是必须在插值之前对点进行排序(参见 参考资料numpy.interp):

# Generate random data:
t = np.linspace(0, 3, 20)
x = np.cos(t) + 0.1*np.random.randn(np.size(t))
y = np.sin(t) + 0.1*np.random.randn(np.size(t))

# Smooth the 2D data:
sigma = 2
x_smooth = gaussian_filter1d(x, sigma)
y_smooth = gaussian_filter1d(y, sigma)

# Sort (see: https://stackoverflow.com/a/1903579/8069403) 
permutation = x_smooth.argsort()
x_smooth = x_smooth[permutation]
y_smooth = y_smooth[permutation]

x_new = np.sort(x)  # not mandatory

# Interpolation on the original x points:
y_smooth_new = np.interp(x_new, x_smooth, y_smooth)

# Plot:
plt.plot(x, y, label='x, y');
plt.plot(x_smooth, y_smooth, label='x_smooth, y_smooth');
plt.plot(x_new, y_smooth_new, '-ro', label='x_new, Y_smooth_new', alpha=0.7);
plt.legend(); plt.xlabel('x');

在此处输入图像描述


推荐阅读