Pre echo delay estimator: Explicitly considering the initial region when updating the pre echo delay histogram.

Bug: webrtc:14205
Change-Id: Iaa075a52c07ab87fe21da7c40be806c7f80f0e32
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/280540
Reviewed-by: Lionel Koenig <lionelk@webrtc.org>
Reviewed-by: Lionel Koenig <lionelk@google.com>
Reviewed-by: Per Åhgren <peah@webrtc.org>
Commit-Queue: Jesus de Vicente Pena <devicentepena@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#38489}
diff --git a/modules/audio_processing/aec3/echo_path_delay_estimator_unittest.cc b/modules/audio_processing/aec3/echo_path_delay_estimator_unittest.cc
index ae9c873..e2c101f 100644
--- a/modules/audio_processing/aec3/echo_path_delay_estimator_unittest.cc
+++ b/modules/audio_processing/aec3/echo_path_delay_estimator_unittest.cc
@@ -70,7 +70,7 @@
   constexpr size_t kNumCaptureChannels = 1;
   constexpr int kSampleRateHz = 48000;
   constexpr size_t kNumBands = NumBandsForRate(kSampleRateHz);
-
+  Random random_generator(42U);
   Block render(kNumBands, kNumRenderChannels);
   Block capture(/*num_bands=*/1, kNumCaptureChannels);
   ApmDataDumper data_dumper(0);
@@ -81,9 +81,6 @@
     config.delay.down_sampling_factor = down_sampling_factor;
     config.delay.num_filters = 10;
     for (size_t delay_samples : {30, 64, 150, 200, 800, 4000}) {
-      // Random generator become periodic after a while. To avoid issue in the
-      // unittest we ensure to seed it for every case.
-      Random random_generator(42U);
       SCOPED_TRACE(ProduceDebugText(delay_samples, down_sampling_factor));
       std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
           RenderDelayBuffer::Create(config, kSampleRateHz, kNumRenderChannels));
diff --git a/modules/audio_processing/aec3/matched_filter_lag_aggregator.cc b/modules/audio_processing/aec3/matched_filter_lag_aggregator.cc
index 17f517a..bea7868 100644
--- a/modules/audio_processing/aec3/matched_filter_lag_aggregator.cc
+++ b/modules/audio_processing/aec3/matched_filter_lag_aggregator.cc
@@ -18,6 +18,8 @@
 
 namespace webrtc {
 namespace {
+constexpr int kPreEchoHistogramDataNotUpdated = -1;
+
 int GetDownSamplingBlockSizeLog2(int down_sampling_factor) {
   int down_sampling_factor_log2 = 0;
   down_sampling_factor >>= 1;
@@ -129,7 +131,7 @@
 
 void MatchedFilterLagAggregator::PreEchoLagAggregator::Reset() {
   std::fill(histogram_.begin(), histogram_.end(), 0);
-  histogram_data_.fill(0);
+  histogram_data_.fill(kPreEchoHistogramDataNotUpdated);
   histogram_data_index_ = 0;
   pre_echo_candidate_ = 0;
 }
@@ -141,7 +143,10 @@
              pre_echo_block_size < static_cast<int>(histogram_.size()));
   pre_echo_block_size =
       rtc::SafeClamp(pre_echo_block_size, 0, histogram_.size() - 1);
-  if (histogram_[histogram_data_[histogram_data_index_]] > 0) {
+  // Remove the oldest point from the `histogram_`, it ignores the initial
+  // points where no updates have been done to the `histogram_data_` array.
+  if (histogram_data_[histogram_data_index_] !=
+      kPreEchoHistogramDataNotUpdated) {
     --histogram_[histogram_data_[histogram_data_index_]];
   }
   histogram_data_[histogram_data_index_] = pre_echo_block_size;