webRTC AEC 归一化误差信号

因为误差信号的大小变化差异较大,不便于设置门限阈值,因此将误差信号e对远端信号x进行归一化,便于对与阈值比较。

static void ScaleErrorSignalSSE2(AecCore* aec, float ef[2][PART_LEN1]) {
	// extern __m128 _mm_set_ps1(float _W);
	//返回一个__m128的寄存器,Sets the four single-precision, floating-point values to w  
	//r0=r1=r2=r3=_W ,1e-10应该可能是为了避免0的出现
  const __m128 k1e_10f = _mm_set1_ps(1e-10f);
  const __m128 kMu = aec->extended_filter_enabled ? _mm_set1_ps(kExtendedMu)
                                                  : _mm_set1_ps(aec->normal_mu);
  const __m128 kThresh = aec->extended_filter_enabled
                             ? _mm_set1_ps(kExtendedErrorThreshold)
                             : _mm_set1_ps(aec->normal_error_threshold);

  int i;
  // vectorized code (four at once),向量计算
  for (i = 0; i + 3 < PART_LEN1; i += 4) {
	  // 载入数据远场功率、误差频域实部、误差频域虚部
    const __m128 xPow = _mm_loadu_ps(&aec->xPow[i]);
    const __m128 ef_re_base = _mm_loadu_ps(&ef[0][i]);
    const __m128 ef_im_base = _mm_loadu_ps(&ef[1][i]);

	// 远场功率加1e-10,避免出现0做除数
    const __m128 xPowPlus = _mm_add_ps(xPow, k1e_10f);
	// 误差归一化处理,误差频域实部、误差频域虚部除以远场功率
    __m128 ef_re = _mm_div_ps(ef_re_base, xPowPlus);
    __m128 ef_im = _mm_div_ps(ef_im_base, xPowPlus);

	// 归一化误差信号求平方
    const __m128 ef_re2 = _mm_mul_ps(ef_re, ef_re);
    const __m128 ef_im2 = _mm_mul_ps(ef_im, ef_im);
    const __m128 ef_sum2 = _mm_add_ps(ef_re2, ef_im2);

	// 误差功率开方求绝对值
    const __m128 absEf = _mm_sqrt_ps(ef_sum2);
	// 给absEf设置一个下限kThresh
    const __m128 bigger = _mm_cmpgt_ps(absEf, kThresh);

	// 误差绝对值+1e-10,避免0做除数
    __m128 absEfPlus = _mm_add_ps(absEf, k1e_10f);

	// 误差绝对值/下限(上面虽然设置了下限,但是并没有对absEf进行更新,因此>1和<1的情况都有可能存在)
    const __m128 absEfInv = _mm_div_ps(kThresh, absEfPlus);
    __m128 ef_re_if = _mm_mul_ps(ef_re, absEfInv);
    __m128 ef_im_if = _mm_mul_ps(ef_im, absEfInv);
    ef_re_if = _mm_and_ps(bigger, ef_re_if);
    ef_im_if = _mm_and_ps(bigger, ef_im_if);
	// 与非逻辑判断
    ef_re = _mm_andnot_ps(bigger, ef_re);
    ef_im = _mm_andnot_ps(bigger, ef_im);
	// 或逻辑判断
    ef_re = _mm_or_ps(ef_re, ef_re_if);
    ef_im = _mm_or_ps(ef_im, ef_im_if);

	// 计算逻辑判断结果与kMu的乘积
    ef_re = _mm_mul_ps(ef_re, kMu);
    ef_im = _mm_mul_ps(ef_im, kMu);

	// 将误差实部、虚部处理结果分别存储到ef中
    _mm_storeu_ps(&ef[0][i], ef_re);
    _mm_storeu_ps(&ef[1][i], ef_im);
  }
  // scalar code for the remaining items.
  {
    const float mu =
        aec->extended_filter_enabled ? kExtendedMu : aec->normal_mu;
    const float error_threshold = aec->extended_filter_enabled
                                      ? kExtendedErrorThreshold
                                      : aec->normal_error_threshold;
    for (; i < (PART_LEN1); i++) {
      float abs_ef;
	  // 误差对远场信号归一化,求绝对值
      ef[0][i] /= (aec->xPow[i] + 1e-10f);
      ef[1][i] /= (aec->xPow[i] + 1e-10f);
      abs_ef = sqrtf(ef[0][i] * ef[0][i] + ef[1][i] * ef[1][i]);

	  // 如果误差超过了误差阈值
      if (abs_ef > error_threshold) {
		  // 更新误差 = 阈值/误差;
        abs_ef = error_threshold / (abs_ef + 1e-10f);
        ef[0][i] *= abs_ef;
        ef[1][i] *= abs_ef;
      }

      // 误差乘以步进因子
      ef[0][i] *= mu;
      ef[1][i] *= mu;
    }
  }
}

 

你可能感兴趣的:(C++,DSP,语音信号处理,webRTC)