Velocity Reviews > Re: Simple Interpolation in Numpy?

# Re: Simple Interpolation in Numpy?

Steve Holden
Guest
Posts: n/a

 02-08-2007
LAPI, VINCENT J, ATTLABS wrote:
> Hi,
> Please bear with me as I am new to Python and have not done any
> programming in about 20 years. I am attempting to do a simple
> interpolation of a line's intermediate points given the x,y coordinates
> of the line's two endpoints within an Active State Python script that I
> am working with. Is there a simple way to do this simple interpolation
> in the Active state Python 2.4 that I have or do I need to get Numeric
> Python? And where do I get it?
> Thanks,
> Vince Lapi
>

You shouldn't really *need* Numeric (NumPy or numpy, nowadays) for a
relatively simple problem like that, since the formulae involved are
pretty simple.

Given known points on the line at (xa, ya) and (xb, yb) then for any
point (x, y) on the line we get

y = ya + ((x - xa) * (yb - ya))/(xb - xa)

So you just need to plug the values for x and the known points into the
formula to get the interpolated value of y.

If you are interpolating a non-linear formula through a number of
samples clearly there'd be a little more jiggery pokery involved to
identify the particular interval on which interpolation is required, but
nothing horrendous.

Numpy, which is derived from the old Numeric code, is at

http://numpy.scipy.org/

should you wish to investigate its features. It probably has
better-than-linear interpolation algorithms in it. Note that if you
decide to download it you should choose the Python 2.4 version -
extensions coded in compiled languages are specific to a particular
version of the language.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Blog of Note: http://holdenweb.blogspot.com
See you at PyCon? http://us.pycon.org/TX2007