平滑插值样条曲线有奇怪的四肢

Smooth interpolating spline has weird extremities

我正在构建一个软件来拒绝一些转得太快的轨迹。

我设法用 scipy splrep 和 splev 平滑了它们,但是我在开始和结束时有一些奇怪的地方,有些点根本不遵循原来的轨迹。

知道它的来源或如何更正它吗?我可以使用 splrep 的 "weight" 参数更正它吗?我通过分析轨迹没有原来的10个第一点和最后一个点来避免这个问题,但这有点烦人...

这是我的代码:

import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate


pointsx = [19.96, 19.45, 18.94, 18.43, 17.92, 17.4, 17.02, 16.51, 16.0, 15.48, 14.97, 14.46, 13.95, 13.56, 13.05, 12.54, 12.03, 11.51, 11.13, 10.62, 9.98, 9.59, 9.08, 8.57, 8.06, 7.67, 7.16, 6.78, 6.27, 5.75, 5.24, 4.86, 4.35, 3.83, 3.32, 2.81, 2.43, 1.91, 1.53, 1.02, 0.51, 0.12, -0.38, -0.76, -1.28, -1.79, -2.17, -2.56, -3.07, -3.45, -3.96, -4.35, -4.86, -5.24, -5.63, -6.01, -6.4, -6.78, -7.29, -7.55, -7.93, -8.44, -8.83, -9.21, -9.59, -9.85, -10.24]
pointsy = [-13.18, -13.05, -13.05, -13.05, -13.05, -13.05, -13.18, -13.18, -13.18, -13.05, -13.05, -13.05, -13.18, -13.18, -13.18, -13.18, -13.18, -13.31, -13.31, -13.31, -13.31, -13.31, -13.31, -13.31, -13.31, -13.31, -13.43, -13.43, -13.56, -13.56, -13.56, -13.56, -13.56, -13.56, -13.69, -13.69, -13.69, -13.82, -13.82, -13.82, -13.95, -13.95, -13.95, -14.08, -14.08, -14.2, -14.2, -14.2, -14.33, -14.46, -14.46, -14.46, -14.59, -14.72, -14.72, -14.84, -14.84, -14.97, -14.97, -15.1, -15.1, -15.23, -15.35, -15.48, -15.61, -15.74, -15.87]
degree = 5
lenx = len(pointsx)
x = np.array(pointsx)
y = np.array(pointsy)
w = range(lenx)
ipl_t = np.linspace(0.0, lenx - degree, 1000)
print y.max(), y.min(), ">", abs(y.max()) - abs(y.min())
smooth = 20
x_tup, fpx, ierx, msgx = interpolate.splrep(w, x, k=degree, full_output=1) 
y_tup, fpy, iery, msgy = interpolate.splrep(w, y, k=degree, per=1,full_output=1, s=smooth, task = 0) 


x_i = interpolate.splev(ipl_t, x_tup)
y_i = interpolate.splev(ipl_t, y_tup)

fg, (ax1, ax2, ax3) = plt.subplots(3, sharex=True, sharey=True)

ax1.plot(pointsx, pointsy, 'k.')
ax1.set_title("original")

ax2.plot(x_i, y_i, 'y.')
ax2.set_title("bsplined smooth=%.2f" % smooth)

ax3.plot(pointsx, pointsy, 'k',  x_i, y_i,'y')
ax3.set_title("both")

plt .show()

结果:

还有一些结果具有更相关的平滑轨迹,但在结尾和开头仍然有相同的差距:Trajectory2

您的 y 值插值具有参数 per=1。这意味着

data points are considered periodic ... and a smooth periodic spline approximation is returned.

显然,您的 y 值根本不是周期性的,因此强制样条曲线具有周期性会使其偏离数据。删除 per 参数可解决问题。