【问题标题】:Spectogram with unexpected results (Python, scipy.signal)具有意外结果的频谱图(Python,scipy.signal)
【发布时间】: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


    【解决方案1】:

    我在问这个问题之前找到了答案,所以我不会删除整个帖子,而是与你分享我的结果。我希望有人能从中受益。

    情节不错,但轴的签名很糟糕。如果你睁大眼睛,你会注意到实际上有 3 个小节(最弱的在 15 个左右)。线的可见性与分量信号的幅度值成正比,这是有道理的,因为幅度的颜色代码。所以现在很容易注意到 y 轴由于某种原因除以 4。类似的问题是 x 轴,当您意识到我们没有在任何地方提供有关采样频率的信息时,这更容易理解。

    所以现在我们可以纠正这个问题,并将调色板更改为休闲:

    freqs, times, spectrogram = signal.spectrogram(x,fs=1./sampling_rate)
    
    plt.figure(figsize=(8, 6))
    
    plt.pcolormesh(times, freqs, spectrogram, shading='gouraud')
    plt.ylabel('Frequency [Hz]')
    plt.ylim([0,110])
    plt.xlabel('Time [sec]')
    
    plt.tight_layout()
    plt.show()
    

    这给我们带来了预期的结果(s2 的振幅乘以 2):

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-28
      • 1970-01-01
      • 2020-03-15
      • 1970-01-01
      相关资源
      最近更新 更多