diff --git a/audio_offset_finder/audio_offset_finder.py b/audio_offset_finder/audio_offset_finder.py index c9ff31e..2ce81eb 100644 --- a/audio_offset_finder/audio_offset_finder.py +++ b/audio_offset_finder/audio_offset_finder.py @@ -161,7 +161,7 @@ def find_offset_between_buffers(buffer1, buffer2, fs, hop_length=128, win_length } -# returns an array in which the first half represents an offset of mfcc2 within mfcc2, +# returns an array in which the first half represents an offset of mfcc2 within mfcc1, # and the second half (accessed by negative indices) vice-versa. def cross_correlation(mfcc1, mfcc2, nframes): """Calculate the cross-correlation curve between two numpy arrays (assumed to be MFCCs). @@ -179,15 +179,15 @@ def cross_correlation(mfcc1, mfcc2, nframes): ------- A numpy array containing the cross-correlation of the two arrays for each possible offset between them. The zeroth element contains the cross-correlation with no offset. - The first half of the array (up to len(c)/2) contains offsets of mfcc2 within mfcc1. - The second half of the array contains offsets of mfcc1 within mfcc2. - This is done so that accessing the array with an index in the range -(len(c)/2) to (len(c)/2) will return + The first part of the array (with negative indices) contains offsets of mfcc1 within mfcc2. + The second half of the array (with positive indices) contains offsets of mfcc2 within mfcc1. + This is done so that accessing the array with an index in the range o_min to o_max will return an appropriate cross-correlation coefficient for that offset. """ n1, mdim1 = mfcc1.shape n2, mdim2 = mfcc2.shape - o_min = nframes - min(n1, n2) - o_max = min(n1, n2) - nframes + 1 + o_min = nframes - n2 + o_max = n1 - nframes + 1 n = o_max - o_min c = np.zeros(n) for k in range(o_min, 0): diff --git a/audio_offset_finder/cli.py b/audio_offset_finder/cli.py index d76f203..1c32dae 100644 --- a/audio_offset_finder/cli.py +++ b/audio_offset_finder/cli.py @@ -73,13 +73,12 @@ def main(argv): plot_results(args, results) -# Re-order the cross-correlation array so that the zero offset is in the middle -def reorder_correlations(cc): +# Re-order the cross-correlation array so that the index of the earliest frame offset is at one end of the range +def reorder_correlations(cc, earliest_frame_offset): from numpy import concatenate num_elements = len(cc) - centre_index = int(num_elements / 2) - return concatenate((cc[centre_index:], cc[:centre_index])) + return concatenate((cc[earliest_frame_offset:], cc[:earliest_frame_offset])) def plot_results(args, results): @@ -88,7 +87,7 @@ def plot_results(args, results): cc = results["correlation"] cc_length = len(cc) - plot_data = reorder_correlations(cc) + plot_data = reorder_correlations(cc, results["earliest_frame_offset"]) pyplot.figure(figsize=(15, 5)) xaxis_range = range(results["earliest_frame_offset"], results["latest_frame_offset"]) diff --git a/requirements.txt b/requirements.txt index b8c3944..dcfa3e3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ librosa==0.10.2 matplotlib==3.7.3 -numpy==1.22.0 +numpy==1.22.3 pytest==6.2.5 scipy==1.10.0 diff --git a/setup.cfg b/setup.cfg index 7e0451f..ddcadf1 100644 --- a/setup.cfg +++ b/setup.cfg @@ -17,7 +17,7 @@ packages = find: install_requires = librosa >= 0.10.2 matplotlib >= 3.7.3 - numpy >= 1.22.0 + numpy >= 1.22.3,<2 scipy >= 1.10.0 [options.extras_require] diff --git a/tests/tool_test.py b/tests/tool_test.py index 3b8d75b..e979486 100644 --- a/tests/tool_test.py +++ b/tests/tool_test.py @@ -26,10 +26,10 @@ def test_reorder_correlations(): input_array1 = np.array([0, 1, 2, 3]) - np.testing.assert_array_equal(reorder_correlations(input_array1), np.array([2, 3, 0, 1])) + np.testing.assert_array_equal(reorder_correlations(input_array1, 2), np.array([2, 3, 0, 1])) input_array2 = np.array([0, 1, 2, 3, 4]) - np.testing.assert_array_equal(reorder_correlations(input_array2), np.array([2, 3, 4, 0, 1])) + np.testing.assert_array_equal(reorder_correlations(input_array2, 2), np.array([2, 3, 4, 0, 1])) def test_tool():