{"id":26160100,"url":"https://github.com/otvam/fast_interpolation_matlab","last_synced_at":"2025-06-23T02:07:08.616Z","repository":{"id":164277121,"uuid":"435611061","full_name":"otvam/fast_interpolation_matlab","owner":"otvam","description":"MATLAB Code for Fast Linear Interpolation","archived":false,"fork":false,"pushed_at":"2024-12-18T04:45:41.000Z","size":155,"stargazers_count":9,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-14T11:29:07.360Z","etag":null,"topics":["fast","interpolation","interpolation-techniques","linear","matlab","mex","optimized"],"latest_commit_sha":null,"homepage":"","language":"MATLAB","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/otvam.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2021-12-06T18:50:56.000Z","updated_at":"2025-03-22T21:17:36.000Z","dependencies_parsed_at":null,"dependency_job_id":"61e2f1a0-c4ee-48f3-8586-29f2ef8392a2","html_url":"https://github.com/otvam/fast_interpolation_matlab","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/otvam/fast_interpolation_matlab","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/otvam%2Ffast_interpolation_matlab","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/otvam%2Ffast_interpolation_matlab/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/otvam%2Ffast_interpolation_matlab/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/otvam%2Ffast_interpolation_matlab/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/otvam","download_url":"https://codeload.github.com/otvam/fast_interpolation_matlab/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/otvam%2Ffast_interpolation_matlab/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261397372,"owners_count":23152488,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fast","interpolation","interpolation-techniques","linear","matlab","mex","optimized"],"created_at":"2025-03-11T11:59:50.877Z","updated_at":"2025-06-23T02:07:03.530Z","avatar_url":"https://github.com/otvam.png","language":"MATLAB","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MATLAB Code for Fast Linear Interpolation\n\n![license - BSD](https://img.shields.io/badge/license-BSD-green)\n![language - MATLAB](https://img.shields.io/badge/language-MATLAB-blue)\n![category - science](https://img.shields.io/badge/category-science-lightgrey)\n![status - unmaintained](https://img.shields.io/badge/status-unmaintained-red)\n\nThe **MATLAB code** offers fast **1D linear interpolation** methods.\n\nThe following **fast interpolation methods** is implemented:\n* Linear interpolation inside the domain, linear extrapolation outside.\n* Support vector or matrix (set of 1D values) for the sample values.\n* Support for evenly spaced sample points: `interp_regular`.\n* Support for evenly arbitrarily spaced sample points: `interp_fast`.\n* These algorithms can be **up to 30x faster** than the MATLAB builtin interpolation methods.\n\nThe following **algorithm** is used for `interp_regular`:\n* The sample points are evenly spaced.\n* The position (index) of the query points can be computed without searching.\n* Hence, the complexity is O(1).\n\nThe following **algorithm** is used for `interp_fast`:\n* The sample points are arbitrarily spaced.\n* After each query, the position (index) of the point is returned.\n* This index is used as an initial value for the next query point.\n* Hence, the computational cost is reduced if the query points are partially sorted.\n* For randomly distributed query points, the complexity is O(n).\n* For sorted query points, the complexity is reduced from O(n) to O(1).\n\nThese methods should be **used in the following case**:\n* Many calls are done with the same sample points and values.\n* The calls cannot be vectorized (interdepency between the query points).\n* A typical use case is ODE integration where the calls cannot be vectorized.\n\nThese functions can be compiled to **MEX** files with the **MATLAB Coder**.\n\n## Functions\n\n* [interp_regular.m](interp_regular.m) - Fast interpolation method (evenly spaced sample points).\n* [interp_fast.m](interp_fast.m) - Fast interpolation method (arbitrarily spaced sample points).\n\n## Examples\n\n* [run_example_simple.m](run_example_simple.m) - Minimal working example for the interpolation code.\n* [run_example_ode.m](run_example_ode.m) - Example with interpolation inside an ODE function.\n\n## Benchmark\n\nThe following sample points and values are considered (1000 points):\n\n```\n% sample points (sorted)\nx_vec = linspace(0, 1, 1000);\n\n% sample values (3 rows)\ny_mat = [-1+x_vec+x_vec.^2 ; +1+x_vec-x_vec.^2; +2-x_vec-x_vec.^2];\n```\n\nThe following query points (sorted and random) are considered (12500 points):\n\n```\n% query point, mostly sorted\nx_vec_pts_sort = [...\n    linspace(-1.0, +2.0, 2500)...\n    linspace(+2.0, -0.5, 2500)...\n    linspace(-0.5, +0.5, 2500)...\n    linspace(+0.5, -1.0, 2500)...\n    linspace(-1.0, +2.0, 2500)...\n    ];\n\n% randomly sorted query points\nidx = randperm(length(x_vec_pts_sort));\nx_vec_pts_rand = x_vec_pts_sort(idx);\n```\n\nThe following algorithms are compared with sorted and random query points:\n* `interp1` code (MATLAB builtin function, MATLAB and MEX)\n* `griddedInterpolant` code (MATLAB builtin function, MATLAB)\n* `interp_regular` code (proposed method, MATLAB and MEX)\n* `interp_fast` code (proposed method, MATLAB and MEX)\n* In this document, the benchmark is run on a Intel i5-8250U laptop on Linux (64 bits).\n\nThe following files are required to run the benchmark:\n* [run_benchmark_compile.m](run_benchmark_compile.m) - Compile the MATLAB files into MEX files.\n* [run_benchmark_run.m](run_benchmark_run.m) - Run the benchmark for the different methods.\n\n### Vectorized Call\n\nAll the 12500 query points are evaluated at once with a vectorized call.\n\n```\n============================ vectorized call\n\ninterp1              MATLAB   sorted = 0.74 ms     random = 0.70 ms  \ninterp1              MEX      sorted = 0.70 ms     random = 0.85 ms  \n\ngriddedInterpolant   MATLAB   sorted = 0.18 ms     random = 0.21 ms  \ngriddedInterpolant   MEX      sorted = NaN ms      random = NaN ms   \n\ninterp_regular       MATLAB   sorted = 1.92 ms     random = 1.59 ms  \ninterp_regular       MEX      sorted = 2.89 ms     random = 4.18 ms  \n\ninterp_fast          MATLAB   sorted = 3.62 ms     random = 18.61 ms \ninterp_fast          MEX      sorted = 1.12 ms     random = 18.45 ms \n\n============================ vectorized call\n```\n\n* MEX files are not faster than MATLAB files.\n* Sorted query points are better for `interp_fast`.\n* The best overall algorithm is `griddedInterpolant`.\n* **For vectorized call, `griddedInterpolant` should be prefered.**\n\n### Non-Vectorized Call\n\nAll the 12500 query points are evaluated one by one (in a for-loop).\n\n```\n============================ non-vectorized call\n\ninterp1              MATLAB   sorted = 534.80 ms   random = 649.80 ms\ninterp1              MEX      sorted = 94.57 ms    random = 75.42 ms \n\ngriddedInterpolant   MATLAB   sorted = 37.21 ms    random = 45.32 ms \ngriddedInterpolant   MEX      sorted = NaN ms      random = NaN ms   \n\ninterp_regular       MATLAB   sorted = 45.32 ms    random = 29.57 ms \ninterp_regular       MEX      sorted = 1.16 ms     random = 1.11 ms  \n\ninterp_fast          MATLAB   sorted = 11.36 ms    random = 27.46 ms \ninterp_fast          MEX      sorted = 1.11 ms     random = 17.61 ms \n\n============================ non-vectorized call\n```\n\n* MEX files are faster than MATLAB files.\n* Sorted query points are better for `interp_fast`.\n* The best overall algorithm is `interp_regular` and `interp_fast`.\n* **For non-vectorized call, `interp_regular` should be prefered with evenly spaced samples points.**\n* **For non-vectorized call, `interp_fast` should be prefered with arbitrarily spaced samples points.**\n\n## Compatibility\n\n* Tested with MATLAB R2021a.\n* The MATLAB Coder toolbox is required for compiling MATLAB into MEX.\n* Compatibility with GNU Octave not tested but probably easy to achieve.\n\n## Author\n\n**Thomas Guillod** - [GitHub Profile](https://github.com/otvam)\n\n## License\n\nThis project is licensed under the **BSD License**, see [LICENSE.md](LICENSE.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fotvam%2Ffast_interpolation_matlab","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fotvam%2Ffast_interpolation_matlab","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fotvam%2Ffast_interpolation_matlab/lists"}