[英]Matplotlib spectrogram intensity legend (colorbar)
I'm using matplotlib's specgram
function to generate a spectrogram. 我正在使用matplotlib的
specgram
函数来生成频谱图。 I've attempted to include a colorbar off to the right of the spectrogram to give an indication of dB-to-color-mapping. 我试图在光谱图的右侧包含一个颜色条,以指示dB到颜色的映射。
For some reason though, the dB indicated by the colorbar do not make sense. 但是出于某种原因,颜色条指示的dB没有意义。
Perhaps I've not generated the colorbar correctly? 也许我没有正确生成颜色条? Perhaps there is some parameter that I need to pass to specgram?
也许有一些参数需要传递给specgram?
The signal I'm generating is a 1Khz, 2Vpp sine sampled at 32Khz. 我正在生成的信号是一个1Khz,2Vpp的正弦波,采样频率为32Khz。
I'm expecting that the dark red peak on the spectrogram corresponds to 0dB (Meaning that +1V is my reference) 我期待频谱图上的暗红色峰值对应0dB(意味着+ 1V是我的参考)
Anybody have any idea what is wrong with my approach? 有人知道我的方法有什么问题吗?
def plot_specgram(data, title='', x_label='', y_label='', fig_size=None):
fig = plt.figure()
if fig_size != None:
fig.set_size_inches(fig_size[0], fig_size[1])
ax = fig.add_subplot(111)
ax.set_title(title)
ax.set_xlabel(x_label)
ax.set_ylabel(y_label)
pxx, freq, t, cax = plt.specgram(data, Fs=32000)
fig.colorbar(cax).set_label('Intensity [dB]')
plot_specgram(a,title='Spectrogram', x_label='time (in seconds)', y_label='frequency', fig_size=(14,8))
This is what I get as resulting spectrogram: 这就是我得到的光谱图:
First of all, please supply your a
vector, since it seems to have some kind of harmonics. 首先,请提供
a
矢量,因为它似乎有某种谐波。
This is a bit trial and error, but this seems to produce the correct scaling: 这有点试验和错误,但这似乎产生了正确的缩放:
NFFT = 256
ax.specgram(x/(NFFT/2), NFFT=NFFT, Fs=fs, mode='magnitude', window=plt.window_none)
Using a window seems to lose about 1/2 of the peak power, you can of course adjust for this. 使用窗口似乎失去了大约1/2的峰值功率,你当然可以调整它。
A complete example where I have limited the dynamic range to 40dB (as an example, if you want to hide the small stuff). 一个完整的例子,我将动态范围限制在40dB(例如,如果你想隐藏小东西)。
import numpy as np
import pylab as plt
# generate a 1kHz sine wave
fs = 32e3
t = np.arange(0, 15, 1.0/fs)
f0 = 1e3
A = 1
x = A*np.sin(2*np.pi*f0*t)
fig, ax = plt.subplots()
cmap = plt.get_cmap('viridis')
vmin = 20*np.log10(np.max(x)) - 40 # hide anything below -40 dBc
cmap.set_under(color='k', alpha=None)
NFFT = 256
pxx, freq, t, cax = ax.specgram(x/(NFFT/2), Fs=fs, mode='magnitude',
NFFT=NFFT, noverlap=NFFT/2,
vmin=vmin, cmap=cmap,
window=plt.window_none)
fig.colorbar(cax)
print np.max(pxx) # should match A
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.