Scipy find_peaks函数无法精准识别峰底,求解决方案

解决Scipy find_peaks无法精准识别峰底的问题
一、调整find_peaks参数实现峰底识别
find_peaks本身是为识别峰值设计的,但可以通过反转数据的方式把峰底转换成“峰值”来处理,再结合参数过滤噪声,提升识别精度:
- 核心思路:将原始数据取负数,此时原数据的峰底变为新数据的峰值,再用
find_peaks识别 - 关键参数调整:
prominence:控制峰底的“深度”,值越大,只会识别到落差明显的峰底width:限制峰底的宽度,避免把窄噪声点误判为峰底height:设置反转后峰值的最小高度,对应原数据峰底的最低值
示例代码:
import numpy as np from scipy.signal import find_peaks # 模拟原始数据 y = np.array([2, 5, 2, 6, 1, 4, 1, 3]) # 反转数据,将峰底转为峰值 inverted_y = -y # 识别反转后的峰值(对应原数据峰底) bottom_indices, props = find_peaks(inverted_y, prominence=1.5, width=1) print("峰底位置索引:", bottom_indices) print("峰底对应数值:", y[bottom_indices])
二、更适合识别峰底的替代函数
1. scipy.signal.argrelmin
专门用于寻找**局部最小值(峰底)**的函数,通过order参数控制邻域范围,确保当前点是指定窗口内的最小值:
from scipy.signal import argrelmin # order=2:当前点需比前后2个点都小才被判定为峰底 bottom_indices = argrelmin(y, order=2)[0] print("峰底位置索引:", bottom_indices)
2. 自定义峰底识别逻辑
如果需要更灵活的规则(比如非对称窗口、自定义落差阈值),可以自己实现遍历逻辑:
def find_custom_bottoms(y, window_size=1, min_drop=0.5): bottoms = [] for i in range(window_size, len(y)-window_size): # 检查当前点是否为窗口内最小值 is_min = all(y[i] < y[i-j] for j in range(1, window_size+1)) and all(y[i] < y[i+j] for j in range(1, window_size+1)) # 检查与左右点的落差是否达标 if is_min and (y[i-1] - y[i] >= min_drop) and (y[i+1] - y[i] >= min_drop): bottoms.append(i) return np.array(bottoms) # 检查前后1个点,且落差不小于0.5 custom_bottoms = find_custom_bottoms(y, window_size=1, min_drop=0.5) print("自定义峰底位置索引:", custom_bottoms)
内容的提问来源于stack exchange,提问作者Ruijia Li
相关产品推荐
相关产品推荐

