Reputation: 71
I am using the interp1
function in MATLAB to interpolate some missing data in a signal and it works like a charm. However, I would like to know how the function works.
I checked the code of the function interp1
, which uses the function spline
. The code of the function spline
is extremely hard for me to understand, so I have googled and wikipedia'd it, and I know (generally) how it works, with degrees of freedom, and how the function uses polynomials (usually 3rd order) to generate the missing part of the curve.
If I have an array of 20 numbers, and 8 - 12 would be missing (zeros):
4 5 2 3 5 4 3 0 0 0 0 0 4 5 2 4 3 5 4 3
How does the function determine what numbers would fit in there? Is it a window of a certain width that moves over the data, like processing [1-5], [2-6], [3-7] etc? Or does it use the 2 or 3 numbers to the left and right of the missing data?
I am not looking for a mathematical explanation, I just want to know how it does its magic :)
Upvotes: 1
Views: 3492
Reputation: 3116
I still do not know if this will answer your question but I will try and see.
I will try to be as clear and understandable as possible so I might intentionnaly leave some (maybe important) details apart for the sake of simplicity.
One sometimes know the value of a function at a set of points without knowing its analytical expression. The task of knowing the value of the function at a point that is not in the set is called interpolation / extrapolation. The basic principle of interpolation is to compute the value of the function at the desired point from its value at the nearest neighbors.
The simplest method you can think of is linear interpolation. The value of your unknown function at a given point is a distance weighted average of the nearest neighboring values. This simply means that if the point of interest is at distance 1 of point A and distance 9 from point B the value of the function at this point will be 10 % B and 90 % A. This is equivalent to drawing straight lines between each points where you know the value of the function.
The problem of this method is that it produces discontinuous estimation of the function. This is annoying when modeling function that describes natural phenomenon because these functions are often continuous.
Amongst other interpolation methods, the cubic spline interpolation can solve this problem. The principle remains identical, excepts that instead of having a line between each point you have a third order polynomial. Some constraints on the polynomial makes it unique: namely its first and second derivatives must be continuous with the neighboring polynomials. This assures the "smoothness" of the interpolated function.
So for me, the "magic" of cubic spline interpolation comes from the assumption of "smoothness" that allows this method to correctly interpolate function describing natural phenomenon.
If this answer is not useful or too simplistic I will delete it.
Upvotes: 2