亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

超高斯擬合

超高斯擬合

阿晨1998 2022-09-06 18:11:37
我必須研究激光束輪廓。為此,我需要為我的數據找到一個超高斯曲線擬合。超高斯方程:I * exp(- 2 * ((x - x0) /sigma)^P)其中考慮了平頂激光束曲線的特性。P我開始用Python對我的曲線進行簡單的高斯擬合。擬合返回一條高斯曲線,其中 和 的值被優化。(我用了函數curve_fit)高斯曲線方程:Ix0sigmaI * exp(-(x - x0)^2 / (2 * sigma^2))現在,我想向前邁出一步。我想做超高斯曲線擬合,因為我需要考慮光束的平頂特性。因此,我需要一個同時優化 P 參數的擬合。有人知道如何用Python做一個超級高斯曲線擬合嗎?我知道有一種方法可以用wolfram mathematica做一個超級高斯擬合,這不是開源的。我沒有。因此,我還想知道是否有人知道一個開源軟件,因此可以進行超級高斯曲線擬合或執行wolfram mathematica。
查看完整描述

4 回答

?
蝴蝶不菲

TA貢獻1810條經驗 獲得超4個贊

好吧,您需要編寫一個函數來計算參數化的超高斯函數,并使用它來對數據進行建模,例如.作為LMFIT(https://lmfit.github.io/lmfit-py/)的主要作者,它提供了一個高級接口來擬合和曲線擬合,我建議嘗試該庫。使用這種方法,超高斯和用于擬合數據的模型函數可能如下所示:scipy.optimize.curve_fit


import numpy as np  

from lmfit import Model   


def super_gaussian(x, amplitude=1.0, center=0.0, sigma=1.0, expon=2.0):

    """super-Gaussian distribution

    super_gaussian(x, amplitude, center, sigma, expon) =

        (amplitude/(sqrt(2*pi)*sigma)) * exp(-abs(x-center)**expon / (2*sigma**expon))

    """

    sigma = max(1.e-15, sigma)

    return ((amplitude/(np.sqrt(2*np.pi)*sigma))

            * np.exp(-abs(x-center)**expon / 2*sigma**expon))


# generate some test data

x = np.linspace(0, 10, 101)

y = super_gaussian(x, amplitude=7.1, center=4.5, sigma=2.5, expon=1.5)

y += np.random.normal(size=len(x), scale=0.015)


# make Model from the super_gaussian function

model = Model(super_gaussian)


# build a set of Parameters to be adjusted in fit, named from the arguments 

# of the model function (super_gaussian), and providing initial values

params = model.make_params(amplitude=1, center=5, sigma=2., expon=2)


# you can place min/max bounds on parameters

params['amplitude'].min = 0

params['sigma'].min = 0

params['expon'].min = 0

params['expon'].max = 100


# note: if you wanted to make this strictly Gaussian, you could set 

# expon=2  and prevent it from varying in the fit:

### params['expon'].value = 2.0

### params['expon'].vary = False


# now do the fit

result = model.fit(y, params, x=x)


# print out the fit statistics, best-fit parameter values and uncertainties

print(result.fit_report())


# plot results

import matplotlib.pyplot as plt

plt.plot(x, y, label='data')

plt.plot(x, result.best_fit, label='fit')

plt.legend()

plt.show()

這將打印一個報告,如


[[Model]]

    Model(super_gaussian)

[[Fit Statistics]]

    # fitting method   = leastsq

    # function evals   = 53

    # data points      = 101

    # variables        = 4

    chi-square         = 0.02110713

    reduced chi-square = 2.1760e-04

    Akaike info crit   = -847.799755

    Bayesian info crit = -837.339273

[[Variables]]

    amplitude:  6.96892162 +/- 0.09939812 (1.43%) (init = 1)

    center:     4.50181661 +/- 0.00217719 (0.05%) (init = 5)

    sigma:      2.48339218 +/- 0.02134446 (0.86%) (init = 2)

    expon:      3.25148164 +/- 0.08379431 (2.58%) (init = 2)

[[Correlations]] (unreported correlations are < 0.100)

    C(amplitude, sigma) =  0.939

    C(sigma, expon)     = -0.774

    C(amplitude, expon) = -0.745

并生成這樣的情節

http://img1.sycdn.imooc.com//63171cf80001c11705980476.jpg

查看完整回答
反對 回復 2022-09-06
?
猛跑小豬

TA貢獻1858條經驗 獲得超8個贊

紐維爾的答案非常適合我。


但要小心!在函數定義中,括號在指數的商中是模糊的super_gaussian


def super_gaussian(x, amplitude=1.0, center=0.0, sigma=1.0, expon=2.0):

    ...

    return ((amplitude/(np.sqrt(2*np.pi)*sigma))

            * np.exp(-abs(x-center)**expon / 2*sigma**expon))

應替換為


def super_gaussian(x, amplitude=1.0, center=0.0, sigma=1.0, expon=2.0):

    ...

    return (amplitude/(np.sqrt(2*np.pi)*sigma))

           * np.exp(-abs(x-center)**expon / (2*sigma**expon))

然后是超高斯函數的FWHM,它寫道:


FWHM = 2.*sigma*(2.*np.log(2.))**(1/expon)

經過精心計算,與情節非常一致。


我很抱歉寫這篇文章作為答案。但是我的聲譽得分很低,無法為M Newville帖子添加評論...


查看完整回答
反對 回復 2022-09-06
?
largeQ

TA貢獻2039條經驗 獲得超8個贊

將 y(x)=a *exp(-b *(x-c)**p) 擬合到參數 a,b,c,p 的數據。

下面的數值演算示例顯示了一種非迭代方法,該方法不需要對參數進行初始猜測。

這在應用一般原理中解釋在論文中:https://fr.scribd.com/doc/14674814/Regressions-et-equations-integrales

在本文的當前版本中,超高斯的情況沒有得到明確的處理。沒有必要閱讀論文,因為下面的屏幕副本顯示了整個細節的微積分。

請注意,數值結果 a,b,c,p 可用作回歸的經典迭代測量的初始值。

http://img1.sycdn.imooc.com//63171d1700019a6508501059.jpg

注意:

考慮的線性方程是:

http://img1.sycdn.imooc.com//63171d240001e14c05290054.jpg

A,B,C,D是由于線性回歸而要計算的參數。積分的數值S(k)通過從給定數據進行數值積分直接計算(如上例所示)。


查看完整回答
反對 回復 2022-09-06
?
慕婉清6462132

TA貢獻1804條經驗 獲得超2個贊

這是超高斯的函數


    def super_gaussian(x, amp, x0, sigma):

        rank = 2

        return amp * ((np.exp(-(2 ** (2 * rank - 1)) * np.log(2) * (((x - x0) ** 2) / ((sigma) ** 2)) ** (rank))) ** 2)

然后你需要用 scipy 優化曲線擬合來調用它,如下所示:


from scipy import optimize


opt, _ = optimize.curve_fit(super_gaussian, x, y)

vals = super_gaussian(x, *opt)

“vals”是你需要繪制的,那就是擬合的超高斯函數。

這是您在 rank=1 時得到的:在此輸入圖像描述

排名 = 2在此輸入圖像描述

排名 = 3在此輸入圖像描述


查看完整回答
反對 回復 2022-09-06
  • 4 回答
  • 0 關注
  • 501 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號