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

如何获取信号中最大峰值对应的半高全宽(FWHM)?

获取最大峰值的半高全宽(FWHM)

其实很简单,peak_widths返回的结果数组和find_peaks得到的peaks数组是一一对应的——也就是说,results_half[0]里的第n个元素,就是peaks里第n个峰值的宽度。

你已经通过np.argmax(y[peaks])找到了最大峰值在peaks数组中的位置下标,直接用这个下标去提取results_half[0]里对应的值就行。

完整修改后的代码示例

from scipy.signal import chirp, find_peaks, peak_widths
import matplotlib.pyplot as plt
import numpy as np  # 别忘了导入numpy!

x = np.linspace(0, 6 * np.pi, 1000)
y = np.sin(x) + 0.6 * np.sin(2.6 * x)
peaks, _ = find_peaks(y)

# 找到最大峰值在peaks数组中的下标
idx_in_peaks = np.argmax(y[peaks])
# 获取最大峰值的索引和幅值
max_peak_idx = peaks[idx_in_peaks]
y_max = y[max_peak_idx]

# 计算所有峰值的半高宽
results_half = peak_widths(y, peaks, rel_height=0.5)
# 提取最大峰值对应的宽度
max_peak_width = results_half[0][idx_in_peaks]

print(f"最大峰值的半高全宽(FWHM):{max_peak_width}")

# 可视化验证
plt.plot(y)
plt.plot(max_peak_idx, y_max, "x", color="C3", markersize=10, label="最大峰值")
plt.hlines(*results_half[1:], color="C2")
# 单独标注最大峰值的宽度线
plt.hlines(results_half[1][idx_in_peaks], results_half[2][idx_in_peaks], results_half[3][idx_in_peaks], color="C3", linestyle="--", label="最大峰值FWHM")
plt.legend()
plt.show()

关键说明

  • idx_in_peaks是最大峰值在peaks数组中的位置,而不是在原始信号y中的索引,这一点很重要——因为results_half[0]的顺序完全匹配peaks的顺序。
  • 如果需要其他相对高度的宽度(比如1/4高度),只需要修改peak_widths的rel_height参数即可,提取方式是一样的。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 23:32:41