识别峰值的任务出现在各种应用中,从在傅立叶中查找峰值变换(FFT)以从 2D 阵列中提取峰值。一个常见的挑战是区分真正的峰值和噪声引起的波动。
与其从头开始实现峰值查找算法,不如考虑利用 scipy .signal.find_peaks 函数。此函数提供了根据特定条件过滤和识别峰值的选项。
要有效利用 find_peaks 的强大功能,了解其参数至关重要:
在所有参数中,突出度在区分真实峰值和噪声方面最为有效。它的定义涉及达到更高峰值所需的最小垂直下降。
为了说明其实用性,请考虑受噪声污染的变频正弦曲线。理想的解决方案是准确识别峰值,而不会屈服于虚假噪声峰值。
以下代码演示了如何使用具有各种参数组合的 find_peaks 函数:
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import find_peaks
# Generate signal
x = np.sin(2*np.pi*(2**np.linspace(2,10,1000))*np.arange(1000)/48000) np.random.normal(0, 1, 1000) * 0.15
# Find peaks using different parameters
peaks, _ = find_peaks(x, distance=20)
peaks2, _ = find_peaks(x, prominence=1)
peaks3, _ = find_peaks(x, width=20)
peaks4, _ = find_peaks(x, threshold=0.4)
# Plot results
plt.subplot(2, 2, 1)
plt.plot(peaks, x[peaks], "xr"); plt.plot(x); plt.legend(['distance'])
plt.subplot(2, 2, 2)
plt.plot(peaks2, x[peaks2], "ob"); plt.plot(x); plt.legend(['prominence'])
plt.subplot(2, 2, 3)
plt.plot(peaks3, x[peaks3], "vg"); plt.plot(x); plt.legend(['width'])
plt.subplot(2, 2, 4)
plt.plot(peaks4, x[peaks4], "xk"); plt.plot(x); plt.legend(['threshold'])
plt.show()
从结果中观察到,使用突出度(第二个子图中的蓝线)有效地隔离了真正的峰值,而距离、宽度和阈值在存在噪声的情况下提供了低于标准的性能。
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3