From b56efb0c88dbc424351ea3e1692e6c5e1f66aeaa Mon Sep 17 00:00:00 2001 From: comavius Date: Wed, 9 Sep 2026 23:51:47 +0900 Subject: [PATCH] Speed up cross-correlation with FFT convolution This replaces the O((n1 + n2) * nframes * numcep) sequential offset scan with O((n1 log n1 + n2 log n2) * numcep) FFT convolution. --- audio_offset_finder/audio_offset_finder.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/audio_offset_finder/audio_offset_finder.py b/audio_offset_finder/audio_offset_finder.py index 694ecb2..9e9069a 100644 --- a/audio_offset_finder/audio_offset_finder.py +++ b/audio_offset_finder/audio_offset_finder.py @@ -16,6 +16,7 @@ from subprocess import Popen, PIPE from scipy.io import wavfile +from scipy.signal import fftconvolve import librosa import os import tempfile @@ -195,12 +196,10 @@ def cross_correlation(mfcc1, mfcc2, nframes): o_max = n1 - nframes + 1 n = o_max - o_min c = np.zeros(n) - for k in range(o_min, 0): - cc = np.sum(np.multiply(mfcc1[:nframes], mfcc2[-k : nframes - k]), axis=0) - c[k] = np.linalg.norm(cc) - for k in range(0, o_max): - cc = np.sum(np.multiply(mfcc1[k : k + nframes], mfcc2[:nframes]), axis=0) - c[k] = np.linalg.norm(cc) + ccs_neg = fftconvolve(mfcc2, mfcc1[:nframes][::-1], mode="valid", axes=0) + c[o_max:] = np.linalg.norm(ccs_neg[:0:-1], axis=1) + ccs_pos = fftconvolve(mfcc1, mfcc2[:nframes][::-1], mode="valid", axes=0) + c[:o_max] = np.linalg.norm(ccs_pos, axis=1) return c, o_min, o_max