2017-10-11 12 views
0

LMFIT 라이브러리 인 을 사용하여 멀티 로렌츠 피팅을 만들려고합니다. 그러나 작동하지 않으며 심지어 내가 의 구문이 completelly 잘못되었음을 이해합니다. ,하지만 나는 새로운 아이디어가 없습니다. 내가 피크의 여러 세트와 매우 긴 스펙트럼을 가지고 있지만, 피크의 수는 너무 가끔 난 그냥 1 피크를해야합니다,이 세트에서 일정하지, 그러나 때때로 나는 8이있을 수 있습니다LMFIT를 사용하여 다중 피크 함수를 피팅

내 문제는 이것이다 또는

#function definition: 

def Lorentzian(x, amp, cen, wid, n): 
    f = 0 
    for i in range(int(n)): 
     "lorentzian function: wid = half-width at half-max" 
     f += (amp[i]/(1 + ((x-cen[i])/wid[i])**2)) 
    return f 

#library import and model definition: 

import lmfit as lf 

lmodel = lf.Model(Lorentzian) 

#The initial parameters for the model: 
peaks_in_interval = np.array([2378, 2493, 2525, 2630, 2769]) 

number_of_peaks = len(peaks_in_interval)   
amplitude = width = np.zeros(number_of_peaks) + 1 
center = x[peaks_in_interval] 

params = lmodel.make_params(x = x, amp = amplitude, cen = center, wid = width, n = number_of_peaks) 

#This is the line that doesn't work:       
result = lmodel.fit(y, params, x = x) 

나는 멀티 로렌 시안을 반환하는 일반적인 기능을 만들려고 노력하기 시작했지만, 나는 그 일을하는 방법에 어려움을 겪고있어도 (20) ...

나 ' 또한 x, y 배열에 대한 데이터를 보냅니다.

DataSet for x and y

This is what the DataSet of x and y looks like.

답변

1

당신은 내장 된 모델의 사용과 manual에 설명 된대로 접두사를 사용을 할 수 있어야합니다. 또한 mailinglist과 비슷한 주제에 대한 최근 토론이있었습니다.

다음과 같이 할 수 있습니다. 그것은 아직 마지막 피크에 잘 맞지 않지만 시작 값 등을 가지고 조금 돌아볼 수 있습니다. 또한 기준이 완전히 평평하지 않으므로 ConstantModel 대신 LinearModel을 사용하면 개선 될 수 있지만 시도하지는 않았습니다.

from lmfit.models import LorentzianModel, ConstantModel 
import numpy as np 
import matplotlib.pyplot as plt 

x, y = np.loadtxt('Peaks.txt', unpack=True) 

peaks_in_interval = np.array([43, 159, 191, 296, 435, 544]) 
number_of_peaks = len(peaks_in_interval) 
amplitude = y[peaks_in_interval]/5 
width = np.zeros(number_of_peaks) + 0.1 
center = x[peaks_in_interval] 

def make_model(num): 
    pref = "f{0}_".format(num) 
    model = LorentzianModel(prefix = pref) 
    model.set_param_hint(pref+'amplitude', value=amplitude[num], min=0, max=5*amplitude[num]) 
    model.set_param_hint(pref+'center', value=center[num], min=center[num]-0.5, max=center[num]+0.5) 
    model.set_param_hint(pref+'sigma', value=width[num], min=0, max=2) 
    return model 

mod = None 
for i in range(len(peaks_in_interval)): 
    this_mod = make_model(i) 
    if mod is None: 
     mod = this_mod 
    else: 
     mod = mod + this_mod 

offset = ConstantModel() 
offset.set_param_hint('c', value=np.average(y[-75:])) 
mod = mod + offset 

out=mod.fit(y, x=x, method='nelder') 
plt.interactive(True) 
print(out.fit_report()) 
plt.plot(x, y) 
plt.plot(x, out.best_fit, label='best fit') 
plt.plot(x, out.init_fit, 'r--', label='fit with initial values') 
plt.show() 
+0

고마워요. 궁금하다면, 나는 peakutils.baseline 모듈을 사용하여 간격의 평균 기준선을 만듭니다. 베이스 라인에 맞게 n 차 다항식을 만듭니다 (일반적으로 n = 2 사용). –