如何在给定高度下用Matlab拟合高斯 [英] How to fit gaussian with Matlab with given height
问题描述
我目前无法获得精确的高斯拟合.如何确定高度? (见图).
I am currently unable to have accurate gaussian fit. How can I fix the height? (see picture).
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')
推荐答案
您可以尝试 lsqcurvefit 准确地进行一次或多次高斯拟合.
You can try lsqcurvefit to do single or multiple Gaussian fitting accurately.
x = lsqcurvefit(fun,x0,xdata,ydata)
乐趣是您的高斯函数, x0 保存高斯参数的初始值(mu,sigma,height等). fun(x0)以矢量/数组形式返回高斯.当例程返回时,拟合的参数位于 x 中.您可以自定义功能乐趣以使一个或多个高斯适合您的数据.
fun is your Gaussian function, x0 holds the initial value of the Gaussian parameters (mu, sigma, height, etc). fun(x0) return the gaussian in vector/array form. When the routine returns, the fitted parameters are in x. You can customize the function fun to fit one Gaussian or multiple Gaussians to your data.
在我的情况下,我使用以下例程进行多次高斯拟合:
In my case, I use the following routine to do multiple Gaussian fitting:
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 函数,用于计算多个高斯的叠加:
The myfit function that calculates superposition of multiple Gaussians:
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
这篇关于如何在给定高度下用Matlab拟合高斯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!