1470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com/* 2470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. 3470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com * 4470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com * Use of this source code is governed by a BSD-style license 5470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com * that can be found in the LICENSE file in the root of the source 6470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com * tree. An additional intellectual property rights grant can be found 7470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com * in the file PATENTS. All contributing project authors may 8470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com * be found in the AUTHORS file in the root of the source tree. 9470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com */ 10470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com 11aa30bb7ef5b02c9026dc2c036a0bed9999ae4cf2pbos@webrtc.org#include "webrtc/common_audio/vad/vad_gmm.h" 122111d3b0b068f9737792953c67fac197e23a29e3bjornv@webrtc.org 13aa30bb7ef5b02c9026dc2c036a0bed9999ae4cf2pbos@webrtc.org#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h" 14aa30bb7ef5b02c9026dc2c036a0bed9999ae4cf2pbos@webrtc.org#include "webrtc/typedefs.h" 152111d3b0b068f9737792953c67fac197e23a29e3bjornv@webrtc.org 16c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.orgstatic const int32_t kCompVar = 22005; 17c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.orgstatic const int16_t kLog2Exp = 5909; // log2(exp(1)) in Q12. 18470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com 19c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org// For a normal distribution, the probability of |input| is calculated and 20c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org// returned (in Q20). The formula for normal distributed probability is 21c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org// 22c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org// 1 / s * exp(-(x - m)^2 / (2 * s^2)) 23c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org// 24c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org// where the parameters are given in the following Q domains: 25c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org// m = |mean| (Q7) 26c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org// s = |std| (Q7) 27c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org// x = |input| (Q4) 28c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org// in addition to the probability we output |delta| (in Q11) used when updating 29c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org// the noise/speech model. 30c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.orgint32_t WebRtcVad_GaussianProbability(int16_t input, 31c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org int16_t mean, 32c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org int16_t std, 33c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org int16_t* delta) { 34c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org int16_t tmp16, inv_std, inv_std2, exp_value = 0; 35c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org int32_t tmp32; 36470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com 37c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org // Calculate |inv_std| = 1 / s, in Q10. 38c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org // 131072 = 1 in Q17, and (|std| >> 1) is for rounding instead of truncation. 39c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org // Q-domain: Q17 / Q7 = Q10. 40c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org tmp32 = (int32_t) 131072 + (int32_t) (std >> 1); 41c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org inv_std = (int16_t) WebRtcSpl_DivW32W16(tmp32, std); 42470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com 43c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org // Calculate |inv_std2| = 1 / s^2, in Q14. 44c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org tmp16 = (inv_std >> 2); // Q10 -> Q8. 45c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org // Q-domain: (Q8 * Q8) >> 2 = Q14. 463fbf99c44a30f09d4e3402e192067d053ced5c55Bjorn Volcker inv_std2 = (int16_t)((tmp16 * tmp16) >> 2); 47c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org // TODO(bjornv): Investigate if changing to 483fbf99c44a30f09d4e3402e192067d053ced5c55Bjorn Volcker // inv_std2 = (int16_t)((inv_std * inv_std) >> 6); 49c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org // gives better accuracy. 50470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com 51c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org tmp16 = (input << 3); // Q4 -> Q7 52c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org tmp16 = tmp16 - mean; // Q7 - Q7 = Q7 53470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com 54c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org // To be used later, when updating noise/speech model. 55c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org // |delta| = (x - m) / s^2, in Q11. 56c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org // Q-domain: (Q14 * Q7) >> 10 = Q11. 573fbf99c44a30f09d4e3402e192067d053ced5c55Bjorn Volcker *delta = (int16_t)((inv_std2 * tmp16) >> 10); 58470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com 59c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org // Calculate the exponent |tmp32| = (x - m)^2 / (2 * s^2), in Q10. Replacing 60c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org // division by two with one shift. 61c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org // Q-domain: (Q11 * Q7) >> 8 = Q10. 623fbf99c44a30f09d4e3402e192067d053ced5c55Bjorn Volcker tmp32 = (*delta * tmp16) >> 9; 63470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com 64c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org // If the exponent is small enough to give a non-zero probability we calculate 65c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org // |exp_value| ~= exp(-(x - m)^2 / (2 * s^2)) 66c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org // ~= exp2(-log2(exp(1)) * |tmp32|). 67c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org if (tmp32 < kCompVar) { 68c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org // Calculate |tmp16| = log2(exp(1)) * |tmp32|, in Q10. 69c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org // Q-domain: (Q12 * Q10) >> 12 = Q10. 703fbf99c44a30f09d4e3402e192067d053ced5c55Bjorn Volcker tmp16 = (int16_t)((kLog2Exp * tmp32) >> 12); 71c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org tmp16 = -tmp16; 72c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org exp_value = (0x0400 | (tmp16 & 0x03FF)); 73c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org tmp16 ^= 0xFFFF; 74c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org tmp16 >>= 10; 75c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org tmp16 += 1; 76c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org // Get |exp_value| = exp(-|tmp32|) in Q10. 77c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org exp_value >>= tmp16; 78c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org } 79470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com 80c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org // Calculate and return (1 / s) * exp(-(x - m)^2 / (2 * s^2)), in Q20. 81c68f80a70ab7912114d24a48f2a72e33756d30c9bjornv@webrtc.org // Q-domain: Q10 * Q10 = Q20. 82d25c0340516339718fa06f72ca41d93c7870cac8bjornv@webrtc.org return inv_std * exp_value; 83470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com} 84