def test_stft(): """Ensure every combination of even & odd configs can be handled; leave window length unspecified to ensure unspecified inverts unspecified. """ th = 1e-14 for N in (128, 129): x = np.random.randn(N) for n_fft in (120, 121): for hop_len in (1, 2, 3): for modulated in (True, False): kw = dict(hop_len=hop_len, n_fft=n_fft, modulated=modulated) Sx = stft(x, **kw) xr = istft(Sx, N=len(x), **kw) txt = ("\nSTFT: (N, n_fft, hop_len, modulated) = ({}, {}, " "{}, {})").format(N, n_fft, hop_len, modulated) assert len(x) == len(xr), "%s != %s %s" % (N, len(xr), txt) mae = np.abs(x - xr).mean() assert mae < th, "MAE = %.2e > %.2e %s" % (mae, th, txt)
def test_stft_vs_librosa(): try: lstft except: return np.random.seed(0) # try all even/odd combos for N in (512, 513): for hop_len in (1, 2, 3): for n_fft in (512, 513): for win_len in (N // 8, N // 8 - 1): x = np.random.randn(N) Sx = stft(x, n_fft=n_fft, hop_len=hop_len, win_len=win_len, window='hann', modulated=False, dtype='float64') lSx = lstft(x, n_fft=n_fft, hop_length=hop_len, win_length=win_len, window='hann', pad_mode='reflect') if n_fft % 2 == 0: if hop_len == 1: lSx = lSx[:, :-1] elif (((N % 2 == 0) and hop_len == 2) or ((N % 2 == 1) and hop_len == 3)): lSx = lSx[:, :-1] mae = np.abs(Sx - lSx).mean() assert np.allclose( Sx, lSx), ("N={}, hop_len={}, n_fft={}, win_len={}, MAE={}" ).format(N, hop_len, n_fft, win_len, mae)
def time_stft(x, dtype, n_fft): for _ in range(3): _ = stft(x, dtype=dtype, n_fft=n_fft) del _ gc.collect() return timeit(lambda: stft(x, dtype=dtype, n_fft=n_fft))