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 test {
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_coeffs;
37 ComputeAndPostProcessLpcCoefficients(empty_frame, lpc_coeffs);
38 // Compute LP residual.
39 std::array<float, kFrameSize10ms24kHz> lp_residual;
40 ComputeLpResidual(lpc_coeffs, 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 auto pitch_buf_24kHz_reader = CreatePitchBuffer24kHzReader();
47 auto lp_residual_reader = CreateLpResidualAndPitchPeriodGainReader();
48
49 // Buffers.
50 std::vector<float> pitch_buf_data(kBufSize24kHz);
51 std::array<float, kNumLpcCoefficients> lpc_coeffs;
52 std::vector<float> computed_lp_residual(kBufSize24kHz);
53 std::vector<float> expected_lp_residual(kBufSize24kHz);
54
55 // Test length.
56 const size_t num_frames = std::min(pitch_buf_24kHz_reader.second,
57 static_cast<size_t>(300)); // Max 3 s.
58 ASSERT_GE(lp_residual_reader.second, num_frames);
59
60 {
61 // TODO(bugs.webrtc.org/8948): Add when the issue is fixed.
62 // FloatingPointExceptionObserver fpe_observer;
63 for (size_t i = 0; i < num_frames; ++i) {
64 // Read input.
65 ASSERT_TRUE(pitch_buf_24kHz_reader.first->ReadChunk(pitch_buf_data));
66 // Read expected output (ignore pitch gain and period).
67 ASSERT_TRUE(lp_residual_reader.first->ReadChunk(expected_lp_residual));
68 float unused;
69 ASSERT_TRUE(lp_residual_reader.first->ReadValue(&unused));
70 ASSERT_TRUE(lp_residual_reader.first->ReadValue(&unused));
71
72 // Check every 200 ms.
73 if (i % 20 != 0) {
74 continue;
75 }
76
77 SCOPED_TRACE(i);
78 ComputeAndPostProcessLpcCoefficients(pitch_buf_data, lpc_coeffs);
79 ComputeLpResidual(lpc_coeffs, pitch_buf_data, computed_lp_residual);
80 ExpectNearAbsolute(expected_lp_residual, computed_lp_residual, kFloatMin);
81 }
82 }
83 }
84
85 } // namespace test
86 } // namespace rnn_vad
87 } // namespace webrtc
88