Line smoothing with Numpy/SciPy
I have a line which should be smoothened by scipy.interpolate.splrep
and scipy.interpolate.splev
.
line = ((x1, y1), (x2, y2), ... (xn, yn))
tck = interpolate.splrep(x, y)
I need to find more values for my开发者_开发百科 x-coordinate which should be arranged evenly.
newx = numpy.XXX(x)
newy = interpolate.splev(newx, tck)
e.g. (1, 2, 4, 3) -> (1, 1.5, 2, 2.5, 3, 3.5, 4, 3.5, 3)
Is there a "simple" way to achieve this in Numpy/SciPy?
You could do something like this:
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))
which should give you
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. ]
which will just linearly interpolate between abscissa points in the order they are defined in the array. Is that what you were thinking of?
精彩评论