问题描述
我正在尝试将洛仑兹峰拟合到数据集中的一个峰。
我们得到了适合高斯的拟合,除了实际的拟合方程外,代码非常相似,因此我不确定我要去哪里。我没有看到为什么在使用curve_fit
时尺寸出现问题。
以下是我的代码中的相关部分,可以更好地了解我在说什么。
读取CSV文件并对其进行修剪
import csv
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from matplotlib.ticker import StrMethodFormatter
#reading in the csv file
with open("Data-Oscilloscope.csv") as csv_file:
csv_reader = csv.reader(csv_file,delimiter=",")
time =[]
voltage_raw = []
for row in csv_reader:
time.append(float(row[3]))
voltage_raw.append(float(row[4]))
print("voltage:",row[4])
#trimming the data
trim_lower_index = 980
trim_upper_index = 1170
time_trim = time[trim_lower_index:trim_upper_index]
voltage_trim = voltage_raw[trim_lower_index:trim_upper_index]
给出的高斯拟合
#fitting the gaussian function
def gauss_function(x,a,x0,sigma):
return a*np.exp(-(x-x0)**2/(2*sigma**2))
popt,pcov = curve_fit(gauss_function,time_trim,voltage_trim,p0=[1,.4,0.1])
perr = np.sqrt(np.diag(pcov))
#plot of the gaussian fit
plt.figure(2)
plt.plot(time_trim,gauss_function(time_trim,*popt),label = "fit")
plt.plot(time_trim,"-b")
plt.show()
我尝试的洛伦兹式拟合
#x is just the x values,a is the amplitude,x0 is the central value,and f is the full width at half max
def lorentz_function(x,f):
w = f/2 #half width at half max
return a*w/ [(x-x0)**2+w**2]
popt,pcov = curve_fit(lorentz_function,0.1])
运行此程序时出现错误,提示:
in至少sq引发TypeError(“输入错误:N =%s不能超过M =%s'%(n,m)) TypeError:输入错误:N = 3不能超过M = 1
我可能会丢失一些非常明显的东西,但看不到它。
提前谢谢! 编辑:我看了其他类似的问题并进行了解释,但看不到它们与我的代码如何匹配,因为输入的参数数量和维数应该很好,因为它们适用于高斯拟合。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)