blob: b394074bd3e2ea136e6cee62538eb8e17b9b28bf [file] [log] [blame]
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001/*
2 * Copyright (c) 2012 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
11#include "webrtc/modules/audio_processing/agc/pitch_internal.h"
12
13#include <cmath>
14
15// A 4-to-3 linear interpolation.
16// The interpolation constants are derived as following:
17// Input pitch parameters are updated every 7.5 ms. Within a 30-ms interval
18// we are interested in pitch parameters of 0-5 ms, 10-15ms and 20-25ms. This is
19// like interpolating 4-to-6 and keep the odd samples.
20// The reason behind this is that LPC coefficients are computed for the first
21// half of each 10ms interval.
22static void PitchInterpolation(double old_val, const double* in, double* out) {
23 out[0] = 1. / 6. * old_val + 5. / 6. * in[0];
24 out[1] = 5. / 6. * in[1] + 1. / 6. * in[2];
25 out[2] = 0.5 * in[2] + 0.5 * in[3];
26}
27
28
29void GetSubframesPitchParameters(int sampling_rate_hz,
30 double* gains,
31 double* lags,
32 int num_in_frames,
33 int num_out_frames,
34 double* log_old_gain,
35 double* old_lag,
36 double* log_pitch_gain,
37 double* pitch_lag_hz) {
38 // Gain interpolation is in log-domain, also returned in log-domain.
39 for (int n = 0; n < num_in_frames; n++)
40 gains[n] = log(gains[n] + 1e-12);
41
42 // Interpolate lags and gains.
43 PitchInterpolation(*log_old_gain, gains, log_pitch_gain);
44 *log_old_gain = gains[num_in_frames - 1];
45 PitchInterpolation(*old_lag, lags, pitch_lag_hz);
46 *old_lag = lags[num_in_frames - 1];
47
48 // Convert pitch-lags to Hertz.
49 for (int n = 0; n < num_out_frames; n++) {
50 pitch_lag_hz[n] = (sampling_rate_hz) / (pitch_lag_hz[n]);
51 }
52}