Python中隐式曲线的高阶局部插值 [英] Higher order local interpolation of implicit curves in Python

查看:64
本文介绍了Python中隐式曲线的高阶局部插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一组描述2D平面中某些轨迹的点,我想用局部高阶插值来平滑表示该轨迹.

Given a set of points describing some trajectory in the 2D plane, I would like to provide a smooth representation of this trajectory with local high order interpolation.

例如,假设我们在下图中定义了一个11点的2D圆.我想按顺序在每对连续的点之间添加点,或产生平滑的轨迹.在每个线段上添加点很容易,但是会产生典型的局部线性插值"斜率不连续性.当然,这不是古典意义上的插值,因为

For instance, say we define a circle in 2D with 11 points in the figure below. I would like to add points in between each consecutive pair of points in order or produce a smooth trace. Adding points on every segment is easy enough, but it produces slope discontinuities typical for a "local linear interpolation". Of course it is not an interpolation in the classical sense, because

  • 对于给定的x
  • ,该函数可以具有多个y
  • 只需在轨迹上添加更多点就可以了(不需要连续表示).

所以我不确定什么是合适的词汇.

so I'm not sure what would be the proper vocabulary for this.

产生此图的代码可在下面找到.线性插值通过lin_refine_implicit功能执行.我正在寻找更高阶的解决方案以产生平滑的轨迹,并且想知道Scipy中是否有一种使用经典功能来实现它的方法?我尝试过使用scipy.interpolate中的各种一维插值,但没有成功(再次由于给定x的多个y值).

The code to produce this figure can be found below. The linear interpolation is performed with the lin_refine_implicit function. I'm looking for a higher order solution to produce a smooth trace and I was wondering if there is a way of achieving it with classical functions in Scipy? I have tried to use various 1D interpolations from scipy.interpolate without much success (again because of multiple y values for a given x).

最终目标是使用这种方法从离散测量中获得平滑的GPS轨迹,因此我认为这应该在某个地方具有经典的解决方案.

The end goals is to use this method to provide a smooth GPS trajectory from discrete measurements, so I would think this should have a classical solution somewhere.

import numpy as np
import matplotlib.pyplot as plt

def lin_refine_implicit(x, n):
    """
    Given a 2D ndarray (npt, m) of npt coordinates in m dimension, insert 2**(n-1) additional points on each trajectory segment
    Returns an (npt*2**(n-1), m) ndarray
    """
    if n > 1:
        m = 0.5*(x[:-1] + x[1:])
        if x.ndim == 2:
            msize = (x.shape[0] + m.shape[0], x.shape[1])
        else:
            raise NotImplementedError

        x_new = np.empty(msize, dtype=x.dtype)
        x_new[0::2] = x
        x_new[1::2] = m
        return lin_refine_implicit(x_new, n-1)
    elif n == 1:
        return x
    else:
        raise ValueError
n = 11
r = np.arange(0, 2*np.pi, 2*np.pi/n)
x = 0.9*np.cos(r)
y = 0.9*np.sin(r)
xy = np.vstack((x, y)).T
xy_highres_lin = lin_refine_implicit(xy, n=3)

plt.plot(xy[:,0], xy[:,1], 'ob', ms=15.0, label='original data')
plt.plot(xy_highres_lin[:,0], xy_highres_lin[:,1], 'dr', ms=10.0, label='linear local interpolation')
plt.legend(loc='best')
plt.plot(x, y, '--k')
plt.xlabel('X')
plt.ylabel('Y')
plt.title('GPS trajectory')
plt.show()

推荐答案

这称为参数插值.

scipy.interpolate.splprep 提供此类曲线的样条曲线近似值.假设您知道点在曲线上的顺序.

scipy.interpolate.splprep provides spline approximations for such curves. This assumes you know the order in which the points are on the curve.

如果您不知道曲线上哪个点在哪个点之后,该问题将变得更加困难.我认为在这种情况下,该问题称为流形学习,并且scikit-中的某些算法学习可能会对此有所帮助.

If you don't know which point comes after which on the curve, the problem becomes more difficult. I think in this case, the problem is called manifold learning, and some of the algorithms in scikit-learn may be helpful in that.

这篇关于Python中隐式曲线的高阶局部插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆