gpt4 book ai didi

python - 从 Scipy Signal 对象获取峰的宽度和面积

转载 作者:太空宇宙 更新时间:2023-11-03 14:48:15 25 4
gpt4 key购买 nike

如何使用 cwt get peaks 方法从 Scipy Signal 函数中获取具有位置、峰面积、峰宽等属性的峰对象:

def CWT(trace):
x = []
y = []
for i in range(len(trace)):
x.append(trace[i].Position)
y.append(trace[i].Intensity)
x = np.asarray(x)
y = np.asarray(y)
return signal.find_peaks_cwt(x,y)

这只是返回一个数组?

最佳答案

首先,您似乎错误地使用了 find_peaks_cwt。它的两个位置参数不是数据点的 x 和 y 坐标。第一个参数是 y 值。根本不采用 x 值,它们假定为 0,1,2,...。第二个参数是您感兴趣的峰宽列表;

1-D array of widths to use for calculating the CWT matrix. In general, this range should cover the expected width of peaks of interest.

width 参数没有理由与数据数组大小相同。在下面的示例中,数据有 500 个值,但我使用的宽度是 30...99。

其次,此方法仅查找峰的位置(您获得的数组具有峰的索引)。没有分析它们的宽度和面积。您将不得不寻找其他地方(博客文章 Peak Detection in the Python World 列出了一些替代方案,尽管它们都不会返回您想要的数据),或者想出您自己的方法来估算这些东西。

我的尝试如下。它执行以下操作:

  1. 通过峰之间的中点切割信号
  2. 对于每个部分,使用其中值的中值作为基线
  3. 声明峰值由大于 0.5*(峰值 + 基线)的所有值组成,即中值和最大值之间的中间值。
  4. 找到峰的起点和终点。 (宽度就是它们的区别)
  5. 将峰面积声明为第 4 步中找到的区间内的 (y - 基线) 的总和。

完整示例:

t = np.linspace(0, 4.2, 500)
y = np.sin(t**2) + np.random.normal(0, 0.03, size=t.shape) # simulated noisy signal
peaks = find_peaks_cwt(y, np.arange(30, 100, 10))

cuts = (peaks[1:] + peaks[:-1])//2 # where to cut the signal
cuts = np.insert(cuts, [0, cuts.size], [0, t.size])
peak_begins = np.zeros_like(peaks)
peak_ends = np.zeros_like(peaks)
areas = np.zeros(peaks.shape)
for i in range(peaks.size):
peak_value = y[peaks[i]]
y_cut = y[cuts[i]:cuts[i+1]] # piece of signal with 1 peak
baseline = np.median(y_cut)
large = np.where(y_cut > 0.5*(peak_value + baseline))[0]
peak_begins[i] = large.min() + cuts[i]
peak_ends[i] = large.max() + cuts[i]
areas[i] = np.sum(y[peak_begins[i]:peak_ends[i]] - baseline)

数组areaspeak_beginspeak_ends 在这里很有趣。宽度为 [84 47 36],表示峰变薄(回想一下,这些以索引单位表示,宽度是峰中数据点的数量)。我使用此数据将峰着色为红色:

widths = peak_ends - peak_begins
print(widths, areas)
plt.plot(t, y)
for i in range(peaks.size):
plt.plot(t[peak_begins[i]:peak_ends[i]], y[peak_begins[i]:peak_ends[i]], 'r')
plt.show()

picture

关于python - 从 Scipy Signal 对象获取峰的宽度和面积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47847548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com