diff --git a/auglib/core/transform.py b/auglib/core/transform.py index 808ca87..7387129 100644 --- a/auglib/core/transform.py +++ b/auglib/core/transform.py @@ -3261,16 +3261,20 @@ def _pink_noise(self, samples: int) -> np.ndarray: def psd(f): return 1 / np.where(f == 0, float("inf"), np.sqrt(f)) - white_noise = np.fft.rfft(np.random.randn(samples)) + # Add extra sample + # to ensure correct length for odd samples + length = samples + 1 + + white_noise = np.fft.rfft(np.random.randn(length)) # Normalized pink noise shape - pink_shape = psd(np.fft.rfftfreq(samples)) + pink_shape = psd(np.fft.rfftfreq(length)) pink_shape = pink_shape / np.sqrt(np.mean(pink_shape**2)) white_noise_shaped = white_noise * pink_shape pink_noise = np.fft.irfft(white_noise_shaped) - return np.atleast_2d(pink_noise) + return np.atleast_2d(pink_noise[:samples]) class Prepend(Base): diff --git a/tests/test_transform_pink_noise.py b/tests/test_transform_pink_noise.py index 4a34ee3..ef80f4c 100644 --- a/tests/test_transform_pink_noise.py +++ b/tests/test_transform_pink_noise.py @@ -74,3 +74,19 @@ def test_pink_noise(duration, sampling_rate, gain_db, snr_db): assert noise.shape == expected_noise.shape assert noise.dtype == expected_noise.dtype np.testing.assert_almost_equal(noise, expected_noise) + + +@pytest.mark.parametrize( + "signal", + [ + # Odd, + # compare https://github.com/audeering/auglib/issues/23 + np.ones((1, 30045)), + # Even + np.ones((1, 200)), + ], +) +def test_pink_noise_odd_and_even_samples(signal): + transform = auglib.transform.PinkNoise() + augmented_signal = transform(signal) + assert signal.shape == augmented_signal.shape