如何使用 scipy.signal.spectogram 找到正确的震级
How to find the correct magnitude with scipy.signal.spectogram
我尝试使用 scipy.signal.spectogram
创建幅度谱图。
不幸的是我没有让它工作。
我的测试信号应该是一个频率为 400 Hz、振幅为 1 的正弦波。频谱图的幅度结果似乎是 0.5 而不是 1.0。我不知道可能是什么问题。
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
# 2s time range with 44kHz
t = np.arange(0, 2, 1/44000)
# test signal: sine with 400Hz amplitude 1
x = np.sin(t*2*np.pi*440)
# spectogram for spectrum of magnitudes
f, t, Sxx = signal.spectrogram(x,
44000,
"hanning",
nperseg=1000,
noverlap=0,
scaling="spectrum",
return_onesided=True,
mode="magnitude"
)
# plot last frequency plot
plt.plot(f, Sxx[:,-1])
print("highest magnitude is: %f" %np.max(Sxx))
严格实时域的信号在频域是共轭对称的。例如将出现在复杂结果 FFT 的正半部分和负半部分(或上半部分)。
因此您需要将 FFT 结果的两个 "halves" 加在一起以获得总能量(Parseval 定理)。或者只是将一侧加倍,因为复共轭具有相等的大小。
我尝试使用 scipy.signal.spectogram
创建幅度谱图。
不幸的是我没有让它工作。
我的测试信号应该是一个频率为 400 Hz、振幅为 1 的正弦波。频谱图的幅度结果似乎是 0.5 而不是 1.0。我不知道可能是什么问题。
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
# 2s time range with 44kHz
t = np.arange(0, 2, 1/44000)
# test signal: sine with 400Hz amplitude 1
x = np.sin(t*2*np.pi*440)
# spectogram for spectrum of magnitudes
f, t, Sxx = signal.spectrogram(x,
44000,
"hanning",
nperseg=1000,
noverlap=0,
scaling="spectrum",
return_onesided=True,
mode="magnitude"
)
# plot last frequency plot
plt.plot(f, Sxx[:,-1])
print("highest magnitude is: %f" %np.max(Sxx))
严格实时域的信号在频域是共轭对称的。例如将出现在复杂结果 FFT 的正半部分和负半部分(或上半部分)。
因此您需要将 FFT 结果的两个 "halves" 加在一起以获得总能量(Parseval 定理)。或者只是将一侧加倍,因为复共轭具有相等的大小。