Skip to main content
 首页 » 编程设计

python之如何使用 scipy.signal.spectrogram 找到正确的幅度

2024年11月01日8zhenyulu

我尝试使用 scipy.signal.spectogram 创建幅度谱图。 不幸的是我没有让它工作。

我的测试信号应该是一个频率为 400 赫兹、幅度为 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 结果的两个“一半”加在一起以获得总能量(Parseval 定理)。或者只是将一侧加倍,因为复共轭具有相等的大小。