Webrtc AGC 算法原理初識(shí)(一)

1. AGC 初識(shí)

自動(dòng)增益控制電路的作用是:當(dāng)輸入信號(hào)電壓變化很大時(shí),保持接收機(jī)輸出電壓恒定或基本不變。具體地說(shuō),當(dāng)輸入信號(hào)很弱時(shí),接收機(jī)的增益大,自動(dòng)增益控制電路不起作用;當(dāng)輸入信號(hào)很強(qiáng)時(shí),自動(dòng)增益控制電路進(jìn)行控制,使接收機(jī)的增益減小。這樣,當(dāng)接收信號(hào)強(qiáng)度變化時(shí),接收機(jī)的輸出端的電壓或功率基本不變或保持恒定。因此對(duì)AGC電路的要求是:在輸入信號(hào)較小時(shí),AGC電路不起作用,只有當(dāng)輸入信號(hào)增大到一定程度后,AGC電路才起控制作用,使增益隨輸入信號(hào)的增大而減少。
為實(shí)現(xiàn)上述要求,必須有一個(gè)能隨外來(lái)信號(hào)強(qiáng)弱而變化的控制電壓或電流信號(hào),利用這個(gè)信號(hào)對(duì)放大器的增益自動(dòng)進(jìn)行控制。由上述分析可知,調(diào)幅中頻信號(hào)經(jīng)幅度檢波后,在它的輸出中除音頻信號(hào)外,還含有直流分量。直流分量大小與中頻載波的振幅成正比,也即與外來(lái)高頻信號(hào)成正比。因此,可將檢波器輸出的直流分量作為AGC控制信號(hào)。

AGC電路工作原理:可以分為增益受控放大電路和控制電壓形成電路。增益受控放大電路位于正向放大通路,其增益隨控制電壓U0而改變??刂齐妷盒纬呻娐返幕静考茿GC整流器和低通平滑濾波器,有時(shí)也包含門電路和直流放大器等部件。

放大器及AGC電路.png

2. webrtc 的 AGC算法

AGC是自動(dòng)增益補(bǔ)償功能(Automatic Gain Control),AGC可以自動(dòng)調(diào)麥克風(fēng)的收音量,使與會(huì)者收到一定的音量水平,不會(huì)因發(fā)言者與麥克風(fēng)的距離改變時(shí),聲音有忽大忽小聲的缺點(diǎn)。
webbrtc中的結(jié)構(gòu)如下:

AGC算法目錄結(jié)構(gòu).png
包裝的頭文件目錄.png

說(shuō)明:gain_control.h是包裝的頭文件,在apm里頭gain_control_impl調(diào)用。主要包括了接口定義函數(shù)和參數(shù)配置。

3. 主要配置

include/gain_control.h 里面定義了agc的三種模式,kAdaptiveAnalog、kAdaptiveDigital和kFixedDigital。其中,kAdaptiveAnalog帶有模擬音量調(diào)節(jié)的功能。kAdaptiveDigital是可變?cè)鲆鎍gc,但是不調(diào)節(jié)系統(tǒng)音量。kFixedDigital是固定增益的agc。

enum Mode {
    // Adaptive mode intended for use if an analog volume control is available
    // on the capture device. It will require the user to provide coupling
    // between the OS mixer controls and AGC through the |stream_analog_level()|
    // functions.
    //
    // It consists of an analog gain prescription for the audio device and a
    // digital compression stage.
    kAdaptiveAnalog,

    // Adaptive mode intended for situations in which an analog volume control
    // is unavailable. It operates in a similar fashion to the adaptive analog
    // mode, but with scaling instead applied in the digital domain. As with
    // the analog mode, it additionally uses a digital compression stage.
    kAdaptiveDigital,

    // Fixed mode which enables only the digital compression stage also used by
    // the two adaptive modes.
    //
    // It is distinguished from the adaptive modes by considering only a
    // short time-window of the input signal. It applies a fixed gain through
    // most of the input level range, and compresses (gradually reduces gain
    // with increasing level) the input signal at higher levels. This mode is
    // preferred on embedded devices where the capture signal level is
    // predictable, so that a known gain can be applied.
    kFixedDigital
  };

analog_agc.h 定義了配置targetLevelDbfs和compressionGaindB用于調(diào)節(jié)agc的動(dòng)態(tài)范圍

typedef struct {
  // Configurable parameters/variables
  uint32_t fs;                // Sampling frequency
  int16_t compressionGaindB;  // Fixed gain level in dB
  int16_t targetLevelDbfs;    // Target level in -dBfs of envelope (default -3)
  int16_t agcMode;            // Hard coded mode (adaptAna/adaptDig/fixedDig)
  uint8_t limiterEnable;      // Enabling limiter (on/off (default off))
  WebRtcAgcConfig defaultConfig;
  WebRtcAgcConfig usedConfig;

  // General variables
  int16_t initFlag;
  int16_t lastError;

  // Target level parameters
  // Based on the above: analogTargetLevel = round((32767*10^(-22/20))^2*16/2^7)
  int32_t analogTargetLevel;    // = RXX_BUFFER_LEN * 846805;       -22 dBfs
  int32_t startUpperLimit;      // = RXX_BUFFER_LEN * 1066064;      -21 dBfs
  int32_t startLowerLimit;      // = RXX_BUFFER_LEN * 672641;       -23 dBfs
  int32_t upperPrimaryLimit;    // = RXX_BUFFER_LEN * 1342095;      -20 dBfs
  int32_t lowerPrimaryLimit;    // = RXX_BUFFER_LEN * 534298;       -24 dBfs
  int32_t upperSecondaryLimit;  // = RXX_BUFFER_LEN * 2677832;      -17 dBfs
  int32_t lowerSecondaryLimit;  // = RXX_BUFFER_LEN * 267783;       -27 dBfs
  uint16_t targetIdx;           // Table index for corresponding target level
#ifdef MIC_LEVEL_FEEDBACK
  uint16_t targetIdxOffset;  // Table index offset for level compensation
#endif
  int16_t analogTarget;  // Digital reference level in ENV scale

  // Analog AGC specific variables
  int32_t filterState[8];  // For downsampling wb to nb
  int32_t upperLimit;      // Upper limit for mic energy
  int32_t lowerLimit;      // Lower limit for mic energy
  int32_t Rxx160w32;       // Average energy for one frame
  int32_t Rxx16_LPw32;     // Low pass filtered subframe energies
  int32_t Rxx160_LPw32;    // Low pass filtered frame energies
  int32_t Rxx16_LPw32Max;  // Keeps track of largest energy subframe
  int32_t Rxx16_vectorw32[RXX_BUFFER_LEN];  // Array with subframe energies
  int32_t Rxx16w32_array[2][5];  // Energy values of microphone signal
  int32_t env[2][10];            // Envelope values of subframes

  int16_t Rxx16pos;          // Current position in the Rxx16_vectorw32
  int16_t envSum;            // Filtered scaled envelope in subframes
  int16_t vadThreshold;      // Threshold for VAD decision
  int16_t inActive;          // Inactive time in milliseconds
  int16_t msTooLow;          // Milliseconds of speech at a too low level
  int16_t msTooHigh;         // Milliseconds of speech at a too high level
  int16_t changeToSlowMode;  // Change to slow mode after some time at target
  int16_t firstCall;         // First call to the process-function
  int16_t msZero;            // Milliseconds of zero input
  int16_t msecSpeechOuterChange;  // Min ms of speech between volume changes
  int16_t msecSpeechInnerChange;  // Min ms of speech between volume changes
  int16_t activeSpeech;           // Milliseconds of active speech
  int16_t muteGuardMs;            // Counter to prevent mute action
  int16_t inQueue;                // 10 ms batch indicator

  // Microphone level variables
  int32_t micRef;         // Remember ref. mic level for virtual mic
  uint16_t gainTableIdx;  // Current position in virtual gain table
  int32_t micGainIdx;     // Gain index of mic level to increase slowly
  int32_t micVol;         // Remember volume between frames
  int32_t maxLevel;       // Max possible vol level, incl dig gain
  int32_t maxAnalog;      // Maximum possible analog volume level
  int32_t maxInit;        // Initial value of "max"
  int32_t minLevel;       // Minimum possible volume level
  int32_t minOutput;      // Minimum output volume level
  int32_t zeroCtrlMax;    // Remember max gain => don't amp low input
  int32_t lastInMicLevel;

  int16_t scale;  // Scale factor for internal volume levels
#ifdef MIC_LEVEL_FEEDBACK
  int16_t numBlocksMicLvlSat;
  uint8_t micLvlSat;
#endif
  // Structs for VAD and digital_agc
  AgcVad vadMic;
  DigitalAgc digitalAgc;

#ifdef WEBRTC_AGC_DEBUG_DUMP
  FILE* fpt;
  FILE* agcLog;
  int32_t fcount;
#endif

  int16_t lowLevelSignal;
} LegacyAgc;

相關(guān)源代碼如下:

int GainControlImpl::AnalyzeCaptureAudio(AudioBuffer* audio) {
  if (!enabled_) {
    return AudioProcessing::kNoError;
  }

  RTC_DCHECK(num_proc_channels_);
  RTC_DCHECK_GE(AudioBuffer::kMaxSplitFrameLength,
                audio->num_frames_per_band());
  RTC_DCHECK_EQ(audio->num_channels(), *num_proc_channels_);
  RTC_DCHECK_LE(*num_proc_channels_, gain_controllers_.size());

  int16_t split_band_data[AudioBuffer::kMaxNumBands]
                         [AudioBuffer::kMaxSplitFrameLength];
  int16_t* split_bands[AudioBuffer::kMaxNumBands] = {
      split_band_data[0], split_band_data[1], split_band_data[2]};

  if (mode_ == kAdaptiveAnalog) {
    int capture_channel = 0;
    for (auto& gain_controller : gain_controllers_) {
      gain_controller->set_capture_level(analog_capture_level_);

      audio->ExportSplitChannelData(capture_channel, split_bands);

      int err =
          WebRtcAgc_AddMic(gain_controller->state(), split_bands,
                           audio->num_bands(), audio->num_frames_per_band());

      audio->ImportSplitChannelData(capture_channel, split_bands);

      if (err != AudioProcessing::kNoError) {
        return AudioProcessing::kUnspecifiedError;
      }
      ++capture_channel;
    }
  } else if (mode_ == kAdaptiveDigital) {
    int capture_channel = 0;
    for (auto& gain_controller : gain_controllers_) {
      int32_t capture_level_out = 0;

      audio->ExportSplitChannelData(capture_channel, split_bands);

      int err =
          WebRtcAgc_VirtualMic(gain_controller->state(), split_bands,
                               audio->num_bands(), audio->num_frames_per_band(),
                               analog_capture_level_, &capture_level_out);

      audio->ImportSplitChannelData(capture_channel, split_bands);

      gain_controller->set_capture_level(capture_level_out);

      if (err != AudioProcessing::kNoError) {
        return AudioProcessing::kUnspecifiedError;
      }
      ++capture_channel;
    }
  }

  return AudioProcessing::kNoError;
}

4. 主要接口

analog_agc.h包括模擬的agc結(jié)構(gòu)體聲明,而gain_control.h中的接口函數(shù)在analog_agc.c中實(shí)現(xiàn)。

WebRtcAgc_AddFarend  計(jì)算遠(yuǎn)端信號(hào)的語(yǔ)音活度VAD
WebRtcAgc_AddMic 計(jì)算麥克風(fēng)輸入的語(yǔ)音活度,對(duì)于非常小的信號(hào)會(huì)乘增益系數(shù)
WebRtcAgc_VirtualMic    用虛擬的麥克風(fēng)音量來(lái)調(diào)節(jié)幅度
WebRtcAgc_Process   vad核心處理
WebRtcAgc_set_config    設(shè)置VAD參數(shù)

在analog_agc.c還包括以下函數(shù):

WebRtcAgc_UpdateAgcThresholds   
WebRtcAgc_SaturationCtrl    
WebRtcAgc_ZeroCtrl  
WebRtcAgc_SpeakerInactiveCtrl   
WebRtcAgc_ExpCurve  
WebRtcAgc_ProcessAnalog 

digital_agc.h包括數(shù)字的agc結(jié)構(gòu)體聲明,Vad結(jié)構(gòu)聲明,而gain_control.h中的接口函數(shù)在analog_agc.c中實(shí)現(xiàn)。

WebRtcAgc_ProcessDigital    
WebRtcAgc_AddFarendToDigital    
WebRtcAgc_InitVad   
WebRtcAgc_ProcessVad    
WebRtcAgc_CalculateGainTable    

參考文章:
Webrtc AGC 算法(二)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 選擇題部分 1.(),只有在發(fā)生短路事故時(shí)或者在負(fù)荷電流較大時(shí),變流器中才會(huì)有足夠的二次電流作為繼電保護(hù)跳閘之用。...
    skystarwuwei閱讀 14,427評(píng)論 0 7
  • 第三部分 無(wú)線電系統(tǒng)原理 A0187[A] 無(wú)線電干擾中不屬于有害干擾的是:[A]符合國(guó)家或國(guó)際上規(guī)定的干擾允許...
    COLOR_KU閱讀 4,800評(píng)論 0 4
  • 看過(guò)這100個(gè)知識(shí)點(diǎn),模電其實(shí)也不難 2016-03-18 21ic電子網(wǎng) 模電想必是電子專業(yè)的學(xué)生頭疼的一門課程...
    岳壇閱讀 2,865評(píng)論 1 16
  • 從前,是那個(gè)傻傻輸了一局局小游戲, 在同伴的嘲笑中百思不得其解的時(shí)刻。 是那個(gè)見(jiàn)了陌生人怯生生無(wú)處可躲的時(shí)刻。 是...
    大搖大擺的魚(yú)閱讀 457評(píng)論 0 0
  • 最近各式各種事情太多,拖累的我三個(gè)周沒(méi)能參加頭馬了,電量嚴(yán)重不足,急需充電,趕走我的負(fù)能量。 每天上下班路上的聽(tīng)書(shū)...
    嬖?shī)?/span>閱讀 121評(píng)論 0 0

友情鏈接更多精彩內(nèi)容