python - 从 scipy 频谱图中读取振幅
问题描述
在我看来,在 scipy.signal.spectrogram 中计算的幅度不正确(在我的情况下大约 20% 的折扣)。是否有可能对此进行改进?考虑这个例子
import matplotlib.pyplot as plt
import numpy as np
from numpy import pi as π
f_s = 200 # Sampling rate = number of measurements per second in [Hz]
t = np.arange(-10,10, 1 / f_s)
N = len(t)
T1 = np.tanh(t)/2 + 1.0 # Period in [s]
T2 = 0.125 # Period in [s]
f1 = 1 / T1 # Frequency in [Hz]
f2 = 1 / T2 # Frequency in [Hz]
# Signal
x = 13*np.sin(2 * π * f1 * t) + 42*np.sin(2 * π * f2 * t)
# Spectrogram with good time window
Δt = 4 # window length in [s]
Nw = np.int(2**np.round(np.log2(Δt * f_s)))
print(f"Effective window length is {Nw/f_s:.1f}s")
f, t_, Sxx = signal.spectrogram(x, f_s, window='hanning', nperseg=Nw, noverlap = Nw - 100, detrend=False, scaling='spectrum')
Δf = f[1] - f[0]
Δt_ = t_[1] - t_[0]
fig, ax = plt.subplots(figsize = (15,5))
im = ax.pcolormesh(t_ + t[0] - Δt_, f - Δf/2, np.sqrt(Sxx), cmap = "inferno_r")
plt.colorbar(im)
ax.grid(True)
ax.set_ylabel("Frequency in [Hz]")
ax.set_xlabel("t")
ax.set_ylim(0,10)
plt.show()
显然,这些线没有显示之前定义的幅度(即 13 和 42)。
解决方案
推荐阅读
- python - 我的最后两个 elif/else 语句不会执行,我不知道为什么?
- java - 处理声音比原来慢
- reactjs - 如何在反应打字稿中选择/取消选择复选框?
- angular - 根据 FormGroup 中的另一个字段值切换选择菜单选项
- c - 如何在更新 GTK UI 的 GTask 结束时避免竞争条件?
- caching - Redis HSET 键在几分钟后过期
- javascript - 一旦 promise 数组中的每个 promise 被履行,就返回数据 - Typescript/Javascript
- jquery - 没有值的单选按钮标签的活动状态
- r - 如何将 R 数据集中的某些类别更改为 NA
- python - 如何通过 OpenCv 更有效地使用 SQL 查询,而不必每帧都插入数据