• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "audio_dsp_tests"
18 
19 #include <audio_utils/dsp_utils.h>
20 
21 #include <gtest/gtest.h>
22 #include <utils/Log.h>
23 
24 using namespace android::audio_utils;
25 
26 /*
27  * Check behavior on edge cases of 0 count or identical data.
28  */
TEST(audio_dsp_tests,edge_cases)29 TEST(audio_dsp_tests, edge_cases) {
30     constexpr float* noData{};
31     std::vector<float> zeroData(10);
32     std::vector<float> randomData(20);
33     initUniformDistribution(randomData, -.2, .2);
34 
35     EXPECT_EQ(0, energy(noData, 0));
36     EXPECT_EQ(std::numeric_limits<float>::infinity(), snr(noData, noData, 0));
37     EXPECT_EQ(std::numeric_limits<float>::infinity(), snr(zeroData, zeroData));
38     EXPECT_EQ(std::numeric_limits<float>::infinity(), snr(randomData, randomData));
39 }
40 
41 /*
42  * We use random energy tests to determine
43  * whether the audio dsp methods works as expected.
44  *
45  * We avoid testing initUniform random number generator
46  * for audio quality but rather suitability to evaluate
47  * signal methods.
48  */
TEST(audio_dsp_tests,random_energy)49 TEST(audio_dsp_tests, random_energy) {
50     constexpr size_t frameCount = 4096;
51     constexpr size_t channelCount = 2;
52     constexpr float amplitude = 0.1;
53     constexpr size_t sampleCount = channelCount * frameCount;
54     std::vector<float> randomData(sampleCount);
55     initUniformDistribution(randomData, -amplitude, amplitude);
56 
57     // compute the expected energy in dB for a uniform distribution from -amplitude to amplitude.
58     const float expectedEnergydB = energyOfUniformDistribution(-amplitude, amplitude);
59     const float energy1dB = energy(randomData);
60     ALOGD("%s: expectedEnergydB: %f  energy1dB: %f", __func__, expectedEnergydB, energy1dB);
61     EXPECT_NEAR(energy1dB, expectedEnergydB, 0.1 /* epsilon */);  // within 0.1dB.
62 
63     std::vector<float> randomData2(sampleCount);
64     initUniformDistribution(randomData2, -amplitude, amplitude);
65     const float energy2dB = energy(randomData2);
66     EXPECT_NEAR(energy1dB, energy2dB, 0.1);  // within 0.1dB.
67     // data is correlated, see the larger epsilon.
68     EXPECT_NEAR(-3, snr(randomData, randomData2), 2. /* epsilon */);
69 
70     std::vector<float> scaledData(sampleCount);
71     constexpr float scale = 100.f;
72     std::transform(randomData.begin(), randomData.end(), scaledData.begin(),
73             [](auto e) { return e * scale; });
74     const float energyScaled = energy(scaledData);
75     const float scaledB = 20 * log10(scale);  // 40 = 20 log10(100).
76     EXPECT_NEAR(scaledB, energyScaled - energy1dB, 1. /* epsilon */);
77 }
78 
79