2012-01-18 11 views
0

私はテーブルのXYデータからガウス関数(カーブフィッティング)にカーブフィッティングするアルゴリズムを探しています。私はMatlabのためのいくつかのガウスフィッティングALGOSを見つけることができますグーグルでは、ここではそれらのカップルです:ルアのカーブフィッティング

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

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

一つは、仕事のためのMatlabの「関数polyfit」関数を使用しているようです。

ルア語(ガウスまたはポリフィットのいずれか)のために容易にalgoを作成した人はいますか?もしそうでなければ、おそらく私の限られたLuaスキルで1日を消費するようなアルゴリズムを作成/移植するための助けに感謝します。

答えて

0

式を線形に並べ替えると、ポールバークのLinear Regressionに記載されている方法を使用することができます。

必要に応じて、私はあなたのために並べ替えプロセスを示すことができます。

もしが本当にになる必要があれば、私はLuaで最適線のアルゴリズムの実装を提供することができます。

+0

この再整理プロセスを確認したいと思います。ありがとうございました。 – borges

+0

フィードバックいただきありがとうございます!私はGSLのフィッティング関数を使って何らかの実装を行いました。私はそれを正しく見積もった.gsl-luaの調整を含めて、Windowsで動かすのに1日ほどかかった。ここにコードを掲載します。 – TeroK

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