1 /*
2  *  Copyright (c) 2011 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 "vad_sp.h"
12 
13 #include <assert.h>
14 
15 #include "signal_processing_library.h"
16 #include "typedefs.h"
17 #include "vad_defines.h"
18 
19 // Allpass filter coefficients, upper and lower, in Q13.
20 // Upper: 0.64, Lower: 0.17.
21 static const int16_t kAllPassCoefsQ13[2] = { 5243, 1392 };  // Q13
22 
23 // TODO(bjornv): Move this function to vad_filterbank.c.
24 // Downsampling filter based on splitting filter and allpass functions.
WebRtcVad_Downsampling(int16_t * signal_in,int16_t * signal_out,int32_t * filter_state,int in_length)25 void WebRtcVad_Downsampling(int16_t* signal_in,
26                             int16_t* signal_out,
27                             int32_t* filter_state,
28                             int in_length) {
29   int16_t tmp16_1 = 0, tmp16_2 = 0;
30   int32_t tmp32_1 = filter_state[0];
31   int32_t tmp32_2 = filter_state[1];
32   int n = 0;
33   int half_length = (in_length >> 1);  // Downsampling by 2 gives half length.
34 
35   // Filter coefficients in Q13, filter state in Q0.
36   for (n = 0; n < half_length; n++) {
37     // All-pass filtering upper branch.
38     tmp16_1 = (int16_t) ((tmp32_1 >> 1) +
39         WEBRTC_SPL_MUL_16_16_RSFT(kAllPassCoefsQ13[0], *signal_in, 14));
40     *signal_out = tmp16_1;
41     tmp32_1 = (int32_t) (*signal_in++) -
42         WEBRTC_SPL_MUL_16_16_RSFT(kAllPassCoefsQ13[0], tmp16_1, 12);
43 
44     // All-pass filtering lower branch.
45     tmp16_2 = (int16_t) ((tmp32_2 >> 1) +
46         WEBRTC_SPL_MUL_16_16_RSFT(kAllPassCoefsQ13[1], *signal_in, 14));
47     *signal_out++ += tmp16_2;
48     tmp32_2 = (int32_t) (*signal_in++) -
49         WEBRTC_SPL_MUL_16_16_RSFT(kAllPassCoefsQ13[1], tmp16_2, 12);
50   }
51   // Store the filter states.
52   filter_state[0] = tmp32_1;
53   filter_state[1] = tmp32_2;
54 }
55 
56 // Inserts |feature_value| into |low_value_vector|, if it is one of the 16
57 // smallest values the last 100 frames. Then calculates and returns the median
58 // of the five smallest values.
WebRtcVad_FindMinimum(VadInstT * self,int16_t feature_value,int channel)59 int16_t WebRtcVad_FindMinimum(VadInstT* self,
60                               int16_t feature_value,
61                               int channel) {
62   int i = 0, j = 0;
63   int position = -1;
64   // Offset to beginning of the 16 minimum values in memory.
65   int offset = (channel << 4);
66   int16_t current_median = 1600;
67   int16_t alpha = 0;
68   int32_t tmp32 = 0;
69   // Pointer to memory for the 16 minimum values and the age of each value of
70   // the |channel|.
71   int16_t* age_ptr = &self->index_vector[offset];
72   int16_t* value_ptr = &self->low_value_vector[offset];
73   int16_t *p1, *p2, *p3;
74 
75   assert(channel < NUM_CHANNELS);
76 
77   // Each value in |low_value_vector| is getting 1 loop older.
78   // Update age of each value in |age_ptr|, and remove old values.
79   for (i = 0; i < 16; i++) {
80     p3 = age_ptr + i;
81     if (*p3 != 100) {
82       *p3 += 1;
83     } else {
84       p1 = value_ptr + i + 1;
85       p2 = p3 + 1;
86       for (j = i; j < 16; j++) {
87         *(value_ptr + j) = *p1++;
88         *(age_ptr + j) = *p2++;
89       }
90       *(age_ptr + 15) = 101;
91       *(value_ptr + 15) = 10000;
92     }
93   }
94 
95   // Check if |feature_value| is smaller than any of the values in
96   // |low_value_vector|. If so, find the |position| where to insert the new
97   // value.
98   if (feature_value < *(value_ptr + 7)) {
99     if (feature_value < *(value_ptr + 3)) {
100       if (feature_value < *(value_ptr + 1)) {
101         if (feature_value < *value_ptr) {
102           position = 0;
103         } else {
104           position = 1;
105         }
106       } else if (feature_value < *(value_ptr + 2)) {
107         position = 2;
108       } else {
109         position = 3;
110       }
111     } else if (feature_value < *(value_ptr + 5)) {
112       if (feature_value < *(value_ptr + 4)) {
113         position = 4;
114       } else {
115         position = 5;
116       }
117     } else if (feature_value < *(value_ptr + 6)) {
118       position = 6;
119     } else {
120       position = 7;
121     }
122   } else if (feature_value < *(value_ptr + 15)) {
123     if (feature_value < *(value_ptr + 11)) {
124       if (feature_value < *(value_ptr + 9)) {
125         if (feature_value < *(value_ptr + 8)) {
126           position = 8;
127         } else {
128           position = 9;
129         }
130       } else if (feature_value < *(value_ptr + 10)) {
131         position = 10;
132       } else {
133         position = 11;
134       }
135     } else if (feature_value < *(value_ptr + 13)) {
136       if (feature_value < *(value_ptr + 12)) {
137         position = 12;
138       } else {
139         position = 13;
140       }
141     } else if (feature_value < *(value_ptr + 14)) {
142       position = 14;
143     } else {
144       position = 15;
145     }
146   }
147 
148   // If we have a new small value, put it in the correct position and shift
149   // larger values up.
150   if (position > -1) {
151     for (i = 15; i > position; i--) {
152       j = i - 1;
153       *(value_ptr + i) = *(value_ptr + j);
154       *(age_ptr + i) = *(age_ptr + j);
155     }
156     *(value_ptr + position) = feature_value;
157     *(age_ptr + position) = 1;
158   }
159 
160   // Get |current_median|.
161   if (self->frame_counter > 2) {
162     current_median = *(value_ptr + 2);
163   } else if (self->frame_counter > 0) {
164     current_median = *value_ptr;
165   }
166 
167   // Smooth the median value.
168   if (self->frame_counter > 0) {
169     if (current_median < self->mean_value[channel]) {
170       alpha = (int16_t) ALPHA1;  // 0.2 in Q15.
171     } else {
172       alpha = (int16_t) ALPHA2;  // 0.99 in Q15.
173     }
174   }
175   tmp32 = WEBRTC_SPL_MUL_16_16(alpha + 1, self->mean_value[channel]);
176   tmp32 += WEBRTC_SPL_MUL_16_16(WEBRTC_SPL_WORD16_MAX - alpha, current_median);
177   tmp32 += 16384;
178   self->mean_value[channel] = (int16_t) (tmp32 >> 15);
179 
180   return self->mean_value[channel];
181 }
182