spec2wave

ketos.audio.utils.misc.spec2wave(image, phase_angle, num_fft, step_len, num_iters, window_func)[source]

Estimate audio signal from magnitude spectrogram.

Implements the algorithm described in

    1. Griffin and J. S. Lim, “Signal estimation from modified short-time Fourier transform,” IEEE Trans. ASSP, vol.32, no.2, pp.236–243, Apr. 1984.

Follows closely the implentation of https://github.com/tensorflow/magenta/blob/master/magenta/models/nsynth/utils.py

TODO: If possible, remove librosa dependency

Args:
image: 2d numpy array

Magnitude spectrogram, linear scale

phase_angle:

Initial condition for phase in radians

num_fft: int

Number of points used for the Fast-Fourier Transform. Same as window size.

step_len: int

Step size.

num_iters:

Number of iterations to perform.

window_func: string, tuple, number, function, np.ndarray [shape=(num_fft,)]
  • a window specification (string, tuple, or number); see scipy.signal.get_window

  • a window function, such as scipy.signal.hamming

  • a user-specified window vector of length num_fft

Returns:
audio: 1d numpy array

Audio signal

Example:
>>> #Create a simple sinusoidal audio signal with frequency of 10 Hz
>>> import numpy as np
>>> x = np.arange(1000)
>>> audio = 32600 * np.sin(2 * np.pi * 10 * x / 1000) 
>>> #Compute the Short Time Fourier Transform of the audio signal 
>>> #using a window size of 200, step size of 40, and a Hamming window,
>>> from ketos.audio.utils.misc import stft
>>> win_fun = 'hamming'
>>> mag, freq_max, num_fft, _, _ = stft(x=audio, rate=1000, seg_args={'win_len':200, 'step_len':40}, window_func=win_fun)
>>> #Estimate the original audio signal            
>>> from ketos.audio.utils.misc import spec2wave
>>> audio_est = spec2wave(image=mag, phase_angle=0, num_fft=num_fft, step_len=40, num_iters=25, window_func=win_fun)
>>> #plot the original and the estimated audio signal
>>> import matplotlib.pyplot as plt
>>> plt.clf()
>>> _ = plt.plot(audio)
>>> plt.savefig("ketos/tests/assets/tmp/sig_orig.png")
>>> _ = plt.plot(audio_est)
>>> plt.savefig("ketos/tests/assets/tmp/sig_est.png")
../_images/sig_est.png