griffin#

class diffsptk.GriffinLim(frame_length: int, frame_period: int, fft_length: int, *, center: bool = True, mode: str = 'constant', window: str = 'blackman', norm: str = 'power', symmetric: bool = True, n_iter: int = 100, alpha: float = 0.99, beta: float = 0.99, gamma: float = 1.1, init_phase: str = 'random', verbose: bool = False, device: device | None = None, dtype: dtype | None = None)[source]#

Griffin-Lim phase reconstruction module.

Parameters:
frame_lengthint >= 1

The frame length in samples, \(L\).

frame_periodint >= 1

The frame period in samples, \(P\).

fft_lengthint >= L

The number of FFT bins, \(N\).

centerbool

If True, pad the input on both sides so that the frame is centered.

window[‘blackman’, ‘hamming’, ‘hanning’, ‘bartlett’, ‘trapezoidal’, ‘rectangular’, ‘nuttall’]

The window type.

norm[‘none’, ‘power’, ‘magnitude’]

The normalization type of the window.

symmetricbool

If True, the window is symmetric, otherwise periodic.

n_iterint >= 1

The number of iterations for phase reconstruction.

alphafloat >= 0

The momentum factor, \(\alpha\).

betafloat >= 0

The momentum factor, \(\beta\).

gammafloat >= 0

The smoothing factor, \(\gamma\).

init_phase[‘zeros’, ‘random’]

The initial phase for the reconstruction.

verbosebool

If True, print the SNR at each iteration.

devicetorch.device or None

The device of this module.

dtypetorch.dtype or None

The data type of this module.

References

[1]

R. Nenov et al., “Faster than fast: Accelerating the Griffin-Lim algorithm,” Proceedings of ICASSP, 2023.

forward(y: Tensor, out_length: int | None = None) Tensor[source]#

Reconstruct a waveform from the spectrum using the Griffin-Lim algorithm.

Parameters:
yTensor [shape=(…, T/P, N/2+1)]

The power spectrum.

out_lengthint > 0 or None

The length of the output waveform.

Returns:
outTensor [shape=(…, T)]

The reconstructed waveform.

Examples

>>> x = diffsptk.ramp(1, 3)
>>> x
tensor([1., 2., 3.])
>>> stft_params = {"frame_length": 3, "frame_period": 1, "fft_length": 8}
>>> stft = diffsptk.STFT(**stft_params, out_format="power")
>>> griffin = diffsptk.GriffinLim(**stft_params, n_iter=10, init_phase="zeros")
>>> y = griffin(stft(x), out_length=3)
>>> y
tensor([ 1.0000,  2.0000, -3.0000])
diffsptk.functional.griffin(y: Tensor, *, out_length: int | None = None, frame_length: int = 400, frame_period: int = 80, fft_length: int = 512, center: bool = True, mode: str = 'constant', window: str = 'blackman', norm: str = 'power', symmetric: bool = True, n_iter: int = 100, alpha: float = 0.99, beta: float = 0.99, gamma: float = 1.1, init_phase: str = 'random', verbose: bool = False) Tensor[source]#

Reconstruct a waveform from the spectrum using the Griffin-Lim algorithm.

Parameters:
yTensor [shape=(…, T/P, N/2+1)]

The power spectrum.

out_lengthint > 0 or None

The length of the output waveform.

frame_lengthint >= 1

The frame length in samples, \(L\).

frame_periodint >= 1

The frame period in samples, \(P\).

fft_lengthint >= L

The number of FFT bins, \(N\).

centerbool

If True, pad the input on both sides so that the frame is centered.

window[‘blackman’, ‘hamming’, ‘hanning’, ‘bartlett’, ‘trapezoidal’, ‘rectangular’, ‘nuttall’]

The window type.

norm[‘none’, ‘power’, ‘magnitude’]

The normalization type of the window.

symmetricbool

If True, the window is symmetric, otherwise periodic.

n_iterint >= 1

The number of iterations for phase reconstruction.

alphafloat >= 0

The momentum factor, \(\alpha\).

betafloat >= 0

The momentum factor, \(\beta\).

gammafloat >= 0

The smoothing factor, \(\gamma\).

init_phase[‘zeros’, ‘random’]

The initial phase for the reconstruction.

verbosebool

If True, print the SNR at each iteration.

Returns:
outTensor [shape=(…, T)]

The reconstructed waveform.

See also

stft istft