基于指定突出度检测路径下坡路段的峰谷交替异常问题
解决find_peaks指定突出度后峰谷不交替的问题
你遇到的核心问题是:设置prominence=10过滤微小峰谷时,仅对峰值做了突出度筛选,对应的谷值可能因不满足条件被过滤,导致出现连续峰值,进而破坏下坡路段(峰→谷区间)的判断逻辑。以下是几种实用的解决方法:
1. 同步对谷值应用相同的突出度筛选
谷值可以通过对高程数据取反后调用find_peaks检测,必须给谷值检测也加上相同的prominence参数,确保峰谷的筛选标准一致:
from scipy.signal import find_peaks import numpy as np # 假设你的高程数据是elevations数组 elevations = np.array([...]) # 筛选突出度≥10的峰值 peaks, _ = find_peaks(elevations, prominence=10) # 筛选突出度≥10的谷值(取反后找峰值) valleys, _ = find_peaks(-elevations, prominence=10)
这样能确保只有达到突出度要求的谷值才会被保留,从源头减少连续峰值的出现。
2. 对峰谷序列做后处理,强制交替
如果同步筛选后仍存在连续峰/谷,需要对合并后的峰谷序列进行遍历,保留严格交替的有效点:
# 合并峰谷索引并按从小到大排序 all_points = sorted(list(peaks) + list(valleys)) # 标记每个点的类型(峰/谷) peak_set = set(peaks) point_types = ['peak' if idx in peak_set else 'valley' for idx in all_points] # 筛选交替的峰谷 filtered_points = [] filtered_types = [] prev_type = None for idx, p_type in zip(all_points, point_types): if p_type != prev_type: filtered_points.append(idx) filtered_types.append(p_type) prev_type = p_type
处理后filtered_points就是严格交替的峰谷序列,基于它判断下坡路段(从peak到下一个valley的区间)就不会出现异常。
3. 补充宽度参数过滤密集峰值
如果连续峰值是因为相邻峰值间距过近,可以给find_peaks添加width参数,要求峰值的宽度(即峰值平台的长度)达到指定阈值,过滤掉过于密集的小峰值:
peaks, _ = find_peaks(elevations, prominence=10, width=5) # width根据你的数据密度调整
这能进一步减少无效峰值,让峰谷更易形成交替结构。
修正下坡路段绘制
基于过滤后的交替峰谷序列,遍历每个峰值,找到对应的下一个谷值,将两者之间的路段标记为红色即可:
import matplotlib.pyplot as plt plt.plot(elevations, 'b-') # 遍历交替的峰谷序列,标记下坡路段 for i in range(len(filtered_points)-1): if filtered_types[i] == 'peak' and filtered_types[i+1] == 'valley': start_idx = filtered_points[i] end_idx = filtered_points[i+1] plt.plot(range(start_idx, end_idx+1), elevations[start_idx:end_idx+1], 'r-') plt.show()
内容的提问来源于stack exchange,提问作者debsim
相关产品推荐
相关产品推荐

