blob: e981ad23e27dfbe0886d2b4c3823a5fc4376a5d9 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
bjornv@webrtc.orga496b032012-03-20 12:53:06 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
pbos@webrtc.orgaa30bb72013-05-27 09:49:58 +000011#include "webrtc/common_audio/vad/vad_sp.h"
bjornv@webrtc.org2111d3b2011-10-14 12:58:34 +000012
bjornv@webrtc.org226c5a12012-01-04 09:15:12 +000013#include <assert.h>
14
pbos@webrtc.orgaa30bb72013-05-27 09:49:58 +000015#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
16#include "webrtc/common_audio/vad/vad_core.h"
17#include "webrtc/typedefs.h"
bjornv@webrtc.org2111d3b2011-10-14 12:58:34 +000018
bjornv@webrtc.org226c5a12012-01-04 09:15:12 +000019// Allpass filter coefficients, upper and lower, in Q13.
20// Upper: 0.64, Lower: 0.17.
bjornv@webrtc.orga496b032012-03-20 12:53:06 +000021static const int16_t kAllPassCoefsQ13[2] = { 5243, 1392 }; // Q13.
22static const int16_t kSmoothingDown = 6553; // 0.2 in Q15.
23static const int16_t kSmoothingUp = 32439; // 0.99 in Q15.
niklase@google.com470e71d2011-07-07 08:21:25 +000024
bjornv@webrtc.org226c5a12012-01-04 09:15:12 +000025// TODO(bjornv): Move this function to vad_filterbank.c.
26// Downsampling filter based on splitting filter and allpass functions.
andrew@webrtc.org65f93382014-04-30 16:44:13 +000027void WebRtcVad_Downsampling(const int16_t* signal_in,
bjornv@webrtc.org226c5a12012-01-04 09:15:12 +000028 int16_t* signal_out,
29 int32_t* filter_state,
30 int in_length) {
31 int16_t tmp16_1 = 0, tmp16_2 = 0;
32 int32_t tmp32_1 = filter_state[0];
33 int32_t tmp32_2 = filter_state[1];
34 int n = 0;
35 int half_length = (in_length >> 1); // Downsampling by 2 gives half length.
niklase@google.com470e71d2011-07-07 08:21:25 +000036
bjornv@webrtc.org226c5a12012-01-04 09:15:12 +000037 // Filter coefficients in Q13, filter state in Q0.
38 for (n = 0; n < half_length; n++) {
39 // All-pass filtering upper branch.
40 tmp16_1 = (int16_t) ((tmp32_1 >> 1) +
41 WEBRTC_SPL_MUL_16_16_RSFT(kAllPassCoefsQ13[0], *signal_in, 14));
42 *signal_out = tmp16_1;
43 tmp32_1 = (int32_t) (*signal_in++) -
44 WEBRTC_SPL_MUL_16_16_RSFT(kAllPassCoefsQ13[0], tmp16_1, 12);
niklase@google.com470e71d2011-07-07 08:21:25 +000045
bjornv@webrtc.org226c5a12012-01-04 09:15:12 +000046 // All-pass filtering lower branch.
47 tmp16_2 = (int16_t) ((tmp32_2 >> 1) +
48 WEBRTC_SPL_MUL_16_16_RSFT(kAllPassCoefsQ13[1], *signal_in, 14));
49 *signal_out++ += tmp16_2;
50 tmp32_2 = (int32_t) (*signal_in++) -
51 WEBRTC_SPL_MUL_16_16_RSFT(kAllPassCoefsQ13[1], tmp16_2, 12);
52 }
53 // Store the filter states.
54 filter_state[0] = tmp32_1;
55 filter_state[1] = tmp32_2;
niklase@google.com470e71d2011-07-07 08:21:25 +000056}
57
bjornv@webrtc.org226c5a12012-01-04 09:15:12 +000058// Inserts |feature_value| into |low_value_vector|, if it is one of the 16
59// smallest values the last 100 frames. Then calculates and returns the median
60// of the five smallest values.
61int16_t WebRtcVad_FindMinimum(VadInstT* self,
62 int16_t feature_value,
63 int channel) {
64 int i = 0, j = 0;
65 int position = -1;
66 // Offset to beginning of the 16 minimum values in memory.
bjornv@webrtc.orgeec739f2012-06-11 07:57:57 +000067 const int offset = (channel << 4);
bjornv@webrtc.org226c5a12012-01-04 09:15:12 +000068 int16_t current_median = 1600;
69 int16_t alpha = 0;
70 int32_t tmp32 = 0;
71 // Pointer to memory for the 16 minimum values and the age of each value of
72 // the |channel|.
bjornv@webrtc.orgeec739f2012-06-11 07:57:57 +000073 int16_t* age = &self->index_vector[offset];
74 int16_t* smallest_values = &self->low_value_vector[offset];
niklase@google.com470e71d2011-07-07 08:21:25 +000075
bjornv@webrtc.orga496b032012-03-20 12:53:06 +000076 assert(channel < kNumChannels);
niklase@google.com470e71d2011-07-07 08:21:25 +000077
bjornv@webrtc.orgeec739f2012-06-11 07:57:57 +000078 // Each value in |smallest_values| is getting 1 loop older. Update |age|, and
79 // remove old values.
bjornv@webrtc.org226c5a12012-01-04 09:15:12 +000080 for (i = 0; i < 16; i++) {
bjornv@webrtc.orgeec739f2012-06-11 07:57:57 +000081 if (age[i] != 100) {
82 age[i]++;
bjornv@webrtc.org226c5a12012-01-04 09:15:12 +000083 } else {
bjornv@webrtc.orgeec739f2012-06-11 07:57:57 +000084 // Too old value. Remove from memory and shift larger values downwards.
bjornv@webrtc.org226c5a12012-01-04 09:15:12 +000085 for (j = i; j < 16; j++) {
bjornv@webrtc.orgeec739f2012-06-11 07:57:57 +000086 smallest_values[j] = smallest_values[j + 1];
87 age[j] = age[j + 1];
bjornv@webrtc.org226c5a12012-01-04 09:15:12 +000088 }
bjornv@webrtc.orgeec739f2012-06-11 07:57:57 +000089 age[15] = 101;
90 smallest_values[15] = 10000;
bjornv@webrtc.org226c5a12012-01-04 09:15:12 +000091 }
92 }
niklase@google.com470e71d2011-07-07 08:21:25 +000093
bjornv@webrtc.org226c5a12012-01-04 09:15:12 +000094 // Check if |feature_value| is smaller than any of the values in
bjornv@webrtc.orgeec739f2012-06-11 07:57:57 +000095 // |smallest_values|. If so, find the |position| where to insert the new value
96 // (|feature_value|).
97 if (feature_value < smallest_values[7]) {
98 if (feature_value < smallest_values[3]) {
99 if (feature_value < smallest_values[1]) {
100 if (feature_value < smallest_values[0]) {
bjornv@webrtc.org226c5a12012-01-04 09:15:12 +0000101 position = 0;
102 } else {
103 position = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000104 }
bjornv@webrtc.orgeec739f2012-06-11 07:57:57 +0000105 } else if (feature_value < smallest_values[2]) {
bjornv@webrtc.org226c5a12012-01-04 09:15:12 +0000106 position = 2;
107 } else {
108 position = 3;
109 }
bjornv@webrtc.orgeec739f2012-06-11 07:57:57 +0000110 } else if (feature_value < smallest_values[5]) {
111 if (feature_value < smallest_values[4]) {
bjornv@webrtc.org226c5a12012-01-04 09:15:12 +0000112 position = 4;
113 } else {
114 position = 5;
115 }
bjornv@webrtc.orgeec739f2012-06-11 07:57:57 +0000116 } else if (feature_value < smallest_values[6]) {
bjornv@webrtc.org226c5a12012-01-04 09:15:12 +0000117 position = 6;
118 } else {
119 position = 7;
niklase@google.com470e71d2011-07-07 08:21:25 +0000120 }
bjornv@webrtc.orgeec739f2012-06-11 07:57:57 +0000121 } else if (feature_value < smallest_values[15]) {
122 if (feature_value < smallest_values[11]) {
123 if (feature_value < smallest_values[9]) {
124 if (feature_value < smallest_values[8]) {
bjornv@webrtc.org226c5a12012-01-04 09:15:12 +0000125 position = 8;
126 } else {
127 position = 9;
niklase@google.com470e71d2011-07-07 08:21:25 +0000128 }
bjornv@webrtc.orgeec739f2012-06-11 07:57:57 +0000129 } else if (feature_value < smallest_values[10]) {
bjornv@webrtc.org226c5a12012-01-04 09:15:12 +0000130 position = 10;
131 } else {
132 position = 11;
133 }
bjornv@webrtc.orgeec739f2012-06-11 07:57:57 +0000134 } else if (feature_value < smallest_values[13]) {
135 if (feature_value < smallest_values[12]) {
bjornv@webrtc.org226c5a12012-01-04 09:15:12 +0000136 position = 12;
137 } else {
138 position = 13;
139 }
bjornv@webrtc.orgeec739f2012-06-11 07:57:57 +0000140 } else if (feature_value < smallest_values[14]) {
bjornv@webrtc.org226c5a12012-01-04 09:15:12 +0000141 position = 14;
142 } else {
143 position = 15;
niklase@google.com470e71d2011-07-07 08:21:25 +0000144 }
bjornv@webrtc.org226c5a12012-01-04 09:15:12 +0000145 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000146
bjornv@webrtc.orgeec739f2012-06-11 07:57:57 +0000147 // If we have detected a new small value, insert it at the correct position
148 // and shift larger values up.
bjornv@webrtc.org226c5a12012-01-04 09:15:12 +0000149 if (position > -1) {
150 for (i = 15; i > position; i--) {
bjornv@webrtc.orgeec739f2012-06-11 07:57:57 +0000151 smallest_values[i] = smallest_values[i - 1];
152 age[i] = age[i - 1];
niklase@google.com470e71d2011-07-07 08:21:25 +0000153 }
bjornv@webrtc.orgeec739f2012-06-11 07:57:57 +0000154 smallest_values[position] = feature_value;
155 age[position] = 1;
bjornv@webrtc.org226c5a12012-01-04 09:15:12 +0000156 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000157
bjornv@webrtc.org226c5a12012-01-04 09:15:12 +0000158 // Get |current_median|.
159 if (self->frame_counter > 2) {
bjornv@webrtc.orgeec739f2012-06-11 07:57:57 +0000160 current_median = smallest_values[2];
bjornv@webrtc.org226c5a12012-01-04 09:15:12 +0000161 } else if (self->frame_counter > 0) {
bjornv@webrtc.orgeec739f2012-06-11 07:57:57 +0000162 current_median = smallest_values[0];
bjornv@webrtc.org226c5a12012-01-04 09:15:12 +0000163 }
164
165 // Smooth the median value.
166 if (self->frame_counter > 0) {
167 if (current_median < self->mean_value[channel]) {
bjornv@webrtc.orga496b032012-03-20 12:53:06 +0000168 alpha = kSmoothingDown; // 0.2 in Q15.
bjornv@webrtc.org226c5a12012-01-04 09:15:12 +0000169 } else {
bjornv@webrtc.orga496b032012-03-20 12:53:06 +0000170 alpha = kSmoothingUp; // 0.99 in Q15.
niklase@google.com470e71d2011-07-07 08:21:25 +0000171 }
bjornv@webrtc.org226c5a12012-01-04 09:15:12 +0000172 }
173 tmp32 = WEBRTC_SPL_MUL_16_16(alpha + 1, self->mean_value[channel]);
174 tmp32 += WEBRTC_SPL_MUL_16_16(WEBRTC_SPL_WORD16_MAX - alpha, current_median);
175 tmp32 += 16384;
176 self->mean_value[channel] = (int16_t) (tmp32 >> 15);
niklase@google.com470e71d2011-07-07 08:21:25 +0000177
bjornv@webrtc.org226c5a12012-01-04 09:15:12 +0000178 return self->mean_value[channel];
niklase@google.com470e71d2011-07-07 08:21:25 +0000179}