三次样条python_三次hermit样条插值python
{{em}在這里舉例說明了{em}在一些給定的維數之間的導數的用法。在import numpy as np
from scipy import interpolate
def sampleCubicSplinesWithDerivative(points, tangents, resolution):
'''
Compute and sample the cubic splines for a set of input points with
optional information about the tangent (direction AND magnitude). The
splines are parametrized along the traverse line (piecewise linear), with
the resolution being the step size of the parametrization parameter.
The resulting samples have NOT an equidistant spacing.
Arguments: points: a list of n-dimensional points
tangents: a list of tangents
resolution: parametrization step size
Returns: samples
Notes: Lists points and tangents must have equal length. In case a tangent
is not specified for a point, just pass None. For example:
points = [[0,0], [1,1], [2,0]]
tangents = [[1,1], None, [1,-1]]
'''
resolution = float(resolution)
points = np.asarray(points)
nPoints, dim = points.shape
# Parametrization parameter s.
dp = np.diff(points, axis=0) # difference between points
dp = np.linalg.norm(dp, axis=1) # distance between points
d = np.cumsum(dp) # cumsum along the segments
d = np.hstack([[0],d]) # add distance from first point
l = d[-1] # length of point sequence
nSamples = int(l/resolution) # number of samples
s,r = np.linspace(0,l,nSamples,retstep=True) # sample parameter and step
# Bring points and (optional) tangent information into correct format.
assert(len(points) == len(tangents))
data = np.empty([nPoints, dim], dtype=object)
for i,p in enumerate(points):
t = tangents[i]
# Either tangent is None or has the same
# number of dimensions as the point p.
assert(t is None or len(t)==dim)
fuse = list(zip(p,t) if t is not None else zip(p,))
data[i,:] = fuse
# Compute splines per dimension separately.
samples = np.zeros([nSamples, dim])
for i in range(dim):
poly = interpolate.BPoly.from_derivatives(d, data[:,i])
samples[:,i] = poly(s)
return samples
為了演示此函數的用法,我們指定點和切線。該示例進一步演示了切線的“幅值”更改時的效果。在
^{pr2}$
結果如下:
有三點需要注意:以下內容也適用于兩個以上的維度。在
樣本之間的間距不是固定的。實現等距采樣的一種簡單方法是在返回的samples之間進行線性插值,正如在this post中所討論的那樣。在
切線的指定是可選的,但是BPoly.from_derivatives不能確保在此位置的樣條曲線之間的平滑過渡。例如,如果上述示例中的tangents[1]設置為None,sampleCubicSplinesWithDerivative(points, tangents, resolution),則結果如下:
總結
以上是生活随笔為你收集整理的三次样条python_三次hermit样条插值python的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数仓中应该出现的所有表格
- 下一篇: websocket python爬虫_p