【问题标题】:spline interpolation coefficients of a line curve in 3d space3d 空间中直线曲线的样条插值系数
【发布时间】:2018-04-08 13:47:34
【问题描述】:

我是 python 新手。 我在由一组给定点定义的 3D 空间中有一条线曲线。 谁能建议我如何使用带有 scipy 包的样条函数的插值来获得曲线的样条系数,就像 MATLAB 中的 spline.coeff 函数一样? 谢谢!

编辑:

我用过

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 包含点的坐标

好的,我知道我做错了什么。我的输入点太少了。现在我有另一个问题。函数 get_coeffs 应该在每个不返回样条系数。这些系数按什么顺序返回?我有一个代表结的 79 tx 和 79 ty 的数组,当我调用函数来调用结时,我得到一个 1x5625 的数组

【问题讨论】:

  • 我使用了 tck = interpolate.SmoothBivariateSpline(pts2[:,0],pts2[:,1],pts2[:,2]) 但这显然是用于曲面而不是用于线曲线pts2 是一个 Nx3 numpy 数组,包含点的坐标
  • 您可以使用此信息编辑您的帖子吗?因为它会帮助其他读者
  • codetck = interpolate.SmoothBivariateSpline(pts2[:,0],pts2[:,1],pts2[:,2]) test_pts = pts2[:,2]-tck.ev (pts2[:,0],pts2[:,1]) 打印 test_ptscode
  • 好的,我知道我做错了什么。我的输入点太少了。现在我有另一个问题。函数 get_coeffs 应该在每个不返回样条系数。这些系数按什么顺序返回?我有一个代表结的 79 tx 和 79 ty 数组,当我调用函数来调用结时,我得到一个 1x5625 的数组。
  • 如果您找到了问题的答案,请将其作为答案发布。对于新问题,请打开另一个问题

标签: python interpolation spline


【解决方案1】:

我也是 python 新手,但我最近的搜索让我找到了a very helpful scipy interpolation tutorial。根据我对本文的阅读,我同意 BivariateSpline 系列类/函数旨在用于插值 3D 曲面而不是 3D 曲线。

对于我的 3D 曲线拟合问题(我相信这与您的非常相似,但还希望消除噪音)我最终使用了 scipy.interpolate.splprep(不要与 scipy.interpolate.splrep 混淆)。从上面链接的教程中,您正在寻找的样条系数由 splprep 返回。

正常输出是一个 3 元组 (t,c,k) ,其中包含 节点,t,系数 c 和样条的阶 k。

文档一直将这些过程函数称为“较旧的、非面向对象的 FITPACK 包装”,与“较新的、面向对象的”UnivariateSpline 和 BivariateSpline 类形成对比。我自己更喜欢“更新的、面向对象的”,但据我所知,UnivariateSpline 只处理一维情况,而 splprep 直接处理 N 维数据。

下面是一个简单的测试用例,我用来找出这些功能:

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

【讨论】:

    猜你喜欢
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    • 2017-04-10
    • 2014-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多