【问题标题】:How to fit gaussian with Matlab with given height如何用给定高度的Matlab拟合高斯
【发布时间】:2015-12-06 02:03:33
【问题描述】:

我目前无法进行准确的高斯拟合。我怎样才能固定高度? (见图)。

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') 

【问题讨论】:

  • 我没有看到图片。
  • 在 R2013a 上对我来说似乎工作正常,但我不知道你的数据是什么。你可以试试fitoptions

标签: matlab gaussian


【解决方案1】:

您可以尝试 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

【讨论】:

    猜你喜欢
    • 2012-10-19
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    • 2013-10-12
    • 2017-08-30
    • 1970-01-01
    相关资源
    最近更新 更多