Thursday, January 13, 2011

[Py] Spline function test

To interpolate data points, Scipy has several modules for this work. For what I am interested in, there are two functions seem to meet my requirement:

According to the explanations given by the above links, to use UnivariateSpline with s=1 is equivalent to use InterpolatedUnivariateSpline.

Here is the results of a simple test:

Code:

from numpy import linspace,exp
from numpy.random import randn
from scipy.interpolate import UnivariateSpline, InterpolatedUnivariateSpline
from pylab import plot, legend, show

x = linspace(-3, 3, 100)
y = exp(-x**2) + randn(100)/10

s0 = UnivariateSpline(x, y, s=0)
s1 = UnivariateSpline(x, y, s=1)
s2 = InterpolatedUnivariateSpline(x, y)

xs = linspace(-3, 3, 1000)
ys0 = s0(xs)
ys1 = s1(xs)
ys2 = s2(xs)

plot(xs,ys0,'g--',label='UnivariateSpline s=0',linewidth=4)
plot(xs,ys1,'b',label='UnivariateSpline s=1')
plot(xs,ys2,'k',label='InterpolatedUnivariateSpline')
plot(x,y,'ro',label='original signal')
legend()
show()

No comments:

Post a Comment