How can I interpolate x, y coordinate path with fixed interval?

12 views (last 30 days)
I made x, y coordinate from A star path planning algorithm in matlab m file. And I need to interpolate this with fixed interval.
My final data set is
Path = [1 2; 1 3; 1 4; 1 5; 1 6; 1 7; 2 8; 3 9; 4 9; 5 8; 5 7; 5 6; 5 5; 5 4; 5 3; 6 2;7 2; 8 2; 9 3; 9 4;9 5; 9 6; 9 7; 9 8; 8 9];
And I want interpolated data set from initial point (1,2) to final point (8,9) with distance 0.1944. But I have no idea how to implement in m file code.
Is there anybody who can do this? I really appreciate it advance. Thank you!!
  1 Comment
Aw94
Aw94 on 27 Nov 2016
I had a very similar problem with my data set. The difference is that I have xyz data instead of just xy. I tried this solution and I could not get the correct result. I could really use the help. Here is what I have:
format long g
fpath = 'Y:\Whaling\raw data\matlab\data cleaning\10m res\';
files = dir( fullfile(fpath,'txyzMLLW_*.txt') );
files = strcat(fpath,{files.name}');
f_txt=[];
for i=1:numel(files)
data = importdata(files{i});
total_length = arclength(data(:,2),data(:,3),data(:,4),'linear');
if total_length>10
newData= interparc(0:(10/total_length):1,data(:,2),data(:,3),data(:,4),'linear');
f_txt=[f_txt;newData];
else
N=.5;
newData= interparc(0:N:1, data(:,2), data(:,3),data(:,4),'linear');
f_txt=[f_txt;newData];
end
end
dlmwrite('10mres.txt',f_txt);
All of my files are text files organized with time in the first column, x coordinates (meters) in the second, y coordinates (meters) in the third, and depth (meters) in the fourth column. Each text file is a "path" of points. For each text file, I am attempting to get a new (interpolated) coordinate and depth every 10m along the path formed by the straight line distance calculated from one point to the next. If the total length of all the distances between points adds up to less than 10m, I used an else statement to just take the first and last coordinate and interpolate the point in the middle of the total length. Each point is on average, less than a centimeter apart. Ultimately, this will condense the final output file, 10mres.txt, from millions of points to tens of thousands. The problem is that the interpolated coordinates and depth is not 10m. As an example, here is the data from the last iteration:
>> test= newData([1,2],:)
test =
645042.18 3257946.5 -1.9139999
645046.405950062 3257954.01740637 -1.96472819711583
>> test_l= arclength(test(:,1),test(:,2),test(:,3))
test_l =
8.62395650285223
test_l should equal 10, meaning that the distance between the first two interpolated coordinates and depth should be 10m. I'm not sure which part of my code is wrong or if this is even possible. I would greatly appreciate any help! I've included two text files that are included in the large number of files that my code should loop through. Thanks!

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 17 Jul 2014
Trivial. Using a couple of tools (arclength, interparc) on the file exchange...
The total length of the curve is...
format long g
total_length = arclength(path(:,1),path(:,2),'linear')
total_length =
26.4852813742386
You want equally spaced points along the curve, at a spacing of 0.1944 ALONG the curve. I have no idea why you have chosen that number, but, I don't care. Note that the last point does not fall at the very end, since the total arc length along the curve is not an integer multiple of 0.1944.
interparc(0:(0.1944/total_length):1,path(:,1),path(:,2),'linear')
ans =
1 2
1 2.1944
1 2.3888
1 2.5832
1 2.7776
... (I've cut out a few lines in the middle)
8.30807325416077 8.69192674583923
8.17061169589811 8.82938830410189
8.03315013763544 8.96684986236456
  1 Comment
Image Analyst
Image Analyst on 17 Jul 2014
John's interparc is really good (though slow when I've used it) and is probably your best and easiest approach. I didn't think A* needed uniform spacing, but whatever... Mainly I wanted to say don't use Path as the name of your variable . I know MATLAB is case sensitive but it still seems risky to use a built-in variable/function as the name of your own variable.

Sign in to comment.

More Answers (2)

Pol van Rijn
Pol van Rijn on 18 Sep 2017
Edited: Pol van Rijn on 18 Sep 2017
Dear all,
We import a pitch signal with Matlab. The intervals are not equally spaced. We want to interpolize the signal into evenly spaced points for a fixed number of points (e.g. nbLandmarks = 100).
The code bellow exemplifies the problem:
x = [0.0443877551020408,0.0543877551020408,0.0643877551020408,0.0743877551020408,0.0843877551020408,0.0943877551020408,0.104387755102041,0.114387755102041,0.124387755102041,0.204387755102041,0.214387755102041,0.224387755102041,0.234387755102041,0.244387755102041,0.254387755102041,0.264387755102041,0.274387755102041,0.284387755102041,0.294387755102041,0.304387755102041,0.314387755102041,0.324387755102041,0.334387755102041,0.344387755102041,0.354387755102041,0.364387755102041,0.374387755102041,0.384387755102041,0.394387755102041,0.404387755102041,0.414387755102041,0.424387755102041,0.434387755102041,0.444387755102041,0.454387755102041,0.464387755102041,0.474387755102041,0.484387755102041,0.494387755102041,0.504387755102041,0.514387755102041,0.524387755102041,0.534387755102041,0.544387755102041,0.554387755102041,0.564387755102041,0.574387755102041,0.584387755102041,0.594387755102041,0.604387755102041,0.614387755102041,0.624387755102041,0.634387755102041,0.644387755102041,0.654387755102041,0.664387755102041,0.674387755102041,0.684387755102041,0.694387755102041,0.704387755102041,0.714387755102041,0.724387755102041,0.734387755102041,0.744387755102041,0.754387755102041,0.764387755102041,0.774387755102041,0.784387755102041,0.794387755102041,0.804387755102041,0.814387755102041,0.824387755102041,0.834387755102041,0.844387755102041,0.854387755102041,0.864387755102041,0.874387755102041,0.884387755102041,0.974387755102041,0.984387755102041,0.994387755102041,1.00438775510204,1.01438775510204,1.02438775510204,1.03438775510204,1.04438775510204,1.05438775510204,1.06438775510204,1.07438775510204,1.08438775510204,1.09438775510204,1.10438775510204,1.11438775510204,1.12438775510204,1.13438775510204,1.14438775510204,1.15438775510204,1.16438775510204,1.17438775510204,1.18438775510204,1.19438775510204,1.20438775510204,1.21438775510204,1.22438775510204,1.23438775510204,1.24438775510204,1.25438775510204,1.26438775510204,1.27438775510204,1.28438775510204,1.29438775510204,1.30438775510204,1.31438775510204,1.32438775510204,1.33438775510204,1.34438775510204,1.39438775510204,1.40438775510204,1.41438775510204,1.42438775510204,1.43438775510204,1.44438775510204,1.45438775510204,1.46438775510204,1.47438775510204,1.48438775510204,1.49438775510204,1.50438775510204,1.51438775510204,1.52438775510204,1.53438775510204,1.54438775510204,1.55438775510204,1.56438775510204,1.57438775510204,1.58438775510204,1.59438775510204,1.60438775510204,1.61438775510204,1.62438775510204,1.63438775510204,1.64438775510204,1.65438775510204,1.66438775510204,1.67438775510204,1.68438775510204,1.69438775510204,1.70438775510204,1.71438775510204];
y = [178.501287711490,178.813817319272,189.577320314237,198.531890111164,206.084842988324,210.772659446790,211.335281417129,225.730598341740,225.901977654885,306.196606301549,311.753166263343,306.529882118809,302.572283702370,299.243503200983,291.095074651409,279.481854772107,248.159722754787,214.034507246777,212.647142436683,214.577255800419,205.337286514298,193.497692372285,183.975721348776,184.194731172757,186.922051071877,197.014092067936,205.554295519911,198.012819197895,182.283425886289,173.663152149703,173.548841891897,185.041868348116,181.674858747520,190.872306020846,204.750027549473,217.903703680010,228.724275241253,247.757437675443,274.544838049364,291.329733811144,305.243226052746,314.228480296518,322.314347343363,323.565427408769,328.346912028903,344.803772784512,355.638316302583,365.371138780059,370.612820310853,373.468384184022,368.251725308925,364.370906526989,359.915275206608,349.382417137670,340.756437859346,334.161057959999,327.541751925761,319.936718164203,306.200374198362,299.791516056024,291.801696534321,284.316665363165,275.191681773465,262.431033660143,245.531853644526,233.993133040364,263.035204764925,275.410716045634,278.801012495933,276.075947645212,270.525890004624,265.883974511083,260.488758878041,253.327880797132,235.378716127273,222.297294573365,205.531760297487,195.800381677558,260.847505424418,255.422377848489,252.998437535703,241.663088823371,238.130803984018,231.174450559706,231.272951661193,227.954282074205,224.273074967619,220.502578201465,215.908811382127,213.115791000171,210.694882117113,210.191410569382,208.331620587344,206.889418207533,209.508138301982,198.845889904302,194.214615751282,190.323077342336,187.747212018961,186.247368793523,185.378465024959,183.800566689203,183.487596528727,183.130097319624,182.972172520995,182.949136038847,184.255952332459,185.511207552205,186.796049387191,187.956127411674,188.083790019697,182.699706588926,182.656205931429,167.955624245799,161.730237334813,164.334140324580,222.228845242497,188.486774766025,185.653134577677,183.722858436240,181.215446829656,178.628665268127,176.502654025668,174.724525577723,173.261084454337,171.311040476288,169.415204605069,167.454558556174,165.794225884532,163.878190839415,162.069203412894,159.845921980463,156.699464668517,154.334356064799,152.524246385702,151.462151275399,149.289694397227,147.666645346538,146.250525583130,144.097828914436,142.148718181077,141.300116362692,140.362078497336,139.312279921538,143.015388437532,143.510794105153,142.452192935977,136.907902687353,140.508000973869];
% given is raw pitch data, x is time, y is pitch
This is a first approach to solve the problem (it uses the interparc by John) % number of different points we want to draw on the spline nbLandmarks = 100;
figure % make a figure
numPlots = 3;
ax1 = subplot(numPlots,1,1);
plot(ax1,x,y, 'o-') % plot raw data
title(ax1,'Raw data, fundamental frequency')
ylabel(ax1,'Frequency (Hz)')
xlabel(ax1,'Time')
% first approach
interpolatedXY = interparc(linspace(0, 1, nbLandmarks), x,y,'lin');
ax2 = subplot(numPlots,1,2);
plot(ax2,interpolatedXY(:,1), interpolatedXY(:,2), 'o-')
title(ax2,'First try with the interparc function, no luck...')
ylabel(ax2,'Frequency (Hz)')
xlabel(ax2,'Time')
This is a second approach I found on one of the Matlab forums, but you get the same result...
% second approach
pathXY = [x;y]'; % merge new x and y coordinates in one table
stepLengths = sqrt(sum(diff(pathXY,[],1).^2,2));
stepLengths = [0; stepLengths]; % add the starting point
cumulativeLen = cumsum(stepLengths); % Cumulative sum of all items --> last item contains total length
finalStepLocs = linspace(0,cumulativeLen(end), nbLandmarks);
finalPathXY = interp1(cumulativeLen, pathXY, finalStepLocs);
ax3 = subplot(numPlots,1,3);
title(ax3,'Second try, still not working')
ylabel(ax3,'Frequency (Hz)')
xlabel(ax3,'Time')
plot(ax3, finalPathXY(:,1),finalPathXY(:,2), 'o-')
I've been puzzled since days, why the code doesn't work for my data (both approaches work for different examples used in forums). Might it be the case that my data is too noisy?
Any help would be very appreciated!!

Gilwon Seo
Gilwon Seo on 17 Jul 2014
Thank you John!! It works perfectly. You just saved one man. :) Have a great day!!

Categories

Find more on Graphics Performance in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!