[英]Finding maxima, above a certain threshold, with Numpy in a 1D numpy array
[英]Finding peaks above threshold
我正在使用from scipy.signal import find_peaks
。 是否可以找到所有大于指定阈值的峰值。 我不确定你是否能做到这一点。 例如: indices = find_peaks(s_volts, threshold= 0.5*maxPeak)
我试图找到大于最大峰值 50% 的所有峰值。
不要认为有一种内置的方法可以做到这一点。 以下是没有 scipy 的方法:
from scipy.signal import find_peaks
import numpy as np
x = np.array([1,2,3,2,1,2,3,2,1,2,3,4,3,2,1,2,3,4,7,4,3,2,1])
peaks_indices = find_peaks(x)[0]
peaks = np.array(list(zip(peaks_indices, x[peaks_indices])))
threshold = 0.5 * max(x[peaks_indices])
filtered_peaks = [(index, value) for index, value in peaks if value > threshold]
# If you just want the indices:
filtered_peaks_indices = [index for index, value in peaks if value > threshold]
# Or just want the values
filtered_peaks_values = [value for index, value in peaks if value > threshold]
# Visualize
from matplotlib import pyplot as plt
plt.plot(range(len(x)), x)
for index in peaks_indices:
plt.axvline(index)
plt.axhline(threshold)
plt.scatter(filtered_peaks_indices, filtered_peaks_values, s=200)
plt.show()
如果您可以事先定义阈值,则可以使用height
参数。 借用@omer-tuchfeld 示例:
from scipy.signal import find_peaks
import numpy as np
from matplotlib import pyplot as plt
x = np.array([1,2,3,2,1,2,3,2,1,2,3,4,3,2,1,2,3,4,7,4,3,2,1])
threshold = 0.5 * max(x)
peaks_indices = find_peaks(x, height=threshold)[0]
peaks_values = x[peaks_indices]
fig = plt.figure()
plt.plot(range(len(x)), x)
for index in peaks_indices:
plt.axvline(index)
plt.axhline(threshold)
plt.scatter(peaks_indices, peaks_values, s=200)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.