Webrtc AGC 算法原理初识(一)

1. AGC 初识

自动增益控制电路的作用是:当输入信号电压变化很大时,保持接收机输出电压恒定或基本不变。具体地说,当输入信号很弱时,接收机的增益大,自动增益控制电路不起作用;当输入信号很强时,自动增益控制电路进行控制,使接收机的增益减小。这样,当接收信号强度变化时,接收机的输出端的电压或功率基本不变或保持恒定。因此对AGC电路的要求是:在输入信号较小时,AGC电路不起作用,只有当输入信号增大到一定程度后,AGC电路才起控制作用,使增益随输入信号的增大而减少。
为实现上述要求,必须有一个能随外来信号强弱而变化的控制电压或电流信号,利用这个信号对放大器的增益自动进行控制。由上述分析可知,调幅中频信号经幅度检波后,在它的输出中除音频信号外,还含有直流分量。直流分量大小与中频载波的振幅成正比,也即与外来高频信号成正比。因此,可将检波器输出的直流分量作为AGC控制信号。

AGC电路工作原理:可以分为增益受控放大电路和控制电压形成电路。增益受控放大电路位于正向放大通路,其增益随控制电压U0而改变。控制电压形成电路的基本部件是AGC整流器和低通平滑滤波器,有时也包含门电路和直流放大器等部件。

放大器及AGC电路.png

2. webrtc 的 AGC算法

AGC是自动增益补偿功能(Automatic Gain Control),AGC可以自动调麦克风的收音量,使与会者收到一定的音量水平,不会因发言者与麦克风的距离改变时,声音有忽大忽小声的缺点。
webbrtc中的结构如下:

AGC算法目录结构.png
包装的头文件目录.png

说明:gain_control.h是包装的头文件,在apm里头gain_control_impl调用。主要包括了接口定义函数和参数配置。

3. 主要配置

include/gain_control.h 里面定义了agc的三种模式,kAdaptiveAnalog、kAdaptiveDigital和kFixedDigital。其中,kAdaptiveAnalog带有模拟音量调节的功能。kAdaptiveDigital是可变增益agc,但是不调节系统音量。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用于调节agc的动态范围

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;

相关源代码如下:

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结构体声明,而gain_control.h中的接口函数在analog_agc.c中实现。

WebRtcAgc_AddFarend  计算远端信号的语音活度VAD
WebRtcAgc_AddMic 计算麦克风输入的语音活度,对于非常小的信号会乘增益系数
WebRtcAgc_VirtualMic    用虚拟的麦克风音量来调节幅度
WebRtcAgc_Process   vad核心处理
WebRtcAgc_set_config    设置VAD参数

在analog_agc.c还包括以下函数:

WebRtcAgc_UpdateAgcThresholds   
WebRtcAgc_SaturationCtrl    
WebRtcAgc_ZeroCtrl  
WebRtcAgc_SpeakerInactiveCtrl   
WebRtcAgc_ExpCurve  
WebRtcAgc_ProcessAnalog 

digital_agc.h包括数字的agc结构体声明,Vad结构声明,而gain_control.h中的接口函数在analog_agc.c中实现。

WebRtcAgc_ProcessDigital    
WebRtcAgc_AddFarendToDigital    
WebRtcAgc_InitVad   
WebRtcAgc_ProcessVad    
WebRtcAgc_CalculateGainTable    

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,458评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,454评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,171评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,062评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,440评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,661评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,906评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,609评论 0 200
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,379评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,600评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,085评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,409评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,072评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,088评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,860评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,704评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,608评论 2 270

推荐阅读更多精彩内容

  • 选择题部分 1.(),只有在发生短路事故时或者在负荷电流较大时,变流器中才会有足够的二次电流作为继电保护跳闸之用。...
    skystarwuwei阅读 12,250评论 0 7
  • 第三部分 无线电系统原理 A0187[A] 无线电干扰中不属于有害干扰的是:[A]符合国家或国际上规定的干扰允许...
    COLOR_KU阅读 3,817评论 0 4
  • 看过这100个知识点,模电其实也不难 2016-03-18 21ic电子网 模电想必是电子专业的学生头疼的一门课程...
    岳坛阅读 2,633评论 1 16
  • 从前,是那个傻傻输了一局局小游戏, 在同伴的嘲笑中百思不得其解的时刻。 是那个见了陌生人怯生生无处可躲的时刻。 是...
    大摇大摆的鱼阅读 365评论 0 0
  • 最近各式各种事情太多,拖累的我三个周没能参加头马了,电量严重不足,急需充电,赶走我的负能量。 每天上下班路上的听书...
    嬖姮阅读 73评论 0 0