簡體   English   中英

三維空間中直線曲線的樣條插值系數

[英]spline interpolation coefficients of a line curve in 3d space

我是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的數組

我也是python的新手,但是我最近的搜索讓我得到了一個非常有用的scipy插值教程 從我的閱讀中我發現,BivariateSpline系列類/函數用於插入3D曲面而不是3D曲線。

對於我的3D曲線擬合問題(我認為它與你的非常相似,但又想要消除噪音)我最終使用了scipy.interpolate.splprep (不要與scipy.interpolate.splrep混淆)。 從上面鏈接的教程中,您正在尋找的樣條系數由splprep返回。

正常輸出是3元組,(t,c,k),包含結點,t,系數c和樣條的階數k。

文檔將這些過程函數稱為“較舊的,非面向對象的FITPACK包裝”,而不是“更新的,面向對象的”UnivariateSpline和BivariateSpline類。 我自己會更喜歡“更新,面向對象”,但據我所知,UnivariateSpline只處理1-D情況,而splprep直接處理ND數據。

下面是一個簡單的測試用例,我用它來弄清楚這些函數:

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

輸出圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM