博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python如何拟合函数_用python中的voigt函数拟合数据
阅读量:5253 次
发布时间:2019-06-14

本文共 1527 字,大约阅读时间需要 5 分钟。

我真的不知道你在做什么或试图做什么,但我会这样做(假设西格玛和伽马对所有峰值都是相同的)。如果这在法布里·珀罗身上说得通的话,我想得不多)import numpy as np

import matplotlib.pyplot as plt

from scipy.optimize import leastsq

def cauchy(x, x0, g):

return 1. / ( np.pi * g * ( 1 + ( ( x - x0 )/ g )**2 ) )

def gauss( x, x0, s):

return 1./ np.sqrt(2 * np.pi * s**2 ) * np.exp( - (x-x0)**2 / ( 2 * s**2 ) )

def pseudo_voigt( x, x0, s, g, a ):

fg = 2 * s * np.sqrt( 2 * np.log(2) )

fl = 2 * g

f = ( fg**5 + 2.69269 * fg**4 * fl + 2.42843 * fg**3 * fl**2 + 4.47163 * fg**2 * fl**3 + 0.07842 * fg * fl**4+ fl**5)**(1./5.)

eta = 1.36603 * ( fl / f ) - 0.47719 * ( fl / f )**2 + 0.11116 * ( f / fl )**3

return a * ( eta * cauchy( x, x0, f) + ( 1 - eta ) * gauss( x, x0, f ) )

def all_peaks(x, mus, amps, s, g ):

out = 0

for m, a in zip( mus, amps ):

out += pseudo_voigt( x, m, s, g, a )

return out

def res( params, xData, yData):

mus = params[0:5]

amp = params[5:10]

sig = params[-3]

gam = params[-2]

off = params[-1]

yth = np.fromiter( ( abs( off ) + all_peaks( x , mus, amp, sig, gam) for x in xData ), np.float )

diff = yth - yData

return diff

sigma, gamma = 0.007, 0.02

offset = 0.01

muList = [ 0.5, 2.6, 4.8, 6.8, 8.9 ]

ampList = [ .135 ] * 5

data = np.loadtxt( 'calibration.txt' )

x = data[ :,0 ]

y = data[ :,1 ]

sol, err = leastsq( res, muList + ampList + [sigma , gamma, offset ], args=(x, y) )

print sol

plt.xlabel( "Voltage [V]" )

plt.ylabel( "Intensity" )

plt.plot( x,y,ls='', marker='o' )

plt.plot( x, sol[-1] + all_peaks( x, sol[0:5],sol[5:10], sol[-3], sol[-2]) )

plt.show()

这给了

^{pr2}$

下面的图表

99ca19aec279ca3843ccb0cd7ead046d.png

转载地址:http://oreav.baihongyu.com/

你可能感兴趣的文章