找到曲线交点的一种更具python风格的方法
我目前正在使用三次样条插值测量值,如图所示: 这个想法是我想找到插值的全宽一半最大值。为此,我使用了一小段代码找到曲线交点的一种更具python风格的方法,python,sorting,interpolation,Python,Sorting,Interpolation,我目前正在使用三次样条插值测量值,如图所示: 这个想法是我想找到插值的全宽一半最大值。为此,我使用了一小段代码 f = interpolate.interp1d(hhg_data, signal_array, kind='cubic') idx2 = np.argsort(np.abs(f(hhg_interp)-0.5)) 它返回与线y=0.5相交的排序索引。但是,我想要曲线左边缘和右边缘的解,有时它会返回两个连续的点。有没有一种优雅的蟒蛇式方法可以避免这种情况?至少比我的黑客解决方案好得
f = interpolate.interp1d(hhg_data, signal_array, kind='cubic')
idx2 = np.argsort(np.abs(f(hhg_interp)-0.5))
它返回与线y=0.5
相交的排序索引。但是,我想要曲线左边缘和右边缘的解,有时它会返回两个连续的点。有没有一种优雅的蟒蛇式方法可以避免这种情况?至少比我的黑客解决方案好得多:
idx_sorted = []
counter = 0
counter2 = 0
while(counter <= 1):
if idx2[counter2] != idx2[counter2-1]+1 and idx2[counter2] != idx2[counter2-1]-1:
idx_sorted.append(idx2[counter2])
counter+=1
counter2+=1
idx_排序=[]
计数器=0
计数器2=0
虽然(计数器假设hhg_interp
已排序且只有一个最大值,并且希望进行网格搜索(即在离散点计算函数并使用这些值),但我将执行以下操作:
# hhg_interp could be something like
# hhg_interp = linspace(18.5, 19.5, 10000)
y = f(hhg_interp) # hhg_interp is an array filled with finely
# spaced x-axis values
# get the indices for all y larger than the half maximum (0.5 in this case)
indices_above_fwhm = np.where(y>0.5)[0]
# if you're interested in the indices
# the first element of the "indices larger then the half maximum" array
# corresponds to your left edge
index_left_edge = indices_above_fwhm[0]
# the last element of the "indices larger then the half maximum array"
# corresponds to your right edge
index_right_edge = indices_above_fwhm[-1]
# then you can get the corresponding x-axis values for these indices:
x_left = hhg_interp[index_left_edge]
x_right = hhg_interp[index_right_edge]
# or directly get the x-axis values of the left and right edges
cond = y > 0.5 # select all points that are above your half maximum
x_left, x_right = hhg_interp[cond][[0,-1]] # then get the first
# and last of the points above maximum
这就是您要查找的信息吗?不太确定,hhg_interp基本上是一个x轴变量的np.数组。在这种情况下,您可以将其视为hhg_interp=linspace(18.5,19.5,10000)只是为了有一个精细的网格来绘制f。添加了一些用于计算左右边缘索引的代码,以及一些注释。这有助于澄清吗?啊,好的,我明白了。谢谢,它工作得很好。也许我会围绕这个想法工作,以使它最适合我感兴趣的案例。