2016-04-25 143 views
2

我目前插值用三次样条函数的测量如图所示的画面:更Python的方式寻找曲线交点

Interpolation of a normalised harmonic signal using cubic splines.

的想法是我想要找的全宽半最大插值的。对于我使用的一小段代码

f = interpolate.interp1d(hhg_data, signal_array, kind='cubic') 
idx2 = np.argsort(np.abs(f(hhg_interp)-0.5)) 

返回我的路口排序指标与线y=0.5。但是,我想要在曲线的左边缘和右边缘的解决方案,有时它会给我两个连续的点。有没有一种优雅的pythonic方式来避免这种情况?至少比我的哈克解决方案好多了:

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 

谢谢你的回答!

回答

0

假设hhg_interp进行排序,并且只有一个最大值和希望做一个gridsearch(即计算出你在离散点的功能,并使用这些值工作),我会做到以下几点:

# 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 

是你正在寻找的信息?

+0

不太确定,hhg_interp基本上是一个x轴变量的np.array。在这种情况下,你可以把它作为hhg_interp = linspace(18.5,19.5,10000),只需要一个精细的网格来绘制f。 – Roland

+0

增加了一些用于计算左右边缘索引的代码以及一些注释。这有助于澄清? – cobaltfiftysix

+0

好的,我明白了。感谢它的工作很好。也许我会解决这个想法,最好的适应我的兴趣。 – Roland