You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何借助scipy find_peaks()计算峰值与旁瓣的垂直距离?

计算峰值与旁瓣的垂直距离解决方案

scipy.signal.find_peaks()本身确实没有直接计算峰值与旁瓣垂直距离的功能,但可以通过结合其他信号处理工具手动实现,核心思路是先定位峰值,再找到对应旁瓣的最低点(谷值),最后计算两者的高度差。

具体步骤:

  1. 定位峰值
    先用find_peaks()找出信号中的峰值位置和高度,示例代码:

    import numpy as np
    from scipy.signal import find_peaks, argrelextrema
    
    # 生成示例信号(替换成你的实际信号)
    x = np.linspace(0, 10, 1000)
    y = np.sin(x) + 0.5*np.sin(3*x) + 0.2*np.sin(5*x)
    
    # 找到峰值,可根据需求调整height、distance等参数
    peaks, props = find_peaks(y, height=0)
    peak_heights = props['peak_heights']
    peak_indices = peaks
    
  2. 定位旁瓣谷值
    用argrelextrema()找出信号中所有的局部最小值(谷值),然后为每个峰值匹配其左右最近的旁瓣谷值:

    # 找出所有局部谷值
    valley_indices = argrelextrema(y, np.less)[0]
    valley_heights = y[valley_indices]
    
    # 计算每个峰值与旁瓣的垂直距离
    peak_sidelobe_distances = []
    for idx, peak_idx in enumerate(peak_indices):
        # 找左侧最近的谷值
        left_valleys = valley_indices[valley_indices < peak_idx]
        left_nearest = left_valleys[-1] if len(left_valleys) > 0 else None
        # 找右侧最近的谷值
        right_valleys = valley_indices[valley_indices > peak_idx]
        right_nearest = right_valleys[0] if len(right_valleys) > 0 else None
    
        # 计算距离(可根据需求选择保留单侧或双侧距离)
        current_peak_height = peak_heights[idx]
        distances = []
        if left_nearest is not None:
            distances.append(current_peak_height - y[left_nearest])
        if right_nearest is not None:
            distances.append(current_peak_height - y[right_nearest])
        peak_sidelobe_distances.append(distances)
    
  3. 针对频谱场景的优化(可选)
    如果你的信号是FFT后的频谱,旁瓣通常位于主瓣外侧,可先通过主瓣宽度过滤掉主瓣内的小谷值,只计算主瓣外旁瓣的距离:

    # 假设主瓣宽度为N个点(根据你的频谱参数调整)
    main_lobe_width = 20
    peak_sidelobe_distances = []
    for idx, peak_idx in enumerate(peak_indices):
        # 只考虑主瓣外的谷值
        left_valleys = valley_indices[(valley_indices < peak_idx - main_lobe_width)]
        right_valleys = valley_indices[(valley_indices > peak_idx + main_lobe_width)]
        # 后续计算逻辑同步骤2
        current_peak_height = peak_heights[idx]
        distances = []
        if len(left_valleys) > 0:
            distances.append(current_peak_height - y[left_valleys[-1]])
        if len(right_valleys) > 0:
            distances.append(current_peak_height - y[right_valleys[0]])
        peak_sidelobe_distances.append(distances)
    

说明

  • 垂直距离的定义是峰值高度减去旁瓣谷值的高度,若需要相对值(比如dB),可对高度做对数转换后再计算。
  • 可根据实际需求调整find_peaks()和argrelextrema()的参数,比如设置order来控制局部极值的判定范围,避免误识别噪声带来的小谷值。

内容的提问来源于stack exchange,提问作者Besz15

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 14:06:24