Lua 上的曲线拟合

发布于 2024-12-27 11:17:34 字数 545 浏览 1 评论 0原文

我正在寻找从表格 XY 数据到高斯函数(又名钟形曲线)的曲线拟合算法。通过谷歌搜索,我可以找到一些 Matlab 的高斯拟合算法,这里有几个:

https: //ccrma.stanford.edu/~jos/sasp/Fitting_Gaussian_Data.html

http://jila.colorado.edu/bec/BEC_for_everyone/matlabfitting.htm

一个似乎使用 Matlab 的“polyfit”函数来完成这项工作。

有人见过现成的 Lua 语言算法(高斯或 Polyfit)吗?如果没有,我将非常感谢您帮助创建/移植此类算法,因为以我有限的 Lua 技能,它可能会花费一天的时间。

I'm looking for algorithms to do curve fitting from tabular XY data to a Gaussian function (a.k.a. bell curve). By googling I can find few gaussian fitting algos for Matlab, here are couple of them:

https://ccrma.stanford.edu/~jos/sasp/Fitting_Gaussian_Data.html

http://jila.colorado.edu/bec/BEC_for_everyone/matlabfitting.htm

One seems to use 'polyfit' function of Matlab for the job.

Anyone seen readily made algo for Lua language (either gaussian or polyfit)? If not, I would greatly appreciate one's help for creating/porting such algorithm as it would probably consume a day with my limited Lua skills.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一城柳絮吹成雪 2025-01-03 11:17:34

这是我尝试从噪声测量数据中提取高斯拟合。

require 'gsl'
require 'math'

--x=x coordinates, y=y coordinates
--clip=relative clip/ignore level 0..1 (i.e 0.1 removes values below 10% of max amplitide)
--removeoffset=set to true if y data offset should be removed
function gaussianFit( x, y, clip, removeoffset )
    local xx = {}
    local yy = {}
    local yoffset=0

    if removeoffset==nil or removeoffset==false then
    else --remove y data offset
        yoffset=gsl.Vector(y):min()
    end

    local ymax=gsl.Vector(y):max()-yoffset

    --pick only data points that has y coord larger than clip level
    for i=1,#x do
        if (y[i]-yoffset) > (clip*ymax) then
            table.insert(xx, x[i])
            table.insert(yy, math.log(y[i]-yoffset))
        end
    end

    local xvect = gsl.Vector(xx)
    local yvect = gsl.Vector(yy)

    --fit to polynomial
    local poly3 = gsl.fit.poly(3) -- a third degree polynomial
    local fit = gsl.lsfit({xvect, poly3}, yvect, nil, "fmulti") -- fits xx and yy with poly3

    --convert to gauss coeffs
    local A2=fit:coeffs()[3]
    local A1=fit:coeffs()[2]
    local A0=fit:coeffs()[1]

    local sigma=math.sqrt(-1/(2*A2))
    local mu=A1*math.pow(sigma,2)
    local A=math.exp(A0+math.pow(mu,2)/(2*math.pow(sigma,2)))

    return sigma, mu, A
end

xx={1, 2, 3, 4, 5, 6, 7, 8, 9}
yy={1, 2, 4, 6, 4, 3, 2, 1, 1}
sigma,mu,A=gaussianFit(xx,yy,0.1,false)
print(sigma.." "..mu.." ".. A)
--prints 2.2829275461334 4.6387484511153 4.201115115886

Here's my attempt to extract gaussian fit out of noisy measurement data.

require 'gsl'
require 'math'

--x=x coordinates, y=y coordinates
--clip=relative clip/ignore level 0..1 (i.e 0.1 removes values below 10% of max amplitide)
--removeoffset=set to true if y data offset should be removed
function gaussianFit( x, y, clip, removeoffset )
    local xx = {}
    local yy = {}
    local yoffset=0

    if removeoffset==nil or removeoffset==false then
    else --remove y data offset
        yoffset=gsl.Vector(y):min()
    end

    local ymax=gsl.Vector(y):max()-yoffset

    --pick only data points that has y coord larger than clip level
    for i=1,#x do
        if (y[i]-yoffset) > (clip*ymax) then
            table.insert(xx, x[i])
            table.insert(yy, math.log(y[i]-yoffset))
        end
    end

    local xvect = gsl.Vector(xx)
    local yvect = gsl.Vector(yy)

    --fit to polynomial
    local poly3 = gsl.fit.poly(3) -- a third degree polynomial
    local fit = gsl.lsfit({xvect, poly3}, yvect, nil, "fmulti") -- fits xx and yy with poly3

    --convert to gauss coeffs
    local A2=fit:coeffs()[3]
    local A1=fit:coeffs()[2]
    local A0=fit:coeffs()[1]

    local sigma=math.sqrt(-1/(2*A2))
    local mu=A1*math.pow(sigma,2)
    local A=math.exp(A0+math.pow(mu,2)/(2*math.pow(sigma,2)))

    return sigma, mu, A
end

xx={1, 2, 3, 4, 5, 6, 7, 8, 9}
yy={1, 2, 4, 6, 4, 3, 2, 1, 1}
sigma,mu,A=gaussianFit(xx,yy,0.1,false)
print(sigma.." "..mu.." ".. A)
--prints 2.2829275461334 4.6387484511153 4.201115115886
℉服软 2025-01-03 11:17:34

您可以将方程重新排列为线性形式,然后使用 Paul Bourke 的线性回归中描述的方法(页面下方一点)。

如果您需要的话,我可以为您演示重新排列的过程。

如果您确实需要,我可以提供 Lua 中最佳拟合线算法的实现。

You can rearrange the equation to a linear form, and then use the methods described in Linear Regression by Paul Bourke (a little down the page).

If you need, I can demonstrate the rearranging process for you.

If you really need, I can provide an implementation of the line-of-best-fit algorithm in Lua.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文