# fit curve with parameter

1 view (last 30 days)
Stefan Lang on 19 Oct 2020
Commented: J. Alex Lee on 19 Oct 2020
I have some points, annotated from an xray, along the spine. I need to fit a curve through these points. After that, i want to parametrize my fitted curve from 0 to 1, so i can move along the curve in equal intervals. If i just project my points onto the x axis, my line will be distorted.
So i want to choose point 0, which should give me the x/y coordinates of the fitted curve and the slope of the curve at this point. Point 0.5 should give me the point in the middle of the curve and its x/y values and the slope there, and point 1 the last point of the curve.
Any ideas how to do this? I thought about spline/cscvn, but i don't really get it...
Rik on 19 Oct 2020
E.g. this:
subplot(1,2,1)
plot(x,y)
title('actual data')
subplot(1,2,2)
t=linspace(0,1,numel(x));
plot(t,x),hold on,plot(t,y),hold off
title('as a function of t')

J. Alex Lee on 19 Oct 2020
If i understand Rik correctly, I think you'd first have to estimate the t data by computing the cumulative arc length between your x and y data points. But for that to work, your annotated points need to be ordered (from your 0 to 1). If they are ordered, your cummulative arc length can be estimated by something like
function s = cummArcLength(x,y)
s = zeros(size(x));
for i = 2:length(x)
s(i) = s(i-1) + sqrt( ...
(x(i)-x(i-1))^2 ...
+ (y(i)-y(i-1))^2 ...
);
end
end
And then just divide s by s(end) to force it to [0,1].
##### 2 CommentsShowHide 1 older comment
J. Alex Lee on 19 Oct 2020
[Approximate] arc length would be safer if points were very unevenly distributed.
And for completeness, in either case you'd want to do the interpolation one way or another on both x and y parametrically on t
tData = cummArcLength(xData,yData)
xFit = interp1(sData,xData,tFit) % or spline or whatever
yFit = interp1(sData,yData,tFit) % or spline or whatever
If you have a predetermined set of tFit you are interested in. Maybe using something like splines makes it easier to create a function that can be evaluated at arbitrary t