1
votes

I'd like to use the interp1 function to (linearly) interpolate between sample points. For uniform interpolation between sample points, I can use

test = [-1 -2 0 3];
new = interp1(test,1:.5:numel(test));

which will give an interpolated series, where the new series now includes one additional interpolated point between each of the original (test) series points.

What I would like to do is, create a new series that interpolates depending upon the differences between test series points. For the case above, the resulting output would look like

[-1 -2 -1 0 1 2 3];

I've worked through the examples at http://au.mathworks.com/help/matlab/ref/interp1.html, but the answer still eludes me. I'd appreciate a little direction for this simple query.

1
Isn't that simply min(test):max(test)?Luis Mendo
@LuisMendo omg, you are right of course. Have look at my complicated answer for this :( Of course that is only right for linear interpolation, while the interp1 solution can use any interpolation method, e.g. cubichbaderts
@hbaderts It's not really clear what the OP wants. Perhaps they should clarifyLuis Mendo

1 Answers

2
votes

The general syntax for interp1 is

vq = interp1(x,v,xq);

where x and v are the input and xq and vq are the interpolated vectors. You are using the shorter syntax

vq = interp1(v,xq);

where x is assumed to be 1:length(v).

In my opinion, it is easier to create an x vector which allows you to interpolate with xq=1:length(vq), than to make x=1:length(v) and calculate xq. To construct the correct x vector, you can do the following:

  1. Use diff(test) to find the difference between the v values. This tells you how many interpolated values you need inbetween.
  2. We'll want to use absolute differences: abs(diff(test)).
  3. To get the corresponding x values, we can do a cumsum on this result, starting at 1. That is: cumsum([1, abs(diff(test))]);. For the test input, this gives us: [1 2 4 7] which seems right: no additional value between -1 and -2, one value between -2 and 0, and finally two values between 0 and 3.

We can use this vector as x and then get the vector xq as all elements from 1 to the maximum value in x:

x = cumsum([1, abs(diff(test))]);
xq = 1:max(x);

Then we can do the interpolation:

vq = interp1(x,test,xq);

which gives us

vq = 
   -1   -2   -1    0    1    2    3