How can I perform a piecewise linear fit to data?
이전 댓글 표시
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.
답변 (2개)
Greg Dionne
2016년 7월 8일
1 개 추천
If you have the R2016a Signal Processing Toolbox, you can try FINDCHANGEPTS.
댓글 수: 1
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일
0 개 추천
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
Priyank Pathak
2021년 6월 23일
편집: Image Analyst
2021년 6월 23일
Please can you solve my issue?
I have same issue as @Ranjan Sonalkar
I have y-axis data that varies nearly linearly in three regions over 0-0.6 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? Or can you develop some code?
From left(x=0) the first slope should be around 95, and the second clope around 33.
x=linspace(0,0.06,129);
y=[5.07742010175952
5.31185099410990
5.10691673392019
4.74615958521947
4.19177682576652
3.37068344802045
2.37609787828215
2.12259276016394
1.85196259843330
1.56974576757917
1.48983435635157
1.50485054453710
1.20735946054088
0.661344454963583
1.13352524509828
1.06028957687000
0.599328807494000
1.03613472954618
0.987301049044117
0.217298697691901
0.920810108362981
1.05394744136997
0.281153643572616
0.469681534710498
0.897003388264222
0.327421850031165
0.183590736373175
0.857998112549593
0.581121685434373
-0.125993250940098
0.683500160910840
0.534539058937860
-0.396726323212621
0.634482480611096
0.658943411183352
-0.438952316218105
0.345284196220544
0.600667158720749
-0.115141464323266
0.120829134737583
0.548098646887191
0.0463178869874998
-0.113048252136384
0.512689921996228
0.231986574873253
-0.207338103120708
0.504643103976643
0.434781176498638
-0.0110627611202763
0.489236905438070
0.571542855334634
0.195839245448528
0.318850093243477
0.454160515353964
0.142640929539695
0.310785155059393
0.512602213738191
0.162014480668000
-0.0607597283337748
0.358834860427813
0.138829100632712
-0.384114894557834
0.196010083174632
0.0877202333255821
-0.439559931012924
0.240175548664580
0.281973380388760
-0.417220964330667
-0.0796052840983549
0.154742142117545
-0.393344891329877
-0.0605607647867973
0.277797879414859
-0.0511663599311094
-0.188885994780561
0.236644526326914
0.0320718007648540
-0.420498358462674
0.116044715556200
0.0456658813295294
-0.481396963008032
0.0686941440278348
0.134350947055467
-0.392697383096293
-0.0822493346453244
0.0840913913909094
-0.560007006542937
-0.309347358363015
0.100439695825144
-0.305329002501373
-0.634393763791120
0.00828091935610617
-0.199591976608657
-0.527071260057054
0.0538501718509587
-0.0536584297550401
-1.06586660939429
-0.192609804740431
-0.117750795306160
-1.23944999109543
-0.238042348232044
0.0205644121511571
-0.516135755182763
-0.362229685328809
0.00351231197974391
-0.465633808298506
-0.632540256198356
0.0215932583608414
-0.187152394729513
-1.07708193704972
-0.198132189949225
-0.270724057886369
-0.967609157331435
-0.109900894024127
-0.0638341059632911
-1.06220258506714
-0.387954592612164
-0.115951129740455
-0.927157022631441
-0.440143390491659
0.0171476050596097
-0.365295340437865
-0.922551001315555
-0.179188776250813
-0.469657233191098
-0.965827555014108
-0.0694730732513197
-0.154504170420157
-1.25531875940010]
Thanks in advance.
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.
Priyank Pathak
2021년 6월 23일
카테고리
도움말 센터 및 File Exchange에서 Descriptive Statistics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

