forked from google/lyra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
noise_estimator_test.cc
149 lines (126 loc) · 4.89 KB
/
noise_estimator_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "noise_estimator.h"
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <memory>
#include <vector>
#include "absl/types/optional.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace chromemedia {
namespace codec {
namespace {
static constexpr float kNumSecondsPerFrame = 0.025f;
static const int kNumFramesPerSecond = std::round(1 / kNumSecondsPerFrame);
static constexpr float kMaxAbsError = 1e-1f;
static constexpr int kTestNumFeatures = 160;
static constexpr float kBoundHalfLifeSec = 1.f;
static constexpr int kNumSeconds = 4;
class NoiseEstimatorTest : public ::testing::Test {
protected:
void SetUp() override {
noise_estimator_ =
NoiseEstimator::Create(kTestNumFeatures, kNumSecondsPerFrame);
ASSERT_NE(noise_estimator_, nullptr);
}
// Sparsely populates a vector with uniform power values.
std::vector<float> RandomSignal(const std::vector<float>& noise) {
// Each frequency bin has a 1 in 10 probability of containing a uniform
// signal.
const int kSignalProbability = 10;
const float kSignalPower = 1;
std::vector<float> signal(noise);
for (auto& sample : signal) {
int signal_prob = rand_r(&seed_) % kSignalProbability;
if (signal_prob == 0) {
sample = kSignalPower;
}
}
return signal;
}
// Adds a small amount of variability to the base noise.
std::vector<float> RandomNoise(const std::vector<float>& base_noise) {
const float kNoiseVariability = 1e-1f;
std::vector<float> noise(base_noise);
for (auto& sample : noise) {
sample += -kNoiseVariability +
static_cast<float>(rand_r(&seed_)) /
(static_cast<float>(RAND_MAX) / (2.f * kNoiseVariability));
}
return noise;
}
// Noise approximated by a line across frequency bins.
std::vector<float> BaseNoise() {
const float kNoiseLowerBound = -2.f;
const float kNoiseUpperBound = -1.f;
std::vector<float> noise(kTestNumFeatures);
// Approximate the base noise with a line.
float rise = (kNoiseLowerBound - kNoiseUpperBound) / kTestNumFeatures;
for (int i = 0; i < noise.size(); i++) {
noise.at(i) = rise * i + kNoiseUpperBound;
}
return noise;
}
std::unique_ptr<NoiseEstimator> noise_estimator_;
uint seed_ = 1;
};
TEST_F(NoiseEstimatorTest, ThreeSecondsEstimate) {
const std::vector<float> kBaseNoise = BaseNoise();
std::vector<float> noise = RandomNoise(kBaseNoise);
ASSERT_TRUE(noise_estimator_->Update(noise));
// Do not add the uniform signal to the first frame.
for (int i = 1; i < kNumSeconds * kNumFramesPerSecond; ++i) {
noise = RandomNoise(kBaseNoise);
std::vector<float> signal = RandomSignal(noise);
ASSERT_TRUE(noise_estimator_->Update(signal));
}
EXPECT_THAT(noise_estimator_->NoiseEstimate(),
testing::Pointwise(testing::FloatNear(kMaxAbsError), kBaseNoise));
}
TEST_F(NoiseEstimatorTest, NoiseIdentification) {
const std::vector<float> kBaseNoise = BaseNoise();
const std::vector<float> wrong_size_vector(kTestNumFeatures - 1);
EXPECT_FALSE(noise_estimator_->IsSimilarNoise(wrong_size_vector).has_value());
// Run noise through estimator first.
for (int i = 1; i < kNumSeconds * kNumFramesPerSecond; ++i) {
const std::vector<float> noise = RandomNoise(kBaseNoise);
ASSERT_TRUE(noise_estimator_->Update(noise));
}
EXPECT_TRUE(
noise_estimator_->IsSimilarNoise(RandomNoise(kBaseNoise)).value());
EXPECT_FALSE(
noise_estimator_->IsSimilarNoise(RandomSignal(RandomNoise(kBaseNoise)))
.value());
}
TEST_F(NoiseEstimatorTest, BoundsDecay) {
const std::vector<float> kBaseNoise = BaseNoise();
// Run noise through estimator first.
for (int i = 1; i < kNumSeconds * kNumFramesPerSecond; ++i) {
const std::vector<float> noise = RandomNoise(kBaseNoise);
ASSERT_TRUE(noise_estimator_->Update(noise));
}
const float kOneHalfLifeFrames = kBoundHalfLifeSec / kNumSecondsPerFrame;
auto similar_noise = RandomNoise(kBaseNoise);
EXPECT_TRUE(noise_estimator_->IsSimilarNoise(similar_noise).value());
// Re-query for one half-life
for (int i = 0; i < kOneHalfLifeFrames; ++i) {
ASSERT_TRUE(noise_estimator_->IsSimilarNoise(similar_noise).has_value());
}
EXPECT_FALSE(noise_estimator_->IsSimilarNoise(similar_noise).value());
}
} // namespace
} // namespace codec
} // namespace chromemedia