Last active
June 13, 2024 02:32
-
-
Save tam17aki/53abbeffe02aa2a74c6cd9cfe55da497 to your computer and use it in GitHub Desktop.
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
import numpy as np | |
import soundfile as sf | |
from oct2py import octave | |
from scipy import signal | |
WIN_LEN = 512 | |
HOP_LEN = 128 | |
FFT_LEN = 512 | |
WINDOW = "hann" | |
IN_WAV = "ONOMATOPEE300_001.wav" | |
OUT_WAV = "hoge.wav" | |
def main(): | |
"""Reconstruct phase by using PGHI.""" | |
# 事前にoctave上で | |
# ltfatstart & ltfatmex | |
# phaseretstart & phaseretmex | |
# を済ませておく | |
octave.addpath(octave.genpath("/work/tamamori/ltfat-main")) | |
octave.ltfatstart(0) # 引数の0を省略するとoct2pyのエラーでこける | |
octave.phaseretstart(0) # 引数の0を省略するとoct2pyのエラーでこける | |
# 振幅スペクトルの取得 | |
audio, fs = sf.read(IN_WAV) | |
stfft = signal.ShortTimeFFT( | |
win=signal.get_window(WINDOW, WIN_LEN), hop=HOP_LEN, fs=int(fs), mfft=FFT_LEN | |
) | |
amp_spec = np.abs(stfft.stft(audio)) | |
# PGHIに基づく位相復元 | |
gamma = octave.pghi_findgamma("hann", HOP_LEN, WIN_LEN) | |
reconst_spec = octave.pghi(amp_spec, gamma, HOP_LEN, WIN_LEN) | |
audio = stfft.istft(reconst_spec) | |
sf.write(OUT_WAV, audio, fs) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment