问题描述
我是python的新手.我在3D空间中有一条由一组给定点定义的线曲线.谁能建议我如何使用scipy包的样条函数进行插值来获取曲线的样条系数,就像MATLAB中的spline.coeff函数一样?谢谢!
I am new to python.I have a line curve in the 3D space defined by a set of given points.Can anyone suggest how I can use the interpolate with spline functions of the scipy package to get the spline coefficients of the curve just like the spline.coeff function in MATLAB?Thank you!
我用过
tck = interpolate.SmoothBivariateSpline(pts2[:,0], pts2[:,1], pts2[:,2])
test_pts = pts2[:,2]-tck.ev(pts2[:,0], pts2[:,1])
print test_pts
但这显然是用于表面的,而不是用于直线曲线的.pts2
是包含点坐标的Nx3 numpy array
but this is for surfaces apparently and not for line curves pts2
is a Nx3 numpy array
containing the coordinates of the points
好的,我弄清楚我做错了什么.我的输入点太少了.现在我有另一个问题.函数get_coeffs应该返回所有样条系数.这些系数按什么顺序返回?我有一个79 tx和79 ty的数组代表结,当我调用函数来调用结时,得到的数组是1x5625
ok I figured out what I was doing wrong. my input points where too few. now I have another question. The function get_coeffs is supposed to return the spline coefficients at every not. In which order those coefficients are returned? I have an array of 79 tx and 79 ty which represent the knots and I get an array of 1x5625 when I call the function to call the knots
推荐答案
我也是python的新手,但是最近的搜索使我转向非常有用的scipy内插教程.从我的阅读中,我同意BivariateSpline系列的类/函数旨在用于插值3D曲面而不是3D曲线.
I too am new to python, but my recent searching led me to a very helpful scipy interpolation tutorial. From my reading of this I concur that the BivariateSpline family of classes/functions are intended for interpolating 3D surfaces rather than 3D curves.
对于我的3D曲线拟合问题(我认为这与您的问题非常相似,但还希望消除噪声),最终我使用了 scipy.interpolate.splprep (不要与scipy.interpolate混淆. splrep).在上面链接的教程中,所需的样条系数由splprep返回.
For my 3D curve fitting problem (which I believe is very similar to yours, but with the addition of wanting to smooth out noise) I ended up using scipy.interpolate.splprep (not to be confused with scipy.interpolate.splrep). From the tutorial linked above, the spline coefficients your are looking for are returned by splprep.
与较新的,面向对象的" UnivariateSpline和BivariateSpline类相比,文档始终将这些过程函数称为"FITPACK的较旧的,非面向对象的包装".我本来希望使用更新的,面向对象的",但据我所知UnivariateSpline仅处理一维情况,而splprep直接处理N-D数据.
The docs keep referring to these procedural functions as an "older, non object-oriented wrapping of FITPACK" in contrast to the "newer, object-oriented" UnivariateSpline and BivariateSpline classes. I would have preferred "newer, object-oriented" myself, but as far as I can tell UnivariateSpline only handles the 1-D case whereas splprep handles N-D data directly.
下面是我用来弄清楚这些功能的简单测试用例:
Below is a simple test-case that I used to figure out these functions:
import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate
from mpl_toolkits.mplot3d import Axes3D
# 3D example
total_rad = 10
z_factor = 3
noise = 0.1
num_true_pts = 200
s_true = np.linspace(0, total_rad, num_true_pts)
x_true = np.cos(s_true)
y_true = np.sin(s_true)
z_true = s_true/z_factor
num_sample_pts = 80
s_sample = np.linspace(0, total_rad, num_sample_pts)
x_sample = np.cos(s_sample) + noise * np.random.randn(num_sample_pts)
y_sample = np.sin(s_sample) + noise * np.random.randn(num_sample_pts)
z_sample = s_sample/z_factor + noise * np.random.randn(num_sample_pts)
tck, u = interpolate.splprep([x_sample,y_sample,z_sample], s=2)
x_knots, y_knots, z_knots = interpolate.splev(tck[0], tck)
u_fine = np.linspace(0,1,num_true_pts)
x_fine, y_fine, z_fine = interpolate.splev(u_fine, tck)
fig2 = plt.figure(2)
ax3d = fig2.add_subplot(111, projection='3d')
ax3d.plot(x_true, y_true, z_true, 'b')
ax3d.plot(x_sample, y_sample, z_sample, 'r*')
ax3d.plot(x_knots, y_knots, z_knots, 'go')
ax3d.plot(x_fine, y_fine, z_fine, 'g')
fig2.show()
plt.show()
这篇关于3d空间中线曲线的样条插值系数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!