ichou1のブログ

主に音声認識、時々、データ分析のことを書く

音声認識メモ(スペクトログラム)

spectrogramに関するメモ。

Wikipediaより

スペクトログラム(英: Spectrogram)とは、複合信号を窓関数に通して、周波数スペクトルを計算した結果を指す。
3次元のグラフ(時間、周波数、信号成分の強さ)で表される。

pythonのmatplotlibライブラリにある「specgram」関数を使ってみる。

プロットする対象は、フーリエ変換のメモで使った波形。

波形データ

f:id:ichou1:20190101110011p:plain

FFT変換後
numpy.fft.fft("波形データ")

f:id:ichou1:20190101112534p:plain

specgram関数に渡すパラメータ
  • Fs: サンプリング周波数、「16k」Hz
  • NFFT : FFTのブロックサイズ、「512」
  • window : 窓関数、使用しない(mlab.window_none)
spectrum, freqs, t, im = plt.specgram("波形データ", NFFT=512, Fs=16000, window=mlab.window_none)
plt.colorbar(im)

f:id:ichou1:20190101120659p:plain

250Hzと500Hzの周波数に該当する部分で、色の差異が出ている。
この色に該当する部分は"信号成分の強さ"を表す。


specgram関数の1番目の返り値を確認してみる。

マニュアル説明

Returns:
spectrum : 2-D array
Columns are the periodograms of successive segments.

# spectrum.shape
(257, 1)

# spectrum[0:20]
array([[3.75250747e-31],
       [4.10801350e-32],
       [5.97746403e-31],
       [1.51855718e-30],
       [2.75157550e-31],
       [6.41012852e-31],
       [2.86066184e-30],
       [2.14148310e-30],
       [4.00000000e+01],  # DFT index 8 corresponds to 250Hz
       [8.24355999e-30],
       [6.58668214e-31],
       [7.53947441e-31],
       [1.47302632e-30],
       [9.28820685e-30],
       [8.76544796e-30],
       [4.72905074e-29],
       [1.60000000e+02],  # DFT index 16 corresponds to 500Hz
       [8.07289044e-29],
       [1.78670584e-29],
       [1.20023304e-29]])

ピリオドグラム法を実装した「psd」関数を使って確認してみる。

マニュアル説明
The power spectral density by Welch's average periodogram method.

関数を実行。

pxx, freqs = plt.psd("波形データ", NFFT=512, Fs=16000, window=mlab.window_none)

f:id:ichou1:20190101125306p:plain
x軸とy軸のラベルは自動的に付与された。

# pxx.shape
(257,)

# pxx[0:20]
array([3.75250747e-31, 4.10801350e-32, 5.97746403e-31, 1.51855718e-30,
       2.75157550e-31, 6.41012852e-31, 2.86066184e-30, 2.14148310e-30,
       4.00000000e+01, 8.24355999e-30, 6.58668214e-31, 7.53947441e-31,
       1.47302632e-30, 9.28820685e-30, 8.76544796e-30, 4.72905074e-29,
       1.60000000e+02, 8.07289044e-29, 1.78670584e-29, 1.20023304e-29])
マニュアル説明

Returns:
Pxx : 1-D array
The values for the power spectrum P_{xx} before scaling (real valued)

この値は、「specgram」関数の1番目の返り値と一致している。

この値pxxに対して、以下の計算を実行したものが"Power Spectral(Spectrum) Density"として、「specgram」関数および「psd」関数のグラフにプロットされる。

マニュアル説明

Notes
For plotting, the power is plotted as



10 \times \log_{10} (P_{xx})

for decibels, though Pxx itself is returned.


検算。

# DFT index 1 corresponds to 31.25Hz
# pxx[1] : 4.10801350e-32
10 * math.log10(4.10801350e-32) 
  -313.86368137867873

# DFT index 8 corresponds to 250Hz
# pxx[8] : 40
10 * math.log10(40) 
  16.02059991327962

# DFT index 16 corresponds to 500Hz
# pxx[16] : 160
10 * math.log10(160) 
  22.041199826559247