How to apply a fit on this shape?

조회 수: 14 (최근 30일)
Niklas Kurz
Niklas Kurz 2020년 7월 15일
이동: John D'Errico 2023년 2월 20일
Since my data doens't really count as a function cftool is not an option. I wonder whether this is still faisable to fit:
(I tried several elliptical fits but they don't seem to work. May be because I dealt with it a little clumsily)

채택된 답변

John D'Errico
John D'Errico 2020년 7월 15일
편집: John D'Errico 2020년 7월 15일
What does it mean to "fit" it to you? What will you do with that fit? The data is not a function, just a set of points, that happen to represent a closed curve in the plane.
A simple solution might be to convert to polar coordinates.
help cart2pol
Now, fit the function r(theta). Be careful, since r is a periodic function. It should have the property that r(0) == r(2*pi). So a polynomial fit would be a bad idea. Instead, perhaps fit this as a sum.
r(theta) = a0 + a1*sin(theta) + b1*cos(theta) + a2*sin(2*theta) + b2*cos(2*theta) + ...
Essentially, I am describing what is essentially a Fourier series there (that is, not an fft, but a Fourier seies.)
As it is constructed, the function ALWAYS has the property of being periodic. And, while you could, in theory, trivially use the curve fitting toolbox for this task, there is no reason to do so. All of the parameters can be estimated using one call to backslash.
x = x(:);
y = y(:);
mux = mean(x);
muy = mean(y);
[Theta,R] = cart2pol(x- mux,y-muy);
[Theta,ind] = sort(Theta);
R = R(ind);
plot(Theta,R,'o')
n = numel(x);
M = 6;
A = [ones(n,1),sin(Theta*(1:M)),cos(Theta*(1:M))];
coeffs = A\R;
Rhat = A*coeffs;
hold on
plot(Theta,Rhat)
Larger values of M will yield a better approximation. A perfect circle would have m == 0. Because your curve has an oval shape, you need to have a few extra terms, but not many. M == 6 will require the estimation of 13 coefficients for the series.
I lack your data, so I generated some random crap data as an example, using ginput.
plot(x,y,'o')
Nothing exciting. Vaguely ovoid. I'm actually surprised it was that good.
R as a function of Theta seems reasonable.
And now, we can reconstruct the original curve.
[xhat,yhat] = pol2cart(Theta,Rhat);
plot(x,y,'bo',xhat + mux,yhat + muy,'-xr')
Surprisingly good for a simple, moderately low order Fourier series approximation, given the data came from my shaky eye/mouse coordinated hand.
  댓글 수: 9
Image Analyst
Image Analyst 2023년 2월 19일
이동: John D'Errico 2023년 2월 20일
@tarek hussein see the attached paper.
Sorry, I don't have MATLAB implementations for any of the shapes.
tarek hussein
tarek hussein 2023년 2월 20일
이동: John D'Errico 2023년 2월 20일
thanks for your response

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile 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!

Translated by