benfmiller/audalign

[Feature] Add a filter for matches that are too close to each other

Closed this issue · 2 comments

I need to get a list of all posible matches that an audio could have, and by looking at the code I found that I could get a list of matches using match_len_filter, however most of results were around the same second and filter_matches wasn't useful at solving that problem, so I made this function for my script that filters numbers that are close to each other while conserving the original order, ex 1.5, 1.1, 60.1, 60.5, 30.4; it would return 1.5, 60.1, 30.4, and I thought it could also be useful for this program.

In my case I needed to remove matches that have less than 0.5 absolute difference, but it could be changed easily.

def remove_close_numbers_by_abs_diff(nums):
    if not nums:
        return []

    output = [nums[0]]

    for num in nums[1:]:
        if all(abs(num - prev) > 0.5 for prev in output):
            output.append(num)

    return output
import unittest

class TestRemoveCloseNumbers(unittest.TestCase):

    def test_remove_close_numbers(self):
        self.assertEqual(remove_close_numbers_by_abs_diff([1.5, 1.1, 3.2, 3.9, 5, 5.9, 0.5, 3.3, 3.3]), [1.5, 3.2, 3.9, 5, 5.9, 0.5])
        self.assertEqual(remove_close_numbers_by_abs_diff([1, 1, 2.2, 2.3, 2.5, 3.5, 4.4, 4.8]), [1, 2.2, 3.5, 4.4])
        self.assertEqual(remove_close_numbers_by_abs_diff([10, 10, 2.2, 2.3, 2.5, 1.5, 1, 0.8]), [10, 2.2, 1.5, 0.8])
        self.assertEqual(remove_close_numbers_by_abs_diff([2, 3]), [2, 3])
        self.assertEqual(remove_close_numbers_by_abs_diff([1, 3, 5, 7, 9]), [1, 3, 5, 7, 9])
        self.assertEqual(remove_close_numbers_by_abs_diff([]), [])


if __name__ == '__main__':
    unittest.main()

Seems like a neat idea! How does a close_seconds_filterfield in the recognizer config class sound? I could add the feature in a week or two

Yes, sound good!