[SciPy-User] Monotonic Piecewise Cubic Hermite Interpolator (Matlab pchip() equivalent).
Chris Michalski
chris.michalski at gmail.com
Sat Aug 29 21:48:10 EDT 2009
Recently, I had a need for a monotonic piece-wise cubic Hermite
interpolator. Matlab provides the function "pchip" (Piecewise Cubic
Hermite Interpolator), but when I Googled I didn't find any Python
equivalent. I tried "interp1d()" from scipy.interpolate but this was
a standard cubic spline using all of the data - not a piece-wise cubic
spline. This type of interpolator wasn't up to my needs because it
tends to ring between points where there are sharp transitions because
it isn't monotonic and it uses all of the data - which makes the
interpolator "miss" sharp breaks in the data.
I had access to Matlab documentation, so I spent a some time tracing
through the code to figure out how I might write a Python duplicate.
This was an massive exercise in frustration and a potent reminder on
why I love Python and use Matlab only under duress. I find typical
Matlab code is poorly documented (if at all) and that apparently
includes the code included in their official releases. I also find
Matlab syntax “dated” and the code very difficult to “read”.
Wikipedia to the rescue:
http://en.wikipedia.org/wiki/Cubic_Hermite_spline
http://en.wikipedia.org/wiki/Monotone_cubic_interpolation
Not to be deterred, these two very well written Wikipedia entries
explained in simple language how to compute the interpolant. Hats off
to whoever wrote these entries – they are excellent. The result was a
surprising small amount of code considering the Matlab code was
approaching 10 pages of incomprehensible code. Again - strong evidence
that things are just better in Python...
The resulting code:
along with a matplotlib plot showing the results:
As the plot shows, the "pchip" does preserve monotonicity compared to
a "straight" piecewise cubic Hermite interpolation. In addition, both
of the piecewise interpolators result in less "ringing" away from
transitions because they only consider the data points in the
immediate vicinity of the desired output point.
Hope this is helpful in filling the void that several have asked about.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20090829/7035a104/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pychip.py
Type: text/x-python-script
Size: 10885 bytes
Desc: not available
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20090829/7035a104/attachment.bin>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20090829/7035a104/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pychip_example.png
Type: image/png
Size: 44786 bytes
Desc: not available
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20090829/7035a104/attachment.png>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20090829/7035a104/attachment-0002.html>
More information about the SciPy-User
mailing list