Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
def specgram(self, x, NFFT=None, Fs=None, Fc=None, detrend=None,
window=None, noverlap=None,
cmap=None, xextent=None, pad_to=None, sides=None,
scale_by_freq=None, mode=None, scale=None,
vmin=None, vmax=None, **kwargs):
Plot a spectrogram.
# selfとはaxexオブジェクトの事
# axesをクリア
if not self._hold:
# Fcが未入力の場合は0(軸目盛に使う)
if Fc is None:
Fc = 0
# 複素数を扱うかの保障
if mode == 'complex':
raise ValueError('Cannot plot a complex specgram')
# スケールの設定
if scale is None or scale == 'default':
if mode in ['angle', 'phase']:
scale = 'linear'
scale = 'dB'
elif mode in ['angle', 'phase'] and scale == 'dB':
raise ValueError('Cannot use dB scale with angle or phase mode')
# stft解析
spec, freqs, t = mlab.specgram(x=x, NFFT=NFFT, Fs=Fs,
detrend=detrend, window=window,
noverlap=noverlap, pad_to=pad_to,
# スケールにより、解析結果を処理
if scale == 'linear':
Z = spec
elif scale == 'dB':
if mode is None or mode == 'default' or mode == 'psd':
Z = 10. * np.log10(spec)
Z = 20. * np.log10(spec)
raise ValueError('Unknown scale %s', scale)
# imshow()で表示するために、行列を反転
Z = np.flipud(Z)
# 目盛の最小値、最大値を設定
if xextent is None:
xextent = 0, np.amax(t)
xmin, xmax = xextent
freqs += Fc
extent = xmin, xmax, freqs[0], freqs[-1]
# imshowをプロット
im = self.imshow(Z, cmap, extent=extent, vmin=vmin, vmax=vmax,
# アスペクト比をグラフサイズに合わせる
return spec, freqs, t, im
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment