【发布时间】:2021-03-16 14:37:38
【问题描述】:
我正在寻求澄清我在理解或实施频谱图时出错的地方。
为了确保一切正确,我从一个玩具示例开始,所以我生成了一个非常简单的信号(三个不同频率和相移的周期信号的总和,没有噪音) 作为休闲:
import numpy as np
sampling_rate = 1.0 / 1000
duration = 2
t = np.arange(0, duration, sampling_rate)
# Signal 1
A_1 = 0.8 # amplitude of the cosine wave
f_1 = 100 # frequency of the cosine wave
phase_1 = 30 #desired phase shift of the cosine in degrees
phi_1 = phase_1*np.pi/180
s1 = A_1*np.cos(2*np.pi*f_1*t+phi_1)
# Signal 2
A_2 = 0.3 # amplitude of the cosine wave
f_2 = 8 # frequency of the cosine wave
phase_2 = 45 #desired phase shift of the cosine in degrees
phi_2 = phase_2*np.pi/180
s2 = A_2*np.cos(2*np.pi*f_2*t+phi_2)
# Signal 3
A_3 = 0.1 # amplitude of the cosine wave
f_3 = 60 # frequency of the cosine wave
phase_3 = -10 #desired phase shift of the cosine in degrees
phi_3 = phase_3*np.pi/180
s3 = A_3*np.cos(2*np.pi*f_3*t+phi_3)
# Result
x = s1 + s2 + s3
我预计“x”信号的频谱图将是三个水平线,对应于生成信号的三个频率。信号不会随时间变化,因此我希望得到类似于 FFT 的结果,但表示形式不同。
但是在用下面的代码绘制之后:
from scipy import signal
import matplotlib.pyplot as plt
freqs, times, spectrogram = signal.spectrogram(x)
plt.figure(figsize=(8, 6))
plt.imshow(spectrogram, aspect='auto', cmap='hot_r', origin='lower')
plt.title('Spectrogram')
plt.ylabel('Frequency band')
plt.xlabel('Time window')
plt.tight_layout()
我得到了一些意想不到的东西:
找到的频率没有意义。
所以我的问题是:我在哪里犯了错误?我的预期错了吗?我的实现以某种方式被破坏了?也许有人可以向我推荐有关此主题的良好知识来源?
非常感谢您的宝贵时间和帮助。
【问题讨论】:
标签: python scipy time-series fft