如何使用Python SciPy检测峰值,获取索引错误“用作索引的数组必须是整数(或布尔)类型”

ahy6op9u  于 2022-12-13  发布在  Python
关注(0)|答案(1)|浏览(108)

我有速度数据,因为我需要检测阈值大于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

如何获得乘坐此错误的任何建议,感谢问候

58wvjzkj

58wvjzkj1#

未找到峰值。即len(peak_locs)为零。
因此,最后得到的是这个数组,其类型默认为float:

>>> np.array(peak_locs)
array([], dtype=float64)

去修?找更多的山峰!

相关问题