def spectrogram(self,
ax=None,
freq_range=None,
dB_thresh=35,
derivative=True,
colormap='gray',
compensated=True):
"""Plots a spectrogram, requires matplotlib
ax - axis on which to plot
freq_range - a tuple of frequencies, eg (300, 8000)
dB_thresh - noise floor threshold value, increase to suppress noise,
decrease to improve detail
derivative - if True, plots the spectral derivative, SAP style
colormap - colormap to use, good values: 'inferno', 'gray'
compensated - if True, centers the displayed window around the center
of the short FFT. If False, the window always starts
at the begining of data window. Both methods are equivalent
when n_overlap = 0 and the data window length is the full NFFT.
Returns an axis object
"""
if compensated:
data_overlap = self._noverlap + self._data_in_window - self._NFFT
if data_overlap < 0:
print('warning: spectrogram does not fully cover the data')
data_overlap = 0
comp = (data_overlap / 2) / self._rate
else:
comp = 0
from matplotlib import colors
if ax is None:
import matplotlib.pyplot as plt
ax = plt.gca()
if derivative:
pxx, f, t = self.max_spec_derivative(freq_range=freq_range)
thresh = value_from_dB(dB_thresh, np.max(pxx))
ax.pcolorfast(t + comp,
f,
pxx,
cmap=colormap,
norm=colors.SymLogNorm(linthresh=thresh))
else:
pxx, f, t = self.power(freq_range)
thresh = value_from_dB(dB_thresh, np.max(pxx))
ax.pcolorfast(t + comp,
f,
pxx,
cmap=colormap,
norm=colors.LogNorm(vmin=thresh))
return ax
评论列表
文章目录