1/*
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/vad/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
28void GetSubframesPitchParameters(int sampling_rate_hz,
29                                 double* gains,
30                                 double* lags,
31                                 int num_in_frames,
32                                 int num_out_frames,
33                                 double* log_old_gain,
34                                 double* old_lag,
35                                 double* log_pitch_gain,
36                                 double* pitch_lag_hz) {
37  // Gain interpolation is in log-domain, also returned in log-domain.
38  for (int n = 0; n < num_in_frames; n++)
39    gains[n] = log(gains[n] + 1e-12);
40
41  // Interpolate lags and gains.
42  PitchInterpolation(*log_old_gain, gains, log_pitch_gain);
43  *log_old_gain = gains[num_in_frames - 1];
44  PitchInterpolation(*old_lag, lags, pitch_lag_hz);
45  *old_lag = lags[num_in_frames - 1];
46
47  // Convert pitch-lags to Hertz.
48  for (int n = 0; n < num_out_frames; n++) {
49    pitch_lag_hz[n] = (sampling_rate_hz) / (pitch_lag_hz[n]);
50  }
51}
52