scipy.signal.spectrogram 与 matplotlib.pyplot.specgram 相比 [英] scipy.signal.spectrogram compared to matplotlib.pyplot.specgram

查看:69
本文介绍了scipy.signal.spectrogram 与 matplotlib.pyplot.specgram 相比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码使用 scipy.signal.spectrogrammatplotlib.pyplot.specgram 生成频谱图.

The following code generates a spectrogram using either scipy.signal.spectrogram or matplotlib.pyplot.specgram.

然而,specgram 函数的颜色对比度相当低.有什么办法可以增加吗?

The color contrast of the specgram function is, however, rather low. Is there a way to increase it?

import numpy as np
from scipy import signal
import matplotlib.pyplot as plt

# Generate data
fs = 10e3
N = 5e4
amp = 4 * np.sqrt(2)
noise_power = 0.01 * fs / 2
time = np.arange(N) / float(fs)
mod = 800*np.cos(2*np.pi*0.2*time)
carrier = amp * np.sin(2*np.pi*time + mod)
noise = np.random.normal(scale=np.sqrt(noise_power), size=time.shape)
noise *= np.exp(-time/5)
x = carrier + noise

使用 matplotlib.pyplot.specgram 会得到以下结果:

Pxx, freqs, bins, im = plt.specgram(x, NFFT=1028, Fs=fs)
x1, x2, y1, y2 = plt.axis()
plt.axis((x1, x2, 0, 200))
plt.show()

使用 scipy.signal.spectrogram 给出以下图

f, t, Sxx = signal.spectrogram(x, fs, nfft=1028)
plt.pcolormesh(t, f[0:20], Sxx[0:20])
plt.ylabel('Frequency [Hz]')
plt.xlabel('Time [sec]')
plt.show()

这两个函数似乎都使用了jet"颜色图.

Both functions seem to use the 'jet' colormap.

我通常也会对这两个功能之间的差异感兴趣.虽然它们做了类似的事情,但它们显然并不完全相同.

I would also be generally interested in the difference between the two functions. Although they do something similar, they are obviously not identical.

推荐答案

plt.specgram 不仅返回 Pxx f t ,但也会自动为您进行绘图.绘制时, plt.specgram 绘制 10 * np.log10(Pxx)而不是 Pxx .

plt.specgram not only returns Pxx, f, t, but also does the plotting for you automatically. When plotting, plt.specgram plots 10*np.log10(Pxx) instead of Pxx.

但是, signal.spectrogram 仅返回 Pxx f t .它根本不做绘图.这就是您使用 plt.pcolormesh(t, f[0:20], Sxx[0:20]) 的原因.您可能想要绘制 10*np.log10(Sxx).

However, signal.spectrogram only returns Pxx, f, t. It does not do plotting at all. That is why you used plt.pcolormesh(t, f[0:20], Sxx[0:20]). You may want to plot 10*np.log10(Sxx).

这篇关于scipy.signal.spectrogram 与 matplotlib.pyplot.specgram 相比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆