如何快速估算Python中点与双三次样条曲面之间的距离?是否存在我可以在SciPy,NumPy或其他软件包中使用的现有解决方案?
我有一个双三次插值定义的表面,如下所示:
import numpy as np
import scipy.interpolate
# Define regular grid surface
xmin,xmax,ymin,ymax = 25, 125, -50, 50
x = np.linspace(xmin,xmax, 201)
y = np.linspace(ymin,ymax, 201)
xx, yy = np.meshgrid(x, y)
z_ideal = ( xx**2 + yy**2 ) / 400
z_ideal += z_ideal + np.random.uniform(-0.5, 0.5, z_ideal.shape)
s_ideal = scipy.interpolate.interp2d(x, y, z_ideal, kind='cubic')
Run Code Online (Sandbox Code Playgroud)
而且我有一些表面的测量点:
# Fake some measured points on the surface
z_measured = z_ideal + np.random.uniform(-0.1, 0.1, z_ideal.shape)
s_measured = scipy.interpolate.interp2d(x, y, z_measured, kind='cubic')
p_x = np.random.uniform(xmin,xmax,10000)
p_y …Run Code Online (Sandbox Code Playgroud) 我正在使用scipy.interpolate.UnivariateSpline平滑插值大量数据.效果很好.我得到一个像函数一样的对象.
现在我想保存样条点以便稍后在Matlab中使用它们(还有Python,但这不那么紧急),而不需要原始数据.我怎样才能做到这一点?
在scipy我不知道; UnivariateSpline似乎没有提供具有先前计算的结和系数的构造函数.
在MATLAB中,我尝试了MATLAB函数spline()和pchip(),并同时兼具接近,他们看起来有点像端点附近错误吉布斯耳朵.
以下是Matlab格式的一组示例数据:
splinedata = struct('coeffs',[-0.0412739180955273 -0.0236463479425733 0.42393753107602 -1.27274336116436 0.255711720888164 1.93923263846732 -2.30438927604816 1.02078680231079 0.997156858475075 -2.35321792387215 0.667027554745454 0.777918416623834],...
'knots',[0 0.125 0.1875 0.25 0.375 0.5 0.625 0.75 0.875 0.9999],...
'y',[-0.0412739180955273 -0.191354308450615 -0.869601364377744 -0.141538578624065 0.895258135865578 -1.04292294390242 0.462652465278345 0.442550440125204 -1.03967756446455 0.777918416623834])
Run Code Online (Sandbox Code Playgroud)
系数和节点是调用get_coeffs()和get_knots()scipy UnivariateSpline的结果.'y'值是结的单变量线的值,或者更确切地说:
y = f(f.get_knots())
Run Code Online (Sandbox Code Playgroud)
其中f是我的UnivariateSpline.
如何使用此数据制作与UnivariateSpline行为匹配的样条线,而无需使用曲线拟合工具箱?我不需要在Matlab中进行任何数据拟合,我只需要知道如何根据knots/coefficient /样条值构造三次样条.
假设我有
import numpy as np
from scipy.interpolate import UnivariateSpline
# "true" data; I don't know this function
x = np.linspace(0, 100, 1000)
d = np.sin(x * 0.5) + 2 + np.cos(x * 0.1)
# sample data; that's what I actually measured
x_sample = x[::20]
d_sample = d[::20]
# fit spline
s = UnivariateSpline(x_sample, d_sample, k=3, s=0.005)
plt.plot(x, d)
plt.plot(x_sample, d_sample, 'o')
plt.plot(x, s(x))
plt.show()
Run Code Online (Sandbox Code Playgroud)
我得到
我现在想要的是所有橙色点之间的功能,所以像
knots = s.get_knots()
f0 = <some expression> for knots[0] <= x < knots[1]
f1 …Run Code Online (Sandbox Code Playgroud) 我正在使用 Scipy 的 SmoothBivariateSpline 类在双变量数据上创建三次 B 样条。我现在需要为这条样条曲线编写分段多项式表达式。
我的数学背景不是很强,所以我无法编写自己的算法来将 SmoothBivariateSpline 的 t、c、k 输出转换为多项式表示。如果这是可行的,您能否提供有关如何解决此问题的指示?我注意到 Scipy 有 interpolate.ppform,但我找不到它的任何文档 - 这是否相关?
我正在考虑的一种方法是将样条的域分解为每个结的区域((n-1)^2总区域,其中n是结的数量),然后对每个区域中样条曲线上的许多点执行三次回归以计算对每个区域的数据进行三次回归。这是一种有效的方法吗?
前一种方法似乎更严格,所以我更喜欢使用那种方法,但后者也可以接受。