# Interpolate X-Y coordinates with variable velocity to specified sampling frequency

5 views (last 30 days)
Michael McGeehan on 2 Jun 2021
Commented: J. Alex Lee on 3 Jun 2021
Can anyone help me interpolate between XY position coordinates with varying velocities between the coordinates. For example, I have a 3xn array with X-Y coordinates (mm) and a linear velocity (mm/s) for traveling from i to i+1 coordinates. I would like to resample these data via linear interpolation at 60 Hz as a series of XY cooridnates in the time domain. For context, these are coordinates for a CNC machine where you specifiy the desired coordinates and a rate at which to move to those coordinates. I would like to calculate intermediate coordinates at 60 Hz.
Thank you.
Cris LaPierre on 3 Jun 2021
You need to figure out the distance between the two points, and then the number of time steps (at 60 Hz) is needed to cover that distance at the indicated velocity. Only then can you use interpolation to figure out you X and Y values at each time step.
Because the velocities will change from one position to the next, you will likely need to do this in a loop.

J. Alex Lee on 3 Jun 2021
Edited: J. Alex Lee on 3 Jun 2021
I don't think you need loops...so assume your data is called XYV, and according to how you describe, I guess the last velocity point will be meaningless (i-th row velocity means the velocity it took to get from coordinates in row i to row i+1)
XYV = [
0,1,2,3;
0,2,5,8;
1,5,3,NaN]
XYV = 3×4
0 1 2 3 0 2 5 8 1 5 3 NaN
Distance traveled between rows is
dxy = diff(XYV(1:2,:),[],2)
dxy = 2×3
1 1 1 2 3 3
d = sqrt(sum(dxy.^2))
d = 1×3
2.2361 3.1623 3.1623
The time it took to move in each row
trow = d./XYV(3,1:end-1)
trow = 1×3
2.2361 0.6325 1.0541
So the timestamp for each row is
t = [0,cumsum(trow)]
t = 1×4
0 2.2361 2.8685 3.9226
And then you can do linear interpolations
tI = 0:1/60:t(end);
xI = interp1(t,XYV(1,:),tI);
yI = interp1(t,XYV(2,:),tI);
##### 2 CommentsShowHide 1 older comment
J. Alex Lee on 3 Jun 2021
• Actually the velocity at i-th row corresponds to speed between the point at (i-1)th and (i)th row, rather than i and i+1 as you noted above. The actual data makes much more sense, incidentally.
• So you need to shift the index
trow = d./XYV(3,2:end)
• the speeds are such that your ending time is 0.1807 seconds (if above is correct). 60Hz sampling is every 0.0167 seconds, so you will only get few times between the start and end (11 when I ran it)

R2021a

### Community Treasure Hunt

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

Start Hunting!