【问题标题】:Python Pcolormesh not showingPython Pcolormesh没有显示
【发布时间】:2020-11-08 06:01:01
【问题描述】:

我有点迷失在这里发生的事情:

我有这个模拟复杂无线麦克风信号的功能

def mic_sig(N, fs, fc, fm, fd):
    t = np.arange(N) / fs
    x = np.exp(1.j*(2.*np.pi*fc*t + fd/fm*np.sin(2.*np.pi*fm*t)))
    return x

我正在生成两个以 6Mhz 采样的信号,它们都从中心频率偏移了 1Mhz

N = int(np.power(2., np.ceil(np.log2(1e5))))
fs = int(1e6)
fm = int(3.9e3)
fd = int(15e3)
fc = int(-1e5)
sig = mic_sig(N, fs=fs, fc=fc, fm=fm, fd=fd)
fc = int(1e5)
sig += mic_sig(N, fs=fs, fc=fc, fm=fm, fd=fd)

绘制频谱图完全符合我的预期:

f1, Pxx_den = signal.welch(sig, fs, nperseg=1024)
plt.plot(f1, Pxx_den)
plt.xlabel('Frequency [Hz]')
plt.ylabel('PSD')
plt.show()

但是当我用 pcolormesh 做 STFT 时,我只会得到负面的部分

f, t, Zxx = signal.stft(sig, fs, window='hann', nperseg=1024)
plt.pcolormesh(t, f, np.abs(Zxx*np.conj(Zxx)), shading='gouraud')
plt.title('STFT Magnitude')
plt.ylabel('Frequency [Hz]')
plt.xlabel('Time [sec]')
plt.show()

我不明白为什么?

STFT 之后的数据如下所示

plt.plot(np.abs(Zxx*np.conj(Zxx)))
plt.show()

所以理论上它也应该打印正面部分。我错过了什么?

感谢任何帮助 谢谢:)

【问题讨论】:

    标签: python matplotlib scipy


    【解决方案1】:

    问题在于 stft 返回一个非单调的“f”向量。 解决办法:

    f, t, Zxx = signal.stft(sig, fs, window='hann', nperseg=1024)
    ind=np.argsort(f) # returns the indices of the sorted f vector
    f=f[ind]
    Zxx=Zxx[ind,:]
    plt.pcolormesh(t, f, np.abs(Zxx*np.conj(Zxx)), shading='gouraud')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-17
      • 1970-01-01
      • 2020-11-03
      相关资源
      最近更新 更多