21 lines
464 B
Python
21 lines
464 B
Python
|
import lmfit
|
||
|
import numpy as np
|
||
|
|
||
|
pv = lmfit.models.PseudoVoigtModel()
|
||
|
|
||
|
def fitPeak(x,y,autorange=False):
|
||
|
if autorange:
|
||
|
# find fwhm
|
||
|
idx = np.ravel(np.argwhere( y<y.max()/2 ))
|
||
|
# find first crossing
|
||
|
p1 = idx[idx<np.argmax(y)][-1]
|
||
|
p2 = idx[idx>np.argmax(y)][0]
|
||
|
c = int( (p1+p2)/2 )
|
||
|
dp = int( np.abs(p1-p2) )
|
||
|
idx = slice(c-dp,c+dp)
|
||
|
x = x[idx]
|
||
|
y = y[idx]
|
||
|
pars = pv.guess(y,x=x)
|
||
|
ret = pv.fit(y,x=x,params=pars)
|
||
|
return ret
|