【发布时间】:2022-12-13 09:28:51
【问题描述】:
我有速度数据,因为我需要检测阈值大于 20 且谷值大于 0 的值。我使用此代码进行峰值检测,但出现索引错误
import numpy as np
from scipy.signal import find_peaks, find_peaks_cwt
import matplotlib.pyplot as plt
import pandas as pd
import sys
np.set_printoptions(threshold=sys.maxsize)
zero_locs = np.where(x==0)
search_lims = np.append(zero_locs, len(x)) # limits for search area
diff_x = np.diff(x)
diff_x_mapped = diff_x > 0
peak_locs = []
x = np.array([1, 9, 18, 24, 26, 5, 26, 25, 26, 16, 20, 16, 23, 5, 1, 27,
22, 26, 27, 26, 25, 24, 25, 26, 3, 25, 26, 24, 23, 12, 22, 11, 15, 24, 11,
26, 26, 26, 24, 25, 24, 24, 22, 22, 22, 23, 24])
for i in range(len(search_lims)-1):
peak_loc = search_lims[i] + np.where(diff_x_mapped[search_lims[i]:search_lims[i+1]]==0)[0][0]
if x[peak_loc] > 20:
peak_locs.append(peak_loc)
fig= plt.figure(figsize=(10,4))
plt.plot(x)
plt.plot(np.array(peak_locs), x[np.array(peak_locs)], "x", color = 'r')
我尝试使用峰值检测算法,它不检测峰值高于 20 的峰值我需要检测 x 值为 0 且峰值为 20 的峰值 expected output: the marked peaks has to be detected 通过运行上面的脚本我得到这个错误
IndexError: arrays used as indices must be of integer (or boolean) type
如何摆脱这个错误任何建议感谢问候
【问题讨论】:
-
显示完整的回溯。您/我们需要知道错误发生在哪里。你不能在不知道的情况下修复它!也就是说,如果问题出在
x[np.array(peak_locs)],那么您需要检查np.array(peak_locs)。那是一个有效的索引数组吗?
标签: python pandas scipy peak-detection