python - 如何在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]
我该如何解决这个错误?另外,是否有办法改进功能并帮助我更轻松地找到协调员?
解决方案
也许process_x_max_peak
必须更改为保存整数。尝试以下操作:
process_y[process_x_max_peak.astype(int)]
推荐阅读
- javascript - 上下文 API 中的 ReactJS 异步 axios
- linux - NiFi PutDatabaseRecord 到 Postgres
- php - 尝试使用 Worpdress 和 WPML 根据语言在主页上发布不同的帖子
- javascript - Discord.js:如何在新的 12.0.0 更新中为机器人添加角色?
- java - 在 Spring Webflux 中组合多个单声道
- json - 如何将 JSON 文件导入 Cloud Firestore
- powershell - Powershell 脚本正在运行,但未采取任何措施
- html - 如何声明一个对象并在 pug js 中对其进行迭代?
- python - 有没有更好/更快的方法来计算数组中每个元素的相对排名?
- swift - 如何在 SwiftUI 中禁用列表中的某些单元格