2013-12-14 91 views
0

我目前無法準確地進行高斯擬合。我如何修復高度? (見圖片)。如何用給定高度的Matlab擬合高斯

ft=fit(x,y,'gauss2') 
Co=coeffvalues(ft) 
sigma=Co(3)/sqrt(2) 
mu = Co(2) 
C=Co(1) 

plot(X,C*exp(-(X - mu).^2/(2*sigma^2))+min(y), '-r') 
+1

我看不到圖片。 – Rafa

+0

似乎在R2013a上可以正常工作,但我不知道你的數據是什麼。你可以試試['fitoptions'](http://www.mathworks.com/help/curvefit/fitoptions.html)。 – horchler

回答

0

您可以嘗試lsqcurvefit準確地做單個或多個高斯擬合。

x = lsqcurvefit(fun,x0,xdata,ydata) 

樂趣是您的高斯函數,X0保持高斯參數(畝,西格瑪,身高等)的初始值。 fun(x0)以向量/數組形式返回高斯。例程返回時,擬合參數在x中。您可以自定義功能樂趣以適合一個高斯或多個高斯到您的數據。

Matlab document of lsqcurvefit

在我的情況,我用下面的程序做多高斯擬合:

x0 = [1000;10.6;0.6; 
    1100;12.8;0.7; %3 Gaussians 
    300;10;2];  %each row is the height, mu, sigma of one Gaussian 

options = optimset('TolFun',10e-6,'MaxFunEvals',150000); 
%lb, ub are the similar matrix as x0 that define lower and upper bound of x. 
[x, resnorm] = lsqcurvefit(@myfit, x0, xdata, ydata, lb, ub); 

myfit函數計算多個高斯疊加:

function [ F ] = myfit(x, xdata) 
    F = zeros(1,size(xdata,2)); 
    len = size(x,1); 
    for i = 1:3:len 
     F = F + x(i)*gauss(xdata, x(i+1), x(i+2)); 
    end 
end 

高斯函數:

function [ g ] = gauss(x, mu, sigma) 
    g = exp(-0.5*((x-mu)/sigma).^2); 
end