如何在Matlab/Octave中将高斯拟合到数据? [英] How to fit a gaussian to data in matlab/octave?

查看:422
本文介绍了如何在Matlab/Octave中将高斯拟合到数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组带有峰值的频率数据,需要拟合高斯曲线,然后从中获取全宽的一半最大值.我可以做的FWHM部分,我已经有相应的代码,但是在编写适合高斯的代码时遇到了麻烦.

I have a set of frequency data with peaks to which I need to fit a Gaussian curve and then get the full width half maximum from. The FWHM part I can do, I already have a code for that but I'm having trouble writing code to fit the Gaussian.

有人知道有什么功能可以帮我实现这一目标,或者可以为我指明正确的方向吗? (我可以对线和多项式进行最小二乘拟合,但对高斯不起作用)

Does anyone know of any functions that'll do this for me or would be able to point me in the right direction? (I can do least squares fitting for lines and polynomials but I can't get it to work for gaussians)

如果它同时与Octave和Matlab兼容(因为我目前具有Octave,但要等到下周才能访问Matlab),这也会有所帮助.

Also it would be helpful if it was compatible with both Octave and Matlab as I have Octave at the moment but don't get access to Matlab until next week.

任何帮助将不胜感激!

推荐答案

直接拟合一个一维高斯是一个非线性拟合问题.您可以在此处此处,或(如果您有统计信息工具箱)(您听说过Google吗?:)

Fitting a single 1D Gaussian directly is a non-linear fitting problem. You'll find ready-made implementations here, or here, or here for 2D, or here (if you have the statistics toolbox) (have you heard of Google? :)

无论如何,可能会有一个更简单的解决方案.如果您确定数据y将由高斯很好地描述,并且在整个x范围内分布合理,则可以线性化问题(这些是方程式,而不是语句):

Anyway, there might be a simpler solution. If you know for sure your data y will be well-described by a Gaussian, and is reasonably well-distributed over your entire x-range, you can linearize the problem (these are equations, not statements):

   y = 1/(σ·√(2π)) · exp( -½ ( (x-μ)/σ )² )
ln y = ln( 1/(σ·√(2π)) ) - ½ ( (x-μ)/σ )²
     = Px² + Qx + R         

替换之处

P = -1/(2σ²)
Q = +2μ/(2σ²)    
R = ln( 1/(σ·√(2π)) ) - ½(μ/σ)²

已完成.现在,用(这些是Matlab语句)求解线性系统Ax=b:

have been made. Now, solve for the linear system Ax=b with (these are Matlab statements):

% design matrix for least squares fit
xdata = xdata(:);
A = [xdata.^2,  xdata,  ones(size(xdata))]; 

% log of your data 
b = log(y(:));                  

% least-squares solution for x
x = A\b;                    

以这种方式发现的向量x将等于

The vector x you found this way will equal

x == [P Q R]

然后您必须进行逆向工程以找到平均值μ和标准偏差σ:

which you then have to reverse-engineer to find the mean μ and the standard-deviation σ:

mu    = -x(2)/x(1)/2;
sigma = sqrt( -1/2/x(1) );

您可以与x(3) == R进行交叉检查(差异应该只有).

Which you can cross-check with x(3) == R (there should only be small differences).

这篇关于如何在Matlab/Octave中将高斯拟合到数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆