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

时间:2017-11-22 14:49:37

标签: python scipy spline smoothing bspline

我正在建立一个软,以拒绝一些转得太快的轨迹。

我设法用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()

结果:trajectory

其他一些结果具有更相关的平滑轨迹,但在结束和开始时仍有相同的差距:Trajectory2

1 个答案:

答案 0 :(得分:0)

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

  

数据点被认为是周期性的...并且返回平滑的周期样条近似。

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

per=0