[autotest] audio_test_utils: Ignore certian frequencies in the check

Ignore certain frequencies in the check so test user can ignore some
known noise resource.

BUG=chromium:561256
TEST=not used yet

Change-Id: I995e42070f051a82c99afbfcf67aee0f6d49f479
Reviewed-on: https://chromium-review.googlesource.com/314430
Commit-Ready: Cheng-Yi Chiang <cychiang@chromium.org>
Tested-by: Cheng-Yi Chiang <cychiang@chromium.org>
Reviewed-by: Kalin Stoyanov <kalin@chromium.org>
diff --git a/client/cros/chameleon/audio_test_utils.py b/client/cros/chameleon/audio_test_utils.py
index fbe3b57..31cf325 100644
--- a/client/cros/chameleon/audio_test_utils.py
+++ b/client/cros/chameleon/audio_test_utils.py
@@ -230,7 +230,8 @@
 def check_recorded_frequency(
         golden_file, recorder,
         second_peak_ratio=DEFAULT_SECOND_PEAK_RATIO,
-        frequency_diff_threshold=DEFAULT_FREQUENCY_DIFF_THRESHOLD):
+        frequency_diff_threshold=DEFAULT_FREQUENCY_DIFF_THRESHOLD,
+        ignore_frequencies=None):
     """Checks if the recorded data contains sine tone of golden frequency.
 
     @param golden_file: An AudioTestData object that serves as golden data.
@@ -243,6 +244,11 @@
                                      frequency of test signal and golden
                                      frequency. This value should be small for
                                      signal passed through line.
+    @param ignore_frequencies: A list of frequencies to be ignored. The
+                               component in the spectral with frequency too
+                               close to the frequency in the list will be
+                               ignored. The comparison of frequencies uses
+                               frequency_diff_threshold as well.
 
     @raises error.TestFail if the recorded data does not contain sine tone of
             golden frequency.
@@ -286,6 +292,24 @@
                     'Channel %d: Dominant frequency %s is away from golden %s' %
                     (test_channel, dominant_frequency, golden_frequency))
 
+        def should_be_ignored(frequency):
+            """Checks if frequency is close to any frequency in ignore list.
+
+            @param frequency: The frequency to be tested.
+
+            @returns: True if the frequency should be ignored. False otherwise.
+
+            """
+            for ignore_frequency in ignore_frequencies:
+                if (abs(frequency - ignore_frequency) <
+                    frequency_diff_threshold):
+                    logging.debug('Ignore frequency: %s', frequency)
+                    return True
+
+        # Filter out the frequencies to be ignored.
+        if ignore_frequencies:
+            spectral = [x for x in spectral if not should_be_ignored(x[0])]
+
         if len(spectral) > 1:
             first_coeff = spectral[0][1]
             second_coeff = spectral[1][1]