Home | History | Annotate | Download | only in source
      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 
     12 /*
     13  * This file includes the implementation of the internal VAD call
     14  * WebRtcVad_GaussianProbability. For function description, see vad_gmm.h.
     15  */
     16 
     17 #include "vad_gmm.h"
     18 #include "signal_processing_library.h"
     19 #include "vad_const.h"
     20 
     21 WebRtc_Word32 WebRtcVad_GaussianProbability(WebRtc_Word16 in_sample,
     22                                             WebRtc_Word16 mean,
     23                                             WebRtc_Word16 std,
     24                                             WebRtc_Word16 *delta)
     25 {
     26     WebRtc_Word16 tmp16, tmpDiv, tmpDiv2, expVal, tmp16_1, tmp16_2;
     27     WebRtc_Word32 tmp32, y32;
     28 
     29     // Calculate tmpDiv=1/std, in Q10
     30     tmp32 = (WebRtc_Word32)WEBRTC_SPL_RSHIFT_W16(std,1) + (WebRtc_Word32)131072; // 1 in Q17
     31     tmpDiv = (WebRtc_Word16)WebRtcSpl_DivW32W16(tmp32, std); // Q17/Q7 = Q10
     32 
     33     // Calculate tmpDiv2=1/std^2, in Q14
     34     tmp16 = WEBRTC_SPL_RSHIFT_W16(tmpDiv, 2); // From Q10 to Q8
     35     tmpDiv2 = (WebRtc_Word16)WEBRTC_SPL_MUL_16_16_RSFT(tmp16, tmp16, 2); // (Q8 * Q8)>>2 = Q14
     36 
     37     tmp16 = WEBRTC_SPL_LSHIFT_W16(in_sample, 3); // Q7
     38     tmp16 = tmp16 - mean; // Q7 - Q7 = Q7
     39 
     40     // To be used later, when updating noise/speech model
     41     // delta = (x-m)/std^2, in Q11
     42     *delta = (WebRtc_Word16)WEBRTC_SPL_MUL_16_16_RSFT(tmpDiv2, tmp16, 10); //(Q14*Q7)>>10 = Q11
     43 
     44     // Calculate tmp32=(x-m)^2/(2*std^2), in Q10
     45     tmp32 = (WebRtc_Word32)WEBRTC_SPL_MUL_16_16_RSFT(*delta, tmp16, 9); // One shift for /2
     46 
     47     // Calculate expVal ~= exp(-(x-m)^2/(2*std^2)) ~= exp2(-log2(exp(1))*tmp32)
     48     if (tmp32 < kCompVar)
     49     {
     50         // Calculate tmp16 = log2(exp(1))*tmp32 , in Q10
     51         tmp16 = (WebRtc_Word16)WEBRTC_SPL_MUL_16_16_RSFT((WebRtc_Word16)tmp32,
     52                                                          kLog10Const, 12);
     53         tmp16 = -tmp16;
     54         tmp16_2 = (WebRtc_Word16)(0x0400 | (tmp16 & 0x03FF));
     55         tmp16_1 = (WebRtc_Word16)(tmp16 ^ 0xFFFF);
     56         tmp16 = (WebRtc_Word16)WEBRTC_SPL_RSHIFT_W16(tmp16_1, 10);
     57         tmp16 += 1;
     58         // Calculate expVal=log2(-tmp32), in Q10
     59         expVal = (WebRtc_Word16)WEBRTC_SPL_RSHIFT_W32((WebRtc_Word32)tmp16_2, tmp16);
     60 
     61     } else
     62     {
     63         expVal = 0;
     64     }
     65 
     66     // Calculate y32=(1/std)*exp(-(x-m)^2/(2*std^2)), in Q20
     67     y32 = WEBRTC_SPL_MUL_16_16(tmpDiv, expVal); // Q10 * Q10 = Q20
     68 
     69     return y32; // Q20
     70 }
     71