spectrum のインストールと動作確認(スペクトログラム,PSD による Yule Walker 法)(Python を使用)(Windows 上)
スペクトログラムは,周波数と時間を,縦横の軸とするグラフ.強度を色や明るさで表すことが多い.
【文献】
Cokelaer et al, (2017), 'Spectrum': Spectral Analysis in Python, Journal of Open Source Software, 2(18), 348, doi:10.21105/joss.00348
【関連する外部ページ】
- specurum の GitHub のページ: https://github.com/cokelaer/spectrum
- spectrum の公式ドキュメント: http://pyspectrum.readthedocs.io/
前準備
Python 3.10,Git のインストール(Windows 上)
Pythonは,プログラミング言語の1つ. Gitは,分散型のバージョン管理システム.
【手順】
- Windows で,コマンドプロンプトを管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)
次のコマンドを実行
次のコマンドは,Python ランチャーとPython 3.10とGitをインストールし,Gitにパスを通すものである.
次のコマンドでインストールされるGitは 「git for Windows」と呼ばれるものであり, Git,MinGW などから構成されている.
winget install --scope machine Python.Launcher winget install --scope machine Python.Python.3.10 winget install --scope machine Git.Git powershell -command "$oldpath = [System.Environment]::GetEnvironmentVariable(\"Path\", \"Machine\"); $oldpath += \";c:\Program Files\Git\cmd\"; [System.Environment]::SetEnvironmentVariable(\"Path\", $oldpath, \"Machine\")"
【関連する外部ページ】
- Python の公式ページ: https://www.python.org/
- Git の公式ページ: https://git-scm.com/
【サイト内の関連ページ】
【関連項目】 Python, Git バージョン管理システム, Git の利用
parselmouth のインストール(Windows 上)
- Windows で,コマンドプロンプトを管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)
- pip を用いてインストール
pip install -U spectrum
動作確認
.wav 形式のファイルからスペクトログラムを表示. スペクトログラムは,周波数と時間を,縦横の軸とするグラフ.強度を色や明るさで表すことが多い.
次で公開されているプログラムを使用
https://github.com/cokelaer/spectrum/blob/master/examples/plot_spectrogram.py
- Windows で,コマンドプロンプトを管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)
- 公式ページから parselmouth のファイルをダウンロード
cd /d c:%HOMEPATH% rmdir /s /q spectrum git clone --recursive https://github.com/cokelaer/spectrum
- numpy, matplotlib, seaborn のインストール
pip install -U numpy matplotlib seaborn
- スペクトログラムの表示
次のページで公開されているプログラムを使用している.
https://github.com/cokelaer/spectrum/blob/master/examples/plot_spectrogram.py
次のコマンドを実行
cd /d c:%HOMEPATH% cd spectrum jupyter qtconsole
- Python プログラムの実行
Python プログラムの実行
- Windows では python (Python ランチャーは py)
- Ubuntu では python3
Python 開発環境(Jupyter Qt Console, Jupyter ノートブック (Jupyter Notebook), Jupyter Lab, Nteract, Spyder, PyCharm, PyScripterなど)も便利である.
Python のまとめ: 別ページ »にまとめ
python
from spectrum import Spectrogram, readwav data, samplerate = readwav("src/spectrum/data/dolphins.wav") p = Spectrogram(data, ws=128, W=4096, sampling=samplerate) p.periodogram() p.plot()
- PSD による Yule Walker 法
次のページで公開されているプログラムを使用している.
https://github.com/cokelaer/spectrum/blob/master/examples/plot_yule.py
次のコマンドを実行
cd /d c:%HOMEPATH% cd spectrum jupyter qtconsole
Python プログラムを実行する
from spectrum import pyule from scipy.signal import lfilter from pylab import plot, mean, log10, randn from numpy import array allpsds = [] # ARフィルタ係数のリスト a = array([1,-2.2137,2.9403,-2.1697,0.9606]) for this in range(1000): # ノイズを含むデータ x = lfilter([1], a, randn(1, 1024)) p = pyule(x[0],4, scale_by_freq=False, NFFT=256) p.plot(color='gray', alpha=0.1) allpsds.append(p.psd) plot(p.frequencies(), 10*log10(mean(allpsds, axis=0)), "red")