@@ -172,7 +172,7 @@ int ComputePitchPeriod24kHz(
172
172
// Auto-correlation energy normalized by frame energy.
173
173
const float numerator =
174
174
auto_correlation[inverted_lag] * auto_correlation[inverted_lag];
175
- const float denominator = y_energy[kMaxPitch24kHz - inverted_lag];
175
+ const float denominator = y_energy[inverted_lag];
176
176
// Compare numerator/denominator ratios without using divisions.
177
177
if (numerator * best_denominator > best_numerator * denominator) {
178
178
best_inverted_lag = inverted_lag;
@@ -256,19 +256,19 @@ void Decimate2x(rtc::ArrayView<const float, kBufSize24kHz> src,
256
256
257
257
void ComputeSlidingFrameSquareEnergies24kHz (
258
258
rtc::ArrayView<const float , kBufSize24kHz > pitch_buffer,
259
- rtc::ArrayView<float , kRefineNumLags24kHz > yy_values) {
260
- float yy = ComputeAutoCorrelation (kMaxPitch24kHz , pitch_buffer);
261
- yy_values[0 ] = yy;
262
- static_assert (kMaxPitch24kHz - (kRefineNumLags24kHz - 1 ) >= 0 , " " );
259
+ rtc::ArrayView<float , kRefineNumLags24kHz > y_energy) {
260
+ float yy = std::inner_product (pitch_buffer.begin (),
261
+ pitch_buffer.begin () + kFrameSize20ms24kHz ,
262
+ pitch_buffer.begin (), 0 .f );
263
+ y_energy[0 ] = yy;
263
264
static_assert (kMaxPitch24kHz - 1 + kFrameSize20ms24kHz < kBufSize24kHz , " " );
264
- for (int lag = 1 ; lag < kRefineNumLags24kHz ; ++lag) {
265
- const int inverted_lag = kMaxPitch24kHz - lag;
266
- const float y_old = pitch_buffer[inverted_lag + kFrameSize20ms24kHz ];
267
- const float y_new = pitch_buffer[inverted_lag];
268
- yy -= y_old * y_old;
269
- yy += y_new * y_new;
270
- yy = std::max (0 .f , yy);
271
- yy_values[lag] = yy;
265
+ static_assert (kMaxPitch24kHz < kRefineNumLags24kHz , " " );
266
+ for (int inverted_lag = 0 ; inverted_lag < kMaxPitch24kHz ; ++inverted_lag) {
267
+ yy -= pitch_buffer[inverted_lag] * pitch_buffer[inverted_lag];
268
+ yy += pitch_buffer[inverted_lag + kFrameSize20ms24kHz ] *
269
+ pitch_buffer[inverted_lag + kFrameSize20ms24kHz ];
270
+ yy = std::max (1 .f , yy);
271
+ y_energy[inverted_lag + 1 ] = yy;
272
272
}
273
273
}
274
274
@@ -382,7 +382,7 @@ PitchInfo ComputeExtendedPitchPeriod48kHz(
382
382
float y_energy; // Energy of the sliding frame `y`.
383
383
};
384
384
385
- const float x_energy = y_energy[0 ];
385
+ const float x_energy = y_energy[kMaxPitch24kHz ];
386
386
const auto pitch_strength = [x_energy](float xy, float y_energy) {
387
387
RTC_DCHECK_GE (x_energy * y_energy, 0 .f );
388
388
return xy / std::sqrt (1 .f + x_energy * y_energy);
@@ -394,7 +394,7 @@ PitchInfo ComputeExtendedPitchPeriod48kHz(
394
394
std::min (initial_pitch_period_48kHz / 2 , kMaxPitch24kHz - 1 );
395
395
best_pitch.xy =
396
396
ComputeAutoCorrelation (kMaxPitch24kHz - best_pitch.period , pitch_buffer);
397
- best_pitch.y_energy = y_energy[best_pitch.period ];
397
+ best_pitch.y_energy = y_energy[kMaxPitch24kHz - best_pitch.period ];
398
398
best_pitch.strength = pitch_strength (best_pitch.xy , best_pitch.y_energy );
399
399
// Keep a copy of the initial pitch candidate.
400
400
const PitchInfo initial_pitch{best_pitch.period , best_pitch.strength };
@@ -435,8 +435,9 @@ PitchInfo ComputeExtendedPitchPeriod48kHz(
435
435
const float xy_secondary_period = ComputeAutoCorrelation (
436
436
kMaxPitch24kHz - dual_alternative_period, pitch_buffer);
437
437
const float xy = 0 .5f * (xy_primary_period + xy_secondary_period);
438
- const float yy = 0 .5f * (y_energy[alternative_pitch.period ] +
439
- y_energy[dual_alternative_period]);
438
+ const float yy =
439
+ 0 .5f * (y_energy[kMaxPitch24kHz - alternative_pitch.period ] +
440
+ y_energy[kMaxPitch24kHz - dual_alternative_period]);
440
441
alternative_pitch.strength = pitch_strength (xy, yy);
441
442
442
443
// Maybe update best period.
0 commit comments