Gaussian non-linear fitting
조회 수: 5 (최근 30일)
이전 댓글 표시
Hello everyone,
To start my question let me just focus on the fact that I'm a novice when it comes to programming and especially Matlab.
I'm currently struggling in finding a way to non-linear fit my data using two Gaussian surfaces. The data is a 3d plot : http://imageshack.us/photo/my-images/268/3dplot.jpg/ where we are measuring the current in function of the position over a surface.
Thanks for the help!
댓글 수: 0
답변 (2개)
Image Analyst
2012년 3월 28일
What I'd probably do is to take a histogram of your data to determine where the "floor," "baseline," "background value" or whatever you want to call the flat part's value. Then I'd threshold slightly above that to get a binary map of where the two peaks are. Then I'd label the binary image with bwlabel. Then you can find out which pixels belong to peak 1 or peak 2 (using find() or regionprops()). Knowing those pixels (their values and x,y locations) I think you can take the log of your data and do a simple linear least squares to get all the parameters in the equation
g(x,y) = amplitude * exp(-((x-xctr)^2+(y-yctr)^2)/sigma^2)
in other words, you can get amplitude, xctr, yctr, and sigma. After you take the log, it's linear in all the coefficients so I don't see why any non-linear stuff is needed. Here's a snippet from a demo of mine that may help you:
% Do a least squares fit of the histogram to a Gaussian.
% Assume y = A*exp(-(x-mu)^2/sigma^2)
% Take log of both sides
% log(y) = (-1/sigma^2)*x^2 + (2*mu/sigma^2)*x + (log(A)-mu^2/sigma^2)
% Which is the same as
% lny = a1*x^2 + a2*x + a3
% Now do the least squares fit.
nonZeroBins = counts > 0;
lny = log(counts(nonZeroBins));
coeffs = polyfit(bins(nonZeroBins), lny, 2)
% Find the Gaussian parameters from the least squares parameters.
sigma = sqrt(-1/coeffs(1))
mu = coeffs(2) * sigma^2/2
amplitude = exp(coeffs(3) + mu^2/sigma^2)
% Now we have all 3 Gaussian parameters,
% so reconstruct the data from the Gaussian model.
xModeled = linspace(bins(1), bins(end), 100);
yModeled = amplitude * exp(-(xModeled - mu).^2 / sigma^2);
참고 항목
카테고리
Help Center 및 File Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!