首页 > 解决方案 > 如何在python中获取轴坐标

问题描述

我正在尝试实现峰值检测,它收集信号的全局峰值和谷值,但是,当我将其实现为一个简单的信号时,该功能可以完美运行,没有错误。但是,当我将代码运行到大量数据集(大约 9000 个样本)中时,它一直给我这个错误:

TypeError: only integer scalar arrays can be converted to a scalar index

我的峰值检测功能如下:

def custom_peakdetection_envelope(y_axis, peak_height, x_axis=None):
    """
    keyword arguments:
    y_axis -- A list containing the signal over which to find peaks
    x_axis -- (optional) A x-axis whose values correspond to the 'y_axis' list and is used in the return to specify the position of the peaks. If omitted the index of the y_axis is used. (default: None)
    peak_height --  This specifies a minimum height of the peak
    
    return -- two lists [maxtab, mintab] containing the positive and negative peaks respectively. Each cell of the lists contains a tuple of:
    (position, peak_value) to get the average peak value do 'np.mean(maxtab, 0)[1]' on the results
    """
    
    global amplitude_envelope_y
    global amplitude_envelope_x
    
    maxtab = []
    mintab = []
    maxthresh = []
    minthresh = []
    ax = []
    amplitude_envelope_mx = []
    amplitude_envelope_mn = []

    if x_axis is None:
        x = arange(len(y_axis))
    else:
        x = asarray(x_axis)

    y = asarray(y_axis)
    ax.append((x,y))

    if len(y) != len(x):
        sys.exit('Input vectors y and x must have same length')

    if not isscalar(peak_height):
        sys.exit('Input argument peak_height must be a scalar')

    # if peak_height <= 0:
    #     sys.exit('Input argument peak_height must be positive')

    # maxima and minima candidates are temporarily stored in mx and mn respectively:
    mn, mx = np.Inf, -np.Inf
    mnpos, mxpos = NaN, NaN
    
    # # Obtaining the maximum and minimum peaks of the signal:
    # key_list = list(x)
    # value_list = list(y)
    # signal_dict = dict(zip(key_list, value_list))
    # signal_full_dict = defaultdict(list)
    # for key, value in chain(signal_dict.items()):
    #     signal_full_dict[key].append(value)
    # max_peak = max(signal_full_dict.items(), key = lambda x: x[1])[1]
    # mxpkpos = max(signal_full_dict.items(), key = lambda x: x[1])[0]
    # min_peak = min(signal_full_dict.items(), key = lambda x: x[1])[1]
    # mnpkpos = min(signal_full_dict.items(), key = lambda x: x[1])[0]
    # maxtab.append((mxpkpos, max_peak))
    # mintab.append((mnpkpos, min_peak))
    
    # amplitude_envelope_min, amplitude_envelope_max = hl_envelopes_idx(s=y,dmin=10,dmax=10)
    amplitude_envelope_min, amplitude_envelope_max = hl_envelopes_idx(s=y,dmin=7,dmax=7)
    for k in range(0, len(amplitude_envelope_max)):
        amplitude_envelope_y = y[amplitude_envelope_max]
        amplitude_envelope_x = x[amplitude_envelope_max]
    amplitude_envelope_mx.append((amplitude_envelope_x, amplitude_envelope_y))
    # print(amplitude_envelope_mx)
    
    for j in range(0, len(amplitude_envelope_min)):
        amplitude_envelope_y = y[amplitude_envelope_min]
        amplitude_envelope_x = x[amplitude_envelope_min]
    amplitude_envelope_mn.append((amplitude_envelope_x, amplitude_envelope_y))
    # print(amplitude_envelope_mn)
    
    for x_val, y_val in amplitude_envelope_mx:
        for i in range(0, len(y_val)):
            this = y_val[i]
            if (this > peak_height):
                mx = this
                mxpos = x_val[i]
                maxtab.append((mxpos, mx))

    for x_val, y_val in amplitude_envelope_mn:
        for i in range(0, len(y_val)):
            this = y_val[i]
            if (this < peak_height):
                mn = this
                mnpos = x_val[i]
                mintab.append((mnpos, mn))

    return [maxtab, mintab]

我获取信号坐标的方法:

    maxheight = (max(process_y) * 0.01)
    minheight = (min(process_y) * 0.01)
    
    S_max_peaks = custom_peakdetection_envelope(y_axis=process_y, peak_height=maxheight)
    S_min_peaks = custom_peakdetection_envelope(y_axis=process_y*(-1), peak_height=minheight*(-1))

    y_max_peaks = list(zip(*S_max_peaks[0]))
    y_min_peaks = list(zip(*S_min_peaks[0]))
    x_max_peaks = list(zip(*S_max_peaks[0]))
    x_min_peaks = list(zip(*S_min_peaks[0]))

    process_y_max_peak = np.array(y_max_peaks[1])
    process_y_min_peak = np.array(y_min_peaks[1])
    process_x_max_peak = np.array(x_max_peaks[0])
    process_x_min_peak = np.array(x_min_peaks[0])
    
    S_x_max_peaks, S_y_max_peaks = x_time[process_x_max_peak], process_y[process_x_max_peak]
    S_x_min_peaks, S_y_min_peaks = x_time[process_x_min_peak], process_y[process_x_min_peak]

我发现错误在于这部分代码:

process_y[process_x_max_peak]

我该如何解决这个错误?另外,是否有办法改进功能并帮助我更轻松地找到协调员?

标签: pythonlistnumpymatplotlibcoordinates

解决方案


也许process_x_max_peak必须更改为保存整数。尝试以下操作:

process_y[process_x_max_peak.astype(int)]

推荐阅读