使用Numpy/SciPy进行线条平滑
我有一条线,想用 scipy.interpolate.splrep
和 scipy.interpolate.splev
来让它变得更平滑。
line = ((x1, y1), (x2, y2), ... (xn, yn))
tck = interpolate.splrep(x, y)
我需要找到更多的 x 坐标值,而且这些值要均匀分布。
newx = numpy.XXX(x)
newy = interpolate.splev(newx, tck)
比如说 (1, 2, 4, 3) -> (1, 1.5, 2, 2.5, 3, 3.5, 4, 3.5, 3)
有没有什么“简单”的方法可以在 Numpy/SciPy 中做到这一点?
1 个回答
3
你可以这样做:
import scipy.interpolate as interp
z = arange(0,4)
x = np.array([1,2,4,3])
f = interp.interp1d(z, x)
newx = f(np.linspace(z[0],z[-1],7))
这样做应该会给你这个结果:
In [40]: print z
[0 1 2 3]
In [41]: print x
[1 2 4 3]
In [42]: print newx
[ 1. 1.5 2. 3. 4. 3.5 3. ]
这个方法会在数组中定义的点之间进行线性插值,也就是说,它会根据这些点的顺序来计算中间的值。你是这个意思吗?