How can I perform a piecewise linear fit to data?
조회 수: 21 (최근 30일)
이전 댓글 표시
I have y-axis data that varies linearly in three regions over 0-1 on the x-axis. I would like to obtain a simple piecewise linear fit to get the 2-break points and the three linear fits. Is there a function that I could use? I have found nlhw that might do it, but I cannot figure out how to use it from the documentation. Would appreciate any help in using it or any other alternative.
댓글 수: 0
답변 (2개)
Greg Dionne
2016년 7월 8일
If you have the R2016a Signal Processing Toolbox, you can try FINDCHANGEPTS.
댓글 수: 1
Tong Zhao
2022년 6월 11일
Good idea Greg. I assume the function can also be used to reverse-find the individual clothoid segment parameters in a Frenet-fitted curve? That is, after fitting a sequence of waypoints with FrenetReferencePath(waypoints) class constructor, one has the [x,y,theta,k,dk,s] sequence of the fitted curve.If one wish to find the length and dkappa/ds of each clothoid piece component programmatically, one can plot the dk-s curve and perform a FINDCHANGEPTS on that dk-s curve to find the length of each clothoid segment, right? Or do you think there can be a more direct method of reverse-finding the parameters of each clothoid component of a multi-clothoid curve without having to use FINDCHANGEPTS?
A piece of example is the following:
waypoints = ...
[-1.20, -0.30;
-3.70, -15.90;
-2.90, -35.10;
5.90, -52.40;
24.50, -66.90;
41.80, -70;
64.30, -67.20;
78.50, -57.60;
87.30, -47.80;
93.40, -30.70;];
frenetFit = nav.algs.internal.FrenetReferencePath(waypoints);
pathPoints = frenetFit.interpolate(0:0.01:100); % first 100 meters of fitted curve
x = pathPoints(:,1);
y = pathPoints(:,2);
% plot the waypoints and fitted frenet curve
figure();plot(waypoints(:,1),waypoints(:,2),'ro');hold on; plot(x,y,'b-');
% plot the dkds-s curve
dkds = pathPoints(:,5);
s = pathPoints(:,6);
figure();plot(s,dkds,'b-');
% now we use findchangepts() to find the corner points on the above figure
% by observation, there are 5 such corner points in the first 100 meters,
% so a MaxNumChanges of 5 is sufficient
ipt = findchangepts(dkds,'MaxNumChanges',5)
% then using the indices found by findchangepts, we can find the arclength
% locations where two adjacent clothoids connect
s(ipt)
Image Analyst
2015년 5월 19일
Do you have the location of the breakpoints? If so, simply use polyfit(). If not, then you should fit a line through some number of points, like 3 or 5, and scan that across your data. You can get the estimates slope at every location. Then plot the slopes and look for where it changes a lot. You can use diff() to find out where the slope changes substantially. Attach some sample data if you want people to kindly help you by writing code for you.
댓글 수: 3
Image Analyst
2021년 6월 23일
@Priyank Pathak, this does not look like a piecewise linear plot:
It looks more like an exponential decay. I'm attaching a demo for that. Or if piecewise linear, just two regions, not three, and so my existing code should work. You may have to adjust the range over where the split occurs.
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!