三次 Hermite 样条插值 python

2024-01-10

我想计算一个三次多项式,该多项式由其函数值和指定点的导数定义。

https://en.wikipedia.org/wiki/Cubic_Hermite_spline https://en.wikipedia.org/wiki/Cubic_Hermite_spline

我知道 scipy 的插值方法。具体来说

splprep http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.splprep.html#scipy.interpolate.splprep插值 N 维样条 和splev http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.splev.html#scipy.interpolate.splev来评估其衍生品。

是否有一个接受函数值的Python例程f(x)及衍生品f'(x)对应值x并计算适合给定数据的样条表示。

举个例子:
我在空间中有两个由坐标定义的对象位置x,y,z我知道速度x',y',z'物体在这些位置的情况。我现在可以插入对象随时间 t 在两点之间经过的路径吗?考虑所有给定的参数。


延伸ev-br 的回答 https://stackoverflow.com/a/36655118/3388962,这里有一些示例代码,说明了用法BPoly.from_derivatives https://docs.scipy.org/doc/scipy-0.19.0/reference/generated/scipy.interpolate.BPoly.from_derivatives.html在点之间进行插值n具有指定导数的尺寸。

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

为了演示此函数的使用,我们指定点和切线。该示例进一步演示了更改切线的“大小”时的效果。

# Input.
points = []
tangents = []
resolution = 0.2
points.append([0.,0.]); tangents.append([1,1])
points.append([3.,4.]); tangents.append([1,0])
points.append([5.,2.]); tangents.append([0,-1])
points.append([3.,0.]); tangents.append([-1,-1])
points = np.asarray(points)
tangents = np.asarray(tangents)

# Interpolate with different tangent lengths, but equal direction.
scale = 1.
tangents1 = np.dot(tangents, scale*np.eye(2))
samples1 = sampleCubicSplinesWithDerivative(points, tangents1, resolution)
scale = 2.
tangents2 = np.dot(tangents, scale*np.eye(2))
samples2 = sampleCubicSplinesWithDerivative(points, tangents2, resolution)
scale = 0.1
tangents3 = np.dot(tangents, scale*np.eye(2))
samples3 = sampleCubicSplinesWithDerivative(points, tangents3, resolution)

# Plot.
import matplotlib.pyplot as plt
plt.scatter(samples1[:,0], samples1[:,1], marker='o', label='samples1')
plt.scatter(samples2[:,0], samples2[:,1], marker='o', label='samples2')
plt.scatter(samples3[:,0], samples3[:,1], marker='o', label='samples3')
plt.scatter(points[:,0], points[:,1], s=100, c='k', label='input')
plt.axis('equal')
plt.title('Interpolation')
plt.legend()
plt.show()

This results in the following plot: Cubic splines for different tangent magnitudes

需要注意三点:

  • 以下也可以应用于二维以上。
  • 样品之间的间距不固定。实现等距采样的一种简单方法是在返回的结果之间进行线性插值samples,正如在例如中所讨论的那样这个帖子 https://stackoverflow.com/a/19122075/3388962.
  • The specification of the tangents is optional, however BPoly.from_derivatives does not ensure smooth transitions between the splines at this position. If for example tangents[1] in the above sample is set to None, sampleCubicSplinesWithDerivative(points, tangents, resolution), the result will look like this: Cubic splines with only some tangents being prescribed
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

三次 Hermite 样条插值 python 的相关文章

随机推荐

  • 包含过期项的哈希表

    我想实施一个HashTable 或者也许是一个HashSet or Dictionary 其中有独特的成员 一段时间后就会过期 例如 Items expire automatically after 10 seconds Expiratio
  • Celery:WorkerLostError:工作人员过早退出:信号 9 (SIGKILL)

    我在 Django 应用程序 在 Elastic Beanstalk 上 中使用 Celery 和 RabbitMQ 来管理后台任务 并使用 Supervisor 对其进行守护进程 现在的问题是 我定义的周期任务之一失败 在正常工作一周后
  • 7u45 webstart 中出现新的死锁错误?

    java 7u45 的乐趣仍在继续 这次是 webstart 内部陷入僵局 这种情况在启动大型应用程序 约 100 个 jar 时非常一致地 每次 发生 还有其他人遇到过这个问题或知道任何解决方法吗 除了恢复到 1 7 0 40 之外 Th
  • Jekyll 自动目录

    我已经建立了一个基于 Jekyll 代码的网站阿帕奇构建者 http buildr apache org Buildr 网站根据标题中的标题自动为每个页面生成一个目录 textile格式文件 例如 您使用纺织品编写一个页面 标记出标题 如下
  • zftool 的行为不符合预期

    我在 Linux 中有一个托管服务 我正在尝试在 zend Framework 2 上开发一个应用程序 我想使用 zftool 但无论我运行哪个 zftool 命令 我总是得到主页的 HTML 我将非常感谢任何帮助 以下是重现步骤 1 我安
  • 以角度 2 实现旋转器

    我尝试在我的应用程序中使用微调器 因为有大量数据需要时间来加载 但这是问题所在 我已将此链接作为参考 Angular2 的预引导加载屏幕 https stackoverflow com questions 35243443 pre boot
  • C++ 中的引用计数究竟意味着什么?

    到底什么是引用计数 特别是对于 C 来说它是什么 如果我们不处理这些问题 我们可能会面临哪些问题 所有语言都需要引用计数吗 到底什么是引用计数 特别是对于 C 来说它是什么 简单来说 引用计数就是对某个对象的引用进行计数 通常 C 采用以下
  • 如何将 vim 选项的值复制到寄存器

    在 vim 中你可以设置选项 set 例如 设置咒语 启用拼写控制 和 设置拼写文件 home custom spell txt 设置自定义拼写文件的位置 您可以使用 echo 打印选项的值 例如 回声 拼写文件 现在我想将选项拼写文件的值
  • Ubuntu下Visual Studio Code显示错误

    我的 Visual Studio Code 遇到了好几天的问题 无法启动它 事实上 窗口几乎全黑 这是我从终端启动它时的控制台输出 bash impossible de r gler le groupe de processus du te
  • 如何构建具有未知列数的 WPF 数据网格

    我需要从 txt 中获取的字符串数组集合构建并显示 WPF 数据网格 问题是我不知道先验的列数 即单个数组中的项目数 所以我在我的xaml中定义
  • MySQL 上 NOT IN 的替代方案

    我有一个疑问 SELECT DISTINCT phoneNum FROM Transaction Register WHERE phoneNum NOT IN SELECT phoneNum FROM Subscription LIMIT
  • 如何使用 EF4 Fluent API 将删除级联到链接表中?

    我在现有 MSSQL 2008 R2 数据库中有两个表 它们通过链接表关联 这两个表是 计划 和 提示 链接表是 PlanTipLinks 计划可以有许多提示 并且提示可以与多个计划关联 即 这是多对多的关系 在应用程序中 我只关心 Pla
  • 如何在wpf中使富文本框自动调整大小?

    如何让RichTextBox自动调整大小 我希望我的富文本框能够适合我分配给它的任何文本 而无需滚动条 谢谢 Set the HorizontalAlignment and VerticalAlignment除了Stretch 这是默认值
  • 如何向 iso 8583 消息添加使用 jpos 的标头。 - ISO 8583 - jpos

    我是使用 iso 8583 进行开发的新手 我需要在 iso 消息之前添加标头 但我以这种方式实现它们 并且不会将其添加到消息标头中 我究竟做错了什么 我希望你可以帮助我 GenericPackager packager new Gener
  • 异步写入文件

    有没有办法编写一个异步函数来重复将数据写入文件 当我编写异步函数时出现以下错误 该进程无法访问文件 c Temp Data txt 因为该文件正在被另一个进程使用 public void GoButton Click object send
  • 如何为

    我只有 HTML 和 CSS 的基本知识 并且广泛地浏览了 Google 试图找到这个问题的答案 这一切似乎都指向 JavaScript 和 或 jQuery 我试过了 但无法让它工作 我有一个音频文件 在我的网站加载时开始播放 我想设置一
  • 如何将

    转换为换行符?

    假设我有一个 HTML p and br 里面有标签 之后 我将剥离 HTML 以清理标签 我怎样才能把它们变成换行符 我正在使用Python的美丽汤 http www crummy com software BeautifulSoup 图
  • 如何在 REST 中处理 @OneToMany 关系

    我正在设计一个小REST允许执行一些基本操作的应用程序 到目前为止一切顺利 我有以下 Entity called Client需要与 Entity called Loan Client Entity Table name clients p
  • 升级到 PyMongo 3.0 导致 ServerSelectionTimeoutError

    我最近将 Bottle uWSGI Nginx 应用程序升级到 MongoDB 3 0 2 它在 PyMongo 2 8 上运行良好 但今天我通过运行以下命令升级到 PyMongo 3 0 pip install upgrade pymon
  • 三次 Hermite 样条插值 python

    我想计算一个三次多项式 该多项式由其函数值和指定点的导数定义 https en wikipedia org wiki Cubic Hermite spline https en wikipedia org wiki Cubic Hermit