xref: /aosp_15_r20/external/webrtc/modules/audio_processing/agc2/rnn_vad/lp_residual_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2018 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/lp_residual.h"
12 
13 #include <algorithm>
14 #include <array>
15 #include <vector>
16 
17 #include "modules/audio_processing/agc2/rnn_vad/common.h"
18 #include "modules/audio_processing/agc2/rnn_vad/test_utils.h"
19 // TODO(bugs.webrtc.org/8948): Add when the issue is fixed.
20 // #include "test/fpe_observer.h"
21 #include "test/gtest.h"
22 
23 namespace webrtc {
24 namespace rnn_vad {
25 namespace {
26 
27 // Checks that the LP residual can be computed on an empty frame.
TEST(RnnVadTest,LpResidualOfEmptyFrame)28 TEST(RnnVadTest, LpResidualOfEmptyFrame) {
29   // TODO(bugs.webrtc.org/8948): Add when the issue is fixed.
30   // FloatingPointExceptionObserver fpe_observer;
31 
32   // Input frame (empty, i.e., all samples set to 0).
33   std::array<float, kFrameSize10ms24kHz> empty_frame;
34   empty_frame.fill(0.f);
35   // Compute inverse filter coefficients.
36   std::array<float, kNumLpcCoefficients> lpc;
37   ComputeAndPostProcessLpcCoefficients(empty_frame, lpc);
38   // Compute LP residual.
39   std::array<float, kFrameSize10ms24kHz> lp_residual;
40   ComputeLpResidual(lpc, empty_frame, lp_residual);
41 }
42 
43 // Checks that the computed LP residual is bit-exact given test input data.
TEST(RnnVadTest,LpResidualPipelineBitExactness)44 TEST(RnnVadTest, LpResidualPipelineBitExactness) {
45   // Input and expected output readers.
46   ChunksFileReader pitch_buffer_reader = CreatePitchBuffer24kHzReader();
47   ChunksFileReader lp_pitch_reader = CreateLpResidualAndPitchInfoReader();
48 
49   // Buffers.
50   std::vector<float> pitch_buffer_24kHz(kBufSize24kHz);
51   std::array<float, kNumLpcCoefficients> lpc;
52   std::vector<float> computed_lp_residual(kBufSize24kHz);
53   std::vector<float> expected_lp_residual(kBufSize24kHz);
54 
55   // Test length.
56   const int num_frames =
57       std::min(pitch_buffer_reader.num_chunks, 300);  // Max 3 s.
58   ASSERT_GE(lp_pitch_reader.num_chunks, num_frames);
59 
60   // TODO(bugs.webrtc.org/8948): Add when the issue is fixed.
61   // FloatingPointExceptionObserver fpe_observer;
62   for (int i = 0; i < num_frames; ++i) {
63     SCOPED_TRACE(i);
64     // Read input.
65     ASSERT_TRUE(pitch_buffer_reader.reader->ReadChunk(pitch_buffer_24kHz));
66     // Read expected output (ignore pitch gain and period).
67     ASSERT_TRUE(lp_pitch_reader.reader->ReadChunk(expected_lp_residual));
68     lp_pitch_reader.reader->SeekForward(2);  // Pitch period and strength.
69     // Check every 200 ms.
70     if (i % 20 == 0) {
71       ComputeAndPostProcessLpcCoefficients(pitch_buffer_24kHz, lpc);
72       ComputeLpResidual(lpc, pitch_buffer_24kHz, computed_lp_residual);
73       ExpectNearAbsolute(expected_lp_residual, computed_lp_residual, kFloatMin);
74     }
75   }
76 }
77 
78 }  // namespace
79 }  // namespace rnn_vad
80 }  // namespace webrtc
81