interp1 question for resampling

I am trying to use interp1 to resample my data. I have the values of forces for different timeframe lengths. (eg. I have 4 trials, trial one has 36 frames, trial 2 has 43, trial 3 has 38, tria4 has 35). To compare them have a nice figure, I want to <<expand>> the number of frames to 100 for each of them (like it would be 1-100% of the timeframe).
I used resample in the beginning, but I get some noise in the beginning and the end of the signal.
dif_Fx = (resample(dif(1:motion_length,1),100,motion_length))
(motion_length is the number of frames and dif is my signal)
I tried to use interp1 as
dif_Fx = (interp1((dif(1:motion_length,1)),1:1:100));
dif_Fx is just the signal (dif), and the rest of the values, from motion_length+1 to 100 are NaN.
I have read again and again the documentation, but I can;t seem to understand how to modify it to get what I want.
Any help is much appreciated.

 Accepted Answer

Star Strider
Star Strider on 11 Apr 2021
To prevent the NaN results from being part of the interp1 output, specify an interpolation method and use the 'extrap' argument. See the function documentation for details.

8 Comments

I tried that, but it was unsuccessful. Does interp1 actually "stretch" the domain from 36 frames to 100 frames, like resample does? or does it just complete the original data from 36 to 100 in this example?
I would have to have at least a representative sample of your data.
Note that interp1 requires at least three arguments, the first two being the original vectors, and the third being the vector of independent variable values to be interpolated (or extrapolated).
Ok, I think I understand now.
dif is my vector that I want to expand and it has 36 rows and 1 column- I am assuming that this would be the third vector you are talking about (" independent variable values to be interpolated (or extrapolated).")
My rows represent the time, so 36 rows will be 36 seconds.
I want to expand this to 100 seconds, so that the first value (for position 1) will be the first value from dif, and the last value (for position 100) will be the last value from dif.
Then, my first vector should be 1:1:36 and the second one 1:1:100?
My function should therefore be vq=interp1(1:1:36, 1:1:200, dif, 'extrap')?
interp1(dif, linspace(1,length(dif), 100))
"Note that interp1 requires at least three arguments"
No, interp1(v, xq) is permitted and equivalent to x=1:length(v)
If you just give it an array, interp1 assumes that the first argument (that it creates) is (1:length(array)).
I believe I now understand where the problem is.
Try something like this:
dif = sin((0:9)/9*2*pi);
vq = linspace(0, numel(dif), 100);
difi = interp1(dif, vq);
figure
plot(dif, 'pg')
hold on
plot(vq, difi, '.r')
hold off
grid
Note that I created ‘vq’ to span the original length of the ‘dif’ vector (since the vectors are different lengths, they would require a separate interp1 call for each of them), however with 100 elements. That is likely close to the result you want.
Thank you so much! This was really helpful.
It woked with minor tweaks.
As always, my pleasure!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!