xref: /aosp_15_r20/external/webrtc/modules/audio_processing/agc2/rnn_vad/auto_correlation_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "modules/audio_processing/agc2/rnn_vad/auto_correlation.h"
12 
13 #include "modules/audio_processing/agc2/rnn_vad/common.h"
14 #include "modules/audio_processing/agc2/rnn_vad/pitch_search_internal.h"
15 #include "modules/audio_processing/agc2/rnn_vad/test_utils.h"
16 #include "test/gtest.h"
17 
18 namespace webrtc {
19 namespace rnn_vad {
20 namespace {
21 
22 // Checks that the auto correlation function produces output within tolerance
23 // given test input data.
TEST(RnnVadTest,PitchBufferAutoCorrelationWithinTolerance)24 TEST(RnnVadTest, PitchBufferAutoCorrelationWithinTolerance) {
25   PitchTestData test_data;
26   std::array<float, kBufSize12kHz> pitch_buf_decimated;
27   Decimate2x(test_data.PitchBuffer24kHzView(), pitch_buf_decimated);
28   std::array<float, kNumLags12kHz> computed_output;
29   {
30     // TODO(bugs.webrtc.org/8948): Add when the issue is fixed.
31     // FloatingPointExceptionObserver fpe_observer;
32     AutoCorrelationCalculator auto_corr_calculator;
33     auto_corr_calculator.ComputeOnPitchBuffer(pitch_buf_decimated,
34                                               computed_output);
35   }
36   auto auto_corr_view = test_data.AutoCorrelation12kHzView();
37   ExpectNearAbsolute({auto_corr_view.data(), auto_corr_view.size()},
38                      computed_output, 3e-3f);
39 }
40 
41 // Checks that the auto correlation function computes the right thing for a
42 // simple use case.
TEST(RnnVadTest,CheckAutoCorrelationOnConstantPitchBuffer)43 TEST(RnnVadTest, CheckAutoCorrelationOnConstantPitchBuffer) {
44   // Create constant signal with no pitch.
45   std::array<float, kBufSize12kHz> pitch_buf_decimated;
46   std::fill(pitch_buf_decimated.begin(), pitch_buf_decimated.end(), 1.f);
47   std::array<float, kNumLags12kHz> computed_output;
48   {
49     // TODO(bugs.webrtc.org/8948): Add when the issue is fixed.
50     // FloatingPointExceptionObserver fpe_observer;
51     AutoCorrelationCalculator auto_corr_calculator;
52     auto_corr_calculator.ComputeOnPitchBuffer(pitch_buf_decimated,
53                                               computed_output);
54   }
55   // The expected output is a vector filled with the same expected
56   // auto-correlation value. The latter equals the length of a 20 ms frame.
57   constexpr int kFrameSize20ms12kHz = kFrameSize20ms24kHz / 2;
58   std::array<float, kNumLags12kHz> expected_output;
59   std::fill(expected_output.begin(), expected_output.end(),
60             static_cast<float>(kFrameSize20ms12kHz));
61   ExpectNearAbsolute(expected_output, computed_output, 4e-5f);
62 }
63 
64 }  // namespace
65 }  // namespace rnn_vad
66 }  // namespace webrtc
67