No BSD License  

Highlights from
Fast interpolation

4.66667

4.7 | 7 ratings Rate this file 32 Downloads (last 30 days) File Size: 3.44 KB File ID: #10286
image thumbnail

Fast interpolation

by Nathaniel Brahms

 

08 Mar 2006 (Updated 18 Apr 2006)

Performs nearest-neighbor or linear interpolation much faster than interp1 when an evenly-spaced lib

| Watch this File

File Information
Description

This function performs interpolation faster than MATLAB's "interp1" function. In the limit of small library and search arrays, it is ~5x faster. In the limit of large library arrays, qinterp1 has a flat scaling, while interp1 has a linearly increasing scaling (see the image for this file). qinterp1 requires an evenly spaced, monotonically increasing x array. Like interp1, qinterp1 returns NaN for xi values that are out of bounds.

Per John D'Errico's suggestion, the nearest-lower-neighbor method has been changed to now use true nearest-neighbor interpolation (at a slight speed cost).

A note on error checking: Because any error checking of the library array would destroy the flat scaling law, this function performs no error checking on the library (x and y) arrays. This function will return an error if the y and xi arrays are not both column or both row vectors.

Type "help qinterp1" for usage instructions.

This should be backwards compatible for quite a few releases. It is platform-independent.

The attached image shows the result of speed tests performed on a 2.4GHz, 2GB Windows XP machine. The same x, y, and xi vectors were used for each algorithm. The qinterp1 method came out ahead in all parameters tested. Note that, surprisingly, in the case of evenly-spaced x vectors, interp1q is slower than interp1 for most parameters, and interp1's nearest-neghbor interpolation is almost always slower than linear interpolation!

Note: After writing this function, I noticed Umberto Picchini's fast interpolation function, which provides up to a 4x speedup without the requirement of an evenly-spaced array. The file ID is 8627.

Acknowledgements
This submission has inspired the following:
Fast 2-dimensional interpolation, ScaleTime
MATLAB release MATLAB 7.1.0 (R14SP3)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (8)
09 Mar 2006 Duane Hanselman

FYI: Umberto Picchini's submission 8627 is a mex file, so it is not as platform independent as this submission.

10 Mar 2006 John D'Errico

This does not actually do nearest neighbor interpolation, but instead nearest lower neighbor. In the branch for nearest neighbor, the author could have used round instead of floor to produce true nearest neighbor interpolation with no loss in speed. My rating would go up if this was repaired.
For those who really do need a simple fast interpolant, this code should provide it, as long as your independent variable is uniform. In its quest for speed however, this code also has no error checks on its inputs. I do very much like that this is an m file, not mexed.

10 Mar 2006 John D'Errico

Better now with a proper nearest computation. The goal of this code is maximum speed, any error checks are inconsistent with that goal. This code will be as fast as I would reasonably expect it to be.

18 Sep 2006 Greg Palmer

Works great! I found it to be about 2x faster for my application. One thing I noticed you may want to change is to use NaN(...) instead of NaN*ones(...), which should be faster.

05 May 2007 si ajá  
28 Dec 2007 ahmed khaled  
23 Jan 2009 Adam Chapman

Brilliant job, well done. Works even better with Greg Palmer's suggestion.

Thanks

31 May 2012 Martin Saravia

Excellent job!!!,it works 7 times faster than linterp 1 in my code.

Please login to add a comment or rating.
Updates
10 Mar 2006

Previous version returned the wrong result (shifted by one index from the proper result). Extra speed-ups also added.

10 Mar 2006

Changed nearest-lower-neighbor to true nearest-neighbor.

18 Apr 2006

Now returns NaN for any xi=NaN

Tag Activity for this File
Tag Applied By Date/Time
approximation Nathaniel Brahms 22 Oct 2008 08:17:43
interpolation Nathaniel Brahms 22 Oct 2008 08:17:43
fast Nathaniel Brahms 22 Oct 2008 08:17:43
mathematics Nathaniel Brahms 22 Oct 2008 08:17:43
quick Nathaniel Brahms 22 Oct 2008 08:17:43
fast Silviu Zilberman 30 Jun 2011 03:37:21

Contact us at files@mathworks.com