subtractor.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2017 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. #ifndef MODULES_AUDIO_PROCESSING_AEC3_SUBTRACTOR_H_
  11. #define MODULES_AUDIO_PROCESSING_AEC3_SUBTRACTOR_H_
  12. #include <math.h>
  13. #include <stddef.h>
  14. #include <array>
  15. #include <vector>
  16. #include "api/array_view.h"
  17. #include "api/audio/echo_canceller3_config.h"
  18. #include "modules/audio_processing/aec3/adaptive_fir_filter.h"
  19. #include "modules/audio_processing/aec3/aec3_common.h"
  20. #include "modules/audio_processing/aec3/aec3_fft.h"
  21. #include "modules/audio_processing/aec3/aec_state.h"
  22. #include "modules/audio_processing/aec3/coarse_filter_update_gain.h"
  23. #include "modules/audio_processing/aec3/echo_path_variability.h"
  24. #include "modules/audio_processing/aec3/refined_filter_update_gain.h"
  25. #include "modules/audio_processing/aec3/render_buffer.h"
  26. #include "modules/audio_processing/aec3/render_signal_analyzer.h"
  27. #include "modules/audio_processing/aec3/subtractor_output.h"
  28. #include "modules/audio_processing/logging/apm_data_dumper.h"
  29. #include "rtc_base/checks.h"
  30. namespace webrtc {
  31. // Proves linear echo cancellation functionality
  32. class Subtractor {
  33. public:
  34. Subtractor(const EchoCanceller3Config& config,
  35. size_t num_render_channels,
  36. size_t num_capture_channels,
  37. ApmDataDumper* data_dumper,
  38. Aec3Optimization optimization);
  39. ~Subtractor();
  40. Subtractor(const Subtractor&) = delete;
  41. Subtractor& operator=(const Subtractor&) = delete;
  42. // Performs the echo subtraction.
  43. void Process(const RenderBuffer& render_buffer,
  44. const std::vector<std::vector<float>>& capture,
  45. const RenderSignalAnalyzer& render_signal_analyzer,
  46. const AecState& aec_state,
  47. rtc::ArrayView<SubtractorOutput> outputs);
  48. void HandleEchoPathChange(const EchoPathVariability& echo_path_variability);
  49. // Exits the initial state.
  50. void ExitInitialState();
  51. // Returns the block-wise frequency responses for the refined adaptive
  52. // filters.
  53. const std::vector<std::vector<std::array<float, kFftLengthBy2Plus1>>>&
  54. FilterFrequencyResponses() const {
  55. return refined_frequency_responses_;
  56. }
  57. // Returns the estimates of the impulse responses for the refined adaptive
  58. // filters.
  59. const std::vector<std::vector<float>>& FilterImpulseResponses() const {
  60. return refined_impulse_responses_;
  61. }
  62. void DumpFilters() {
  63. data_dumper_->DumpRaw(
  64. "aec3_subtractor_h_refined",
  65. rtc::ArrayView<const float>(
  66. refined_impulse_responses_[0].data(),
  67. GetTimeDomainLength(
  68. refined_filters_[0]->max_filter_size_partitions())));
  69. refined_filters_[0]->DumpFilter("aec3_subtractor_H_refined");
  70. coarse_filter_[0]->DumpFilter("aec3_subtractor_H_coarse");
  71. }
  72. private:
  73. class FilterMisadjustmentEstimator {
  74. public:
  75. FilterMisadjustmentEstimator() = default;
  76. ~FilterMisadjustmentEstimator() = default;
  77. // Update the misadjustment estimator.
  78. void Update(const SubtractorOutput& output);
  79. // GetMisadjustment() Returns a recommended scale for the filter so the
  80. // prediction error energy gets closer to the energy that is seen at the
  81. // microphone input.
  82. float GetMisadjustment() const {
  83. RTC_DCHECK_GT(inv_misadjustment_, 0.0f);
  84. // It is not aiming to adjust all the estimated mismatch. Instead,
  85. // it adjusts half of that estimated mismatch.
  86. return 2.f / sqrtf(inv_misadjustment_);
  87. }
  88. // Returns true if the prediciton error energy is significantly larger
  89. // than the microphone signal energy and, therefore, an adjustment is
  90. // recommended.
  91. bool IsAdjustmentNeeded() const { return inv_misadjustment_ > 10.f; }
  92. void Reset();
  93. void Dump(ApmDataDumper* data_dumper) const;
  94. private:
  95. const int n_blocks_ = 4;
  96. int n_blocks_acum_ = 0;
  97. float e2_acum_ = 0.f;
  98. float y2_acum_ = 0.f;
  99. float inv_misadjustment_ = 0.f;
  100. int overhang_ = 0.f;
  101. };
  102. const Aec3Fft fft_;
  103. ApmDataDumper* data_dumper_;
  104. const Aec3Optimization optimization_;
  105. const EchoCanceller3Config config_;
  106. const size_t num_capture_channels_;
  107. std::vector<std::unique_ptr<AdaptiveFirFilter>> refined_filters_;
  108. std::vector<std::unique_ptr<AdaptiveFirFilter>> coarse_filter_;
  109. std::vector<std::unique_ptr<RefinedFilterUpdateGain>> refined_gains_;
  110. std::vector<std::unique_ptr<CoarseFilterUpdateGain>> coarse_gains_;
  111. std::vector<FilterMisadjustmentEstimator> filter_misadjustment_estimators_;
  112. std::vector<size_t> poor_coarse_filter_counters_;
  113. std::vector<std::vector<std::array<float, kFftLengthBy2Plus1>>>
  114. refined_frequency_responses_;
  115. std::vector<std::vector<float>> refined_impulse_responses_;
  116. };
  117. } // namespace webrtc
  118. #endif // MODULES_AUDIO_PROCESSING_AEC3_SUBTRACTOR_H_