Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use rfft when appropriate in autocorrelate, fixes #1526 #1565

Merged
merged 1 commit into from Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 17 additions & 11 deletions librosa/core/audio.py
Expand Up @@ -847,24 +847,30 @@ def autocorrelate(y, *, max_size=None, axis=-1):

max_size = int(min(max_size, y.shape[axis]))

# Compute the power spectrum along the chosen axis
# Pad out the signal to support full-length auto-correlation.
fft = get_fftlib()
powspec = util.abs2(fft.fft(y, n=2 * y.shape[axis] + 1, axis=axis))

# Convert back to time domain
autocorr = fft.ifft(powspec, axis=axis)
# Pad out the signal to support full-length auto-correlation.
n_pad = 2 * y.shape[axis] - 1

if np.iscomplexobj(y):
# Compute the power spectrum along the chosen axis
powspec = util.abs2(fft.fft(y, n=n_pad, axis=axis))

# Convert back to time domain
autocorr = fft.ifft(powspec, n=n_pad, axis=axis)
else:
# Compute the power spectrum along the chosen axis
# Pad out the signal to support full-length auto-correlation.
powspec = util.abs2(fft.rfft(y, n=n_pad, axis=axis))

# Convert back to time domain
autocorr = fft.irfft(powspec, n=n_pad, axis=axis)

# Slice down to max_size
subslice = [slice(None)] * autocorr.ndim
subslice[axis] = slice(max_size)

autocorr = autocorr[tuple(subslice)]

if not np.iscomplexobj(y):
autocorr = autocorr.real

return autocorr
return autocorr[tuple(subslice)]


def lpc(y, *, order, axis=-1):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_core.py
Expand Up @@ -966,7 +966,7 @@ def test_get_duration_fail():

@pytest.mark.parametrize(
"y",
[np.random.randn(256, 256), np.exp(1.0j * np.random.randn(256, 256))],
[np.random.randn(256, 384), np.exp(1.0j * np.random.randn(256, 384))],
ids=["real", "complex"],
)
@pytest.mark.parametrize("axis", [0, 1, -1])
Expand Down