2012-01-18 162 views
0

我正在尋找用於從表格XY數據到高斯函數(又名鐘形曲線)進行曲線擬合的算法。谷歌搜索我能找到一些高斯擬合交易算法的MATLAB,這裏是他們夫婦:Lua上的曲線擬合

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

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

人似乎用Matlab的「polyfit」功能的工作。

任何人都看到很容易使用算法語言(高斯或polyfit)算法?如果不是的話,我會非常感謝人們幫助創建/移植這樣的算法,因爲它可能會消耗我有限的Lua技能的一天。

回答

0

您可以重新排列方程爲線性形式,然後使用Paul Bourke的Linear Regression中描述的方法(稍微向下)。

如果您需要,我可以爲您演示重新安排的過程。

如果你確實需要,我可以在Lua中提供最佳擬合算法的實現。

+0

如果您不介意,我希望看到這個重新安排過程。謝謝。 – borges 2012-01-18 22:03:07

+0

感謝您的反饋!我設法通過擬合GSL的功能來實現某種實現。我估計它是正確的,花費了將近一天的時間,包括gsl-lua調整,以使其在Windows下運行。我會在這裏發佈代碼。 – TeroK 2012-01-19 16:46:27

1

這是我嘗試從噪聲測量數據中提取高斯擬合。

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