您可以尝试 lsqcurvefit 精确地进行单次或多次高斯拟合。
x = lsqcurvefit(fun,x0,xdata,ydata)
fun 是您的高斯函数,x0 保存高斯参数(mu、sigma、高度等)的初始值。 fun(x0) 以向量/数组形式返回高斯。当例程返回时,拟合的参数在 x 中。您可以自定义函数 fun 以将一个高斯或多个高斯拟合到您的数据中。
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