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