Last active
September 30, 2020 16:56
-
-
Save tam17aki/b7945321382814e0da9c8e9ef2ed9522 to your computer and use it in GitHub Desktop.
librosaのGriffin-Limアルゴリズムで無矛盾位相復元
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# MIT License | |
# Copyright (C) 2020 by Akira TAMAMORI | |
# Permission is hereby granted, free of charge, to any person | |
# obtaining a copy of this software and associated documentation files | |
# (the Software"), to deal in the Software without restriction, | |
# including without limitation the rights to use, copy, modify, merge, | |
# publish, distribute, sublicense, and/or sell copies of the Software, | |
# and to permit persons to whom the Software is furnished to do so, | |
# subject to the following conditions: | |
# The above copyright notice and this permission notice shall be | |
# included in all copies or substantial portions of the Software. | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
# Commentary: | |
# - Griffin-Lim法により位相を復元する | |
import numpy as np | |
from scipy.io import wavfile | |
import librosa | |
IN_WAVE_FILE = "in.wav" # モノラル音声 | |
OUT_WAVE_FILE = "out_librosa_gla.wav" # 復元音声 | |
FRAME_LENGTH = 1024 # フレーム長 (FFTサイズ) | |
HOP_LENGTH = 80 # フレームのシフト長 | |
ITERATION = 200 # Griffin-Lim法における位相推定の最大繰り返し数 | |
# 音声のロード | |
fs, data = wavfile.read(IN_WAVE_FILE) | |
data = data.astype(np.float64) | |
# 振幅スペクトル(位相復元なので手に入るのはこれのみ) | |
amp_spec = np.abs(librosa.core.stft(data, n_fft=FRAME_LENGTH, | |
hop_length=HOP_LENGTH, | |
win_length=FRAME_LENGTH)) | |
# Griffin-Lim法により位相復元した音声 | |
recovered = librosa.griffinlim(amp_spec, n_iter=ITERATION, | |
hop_length=HOP_LENGTH, random_state=0) | |
recovered = recovered.astype(np.int16) | |
# 復元された音声をwavファイルとして保存 | |
wavfile.write(OUT_WAVE_FILE, fs, recovered) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment