Skip to content

Instantly share code, notes, and snippets.

@tam17aki
Last active June 13, 2024 12:41
Show Gist options
  • Save tam17aki/7034821962b2d3926b1651e897846551 to your computer and use it in GitHub Desktop.
Save tam17aki/7034821962b2d3926b1651e897846551 to your computer and use it in GitHub Desktop.
Demonstration script of phase reconstruction with PHASRET and oct2py.
# -*- coding: utf-8 -*-
"""Demonstration script of phase reconstruction with PHASRET and oct2py.
Copyright (C) 2024 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.
"""
import argparse
from pathlib import Path
import numpy as np
import soundfile as sf
from oct2py import octave
from pesq import pesq
from scipy import signal
def main():
"""Reconstruct phase by using SPSI."""
parser = argparse.ArgumentParser()
parser.add_argument("--ltfat_dir", type=str, default="/work/tamamori/ltfat-main")
parser.add_argument("--win_len", type=int, default=512)
parser.add_argument("--hop_len", type=int, default=128)
parser.add_argument("--fft_len", type=int, default=512)
parser.add_argument("--window", type=str, default="hann")
parser.add_argument("--pghi_type", choices=["normal", "causal"], default="causal")
parser.add_argument("--in_wavdir", type=str, default="/home/tamamori")
parser.add_argument("--in_wav", type=str, default="in.wav")
args = parser.parse_args()
# initialization
octave.addpath(octave.genpath(args.ltfat_dir))
octave.ltfatstart(0)
octave.phaseretstart(0)
# compute magnitude spectrum
audio, rate = sf.read(Path(args.in_wavdir, args.in_wav))
stfft = signal.ShortTimeFFT(
win=signal.get_window(args.window, args.win_len),
hop=args.hop_len,
fs=rate,
mfft=args.fft_len,
)
mag_spec = np.abs(stfft.stft(audio))
# reconstruct phase spectrum with SPSI
reconst_spec = octave.spsi(mag_spec, args.hop_len, args.win_len)
audio_rec = stfft.istft(reconst_spec)
score = pesq(rate, audio, audio_rec, "wb")
print(f"SPSI={score}")
# reconstruct phase spectrum with PGHI
gamma = octave.pghi_findgamma(args.window, args.hop_len, args.win_len)
reconst_spec = octave.pghi(mag_spec, gamma, args.hop_len, args.win_len)
audio_rec = stfft.istft(reconst_spec)
score = pesq(rate, audio, audio_rec, "wb")
print(f"PGHI={score}")
# reconstruct phase spectrum with RTPGHI
reconst_spec = octave.rtpghi(
mag_spec, gamma, args.hop_len, args.win_len, args.pghi_type
)
audio_rec = stfft.istft(reconst_spec)
score = pesq(rate, audio, audio_rec, "wb")
print(f"RTPGHI={score}")
# reconstruct phase spectrum with RTISILA
_, n_frame = mag_spec.shape
dgtlen = octave.dgtlength(n_frame * args.hop_len, args.hop_len, args.win_len)
if dgtlen > n_frame * args.hop_len:
ratio = (dgtlen - n_frame * args.hop_len) // args.hop_len
mag_spec = np.pad(mag_spec, ((0, 0), (int(ratio), 0)))
gabwin = octave.gabwin(args.window, args.hop_len, args.win_len, args.win_len)
# online: no lookahead (0 frame)
reconst_spec = octave.rtisila(
mag_spec, gabwin, args.hop_len, args.win_len, "lookahead", 0
)
audio_rec = stfft.istft(reconst_spec)
score = pesq(rate, audio, audio_rec, "wb")
print(f"RTISILA (online)={score}")
# offline: a few lookahead frames (3 frames)
reconst_spec = octave.rtisila(
mag_spec, gabwin, args.hop_len, args.win_len, "lookahead", 3
)
audio_rec = stfft.istft(reconst_spec)
score = pesq(rate, audio, audio_rec, "wb")
print(f"RTISILA (offline)={score}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment