2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
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.
13 #include "webrtc/modules/audio_processing/ns/include/noise_suppression_x.h"
14 #include "webrtc/modules/audio_processing/ns/nsx_core.h"
15 #include "webrtc/modules/audio_processing/ns/nsx_defines.h"
17 static const int16_t kIndicatorTable[17] = {
18 0, 2017, 3809, 5227, 6258, 6963, 7424, 7718,
19 7901, 8014, 8084, 8126, 8152, 8168, 8177, 8183, 8187
22 // Compute speech/noise probability
23 // speech/noise probability is returned in: probSpeechFinal
24 //snrLocPrior is the prior SNR for each frequency (in Q11)
25 //snrLocPost is the post SNR for each frequency (in Q11)
26 void WebRtcNsx_SpeechNoiseProb(NsxInst_t* inst,
27 uint16_t* nonSpeechProbFinal,
28 uint32_t* priorLocSnr,
29 uint32_t* postLocSnr) {
31 uint32_t zeros, num, den, tmpU32no1, tmpU32no2, tmpU32no3;
32 int32_t invLrtFX, indPriorFX, tmp32, tmp32no1, tmp32no2, besselTmpFX32;
33 int32_t frac32, logTmp;
34 int32_t logLrtTimeAvgKsumFX;
36 int16_t tmp16, tmp16no1, tmp16no2, tmpIndFX, tableIndex, frac, intPart;
37 int i, normTmp, normTmp2, nShifts;
39 // compute feature based on average LR factor
40 // this is the average over all frequencies of the smooth log LRT
41 logLrtTimeAvgKsumFX = 0;
42 for (i = 0; i < inst->magnLen; i++) {
43 besselTmpFX32 = (int32_t)postLocSnr[i]; // Q11
44 normTmp = WebRtcSpl_NormU32(postLocSnr[i]);
45 num = postLocSnr[i] << normTmp; // Q(11+normTmp)
47 den = priorLocSnr[i] << (normTmp - 11); // Q(normTmp)
49 den = priorLocSnr[i] >> (11 - normTmp); // Q(normTmp)
52 besselTmpFX32 -= num / den; // Q11
57 // inst->logLrtTimeAvg[i] += LRT_TAVG * (besselTmp - log(snrLocPrior)
58 // - inst->logLrtTimeAvg[i]);
59 // Here, LRT_TAVG = 0.5
60 zeros = WebRtcSpl_NormU32(priorLocSnr[i]);
61 frac32 = (int32_t)(((priorLocSnr[i] << zeros) & 0x7FFFFFFF) >> 19);
62 tmp32 = (frac32 * frac32 * -43) >> 19;
63 tmp32 += WEBRTC_SPL_MUL_16_16_RSFT((int16_t)frac32, 5412, 12);
65 // tmp32 = log2(priorLocSnr[i])
66 tmp32 = (int32_t)(((31 - zeros) << 12) + frac32) - (11 << 12); // Q12
67 logTmp = (tmp32 * 178) >> 8; // log2(priorLocSnr[i])*log(2)
68 // tmp32no1 = LRT_TAVG * (log(snrLocPrior) + inst->logLrtTimeAvg[i]) in Q12.
69 tmp32no1 = (logTmp + inst->logLrtTimeAvgW32[i]) / 2;
70 inst->logLrtTimeAvgW32[i] += (besselTmpFX32 - tmp32no1); // Q12
72 logLrtTimeAvgKsumFX += inst->logLrtTimeAvgW32[i]; // Q12
74 inst->featureLogLrt = (logLrtTimeAvgKsumFX * BIN_SIZE_LRT) >>
77 // done with computation of LR factor
80 //compute the indicator functions
83 // average LRT feature
85 // indicator0 = 0.5 * (tanh(widthPrior *
86 // (logLrtTimeAvgKsum - threshPrior0)) + 1.0);
87 tmpIndFX = 16384; // Q14(1.0)
88 tmp32no1 = logLrtTimeAvgKsumFX - inst->thresholdLogLrt; // Q12
89 nShifts = 7 - inst->stages; // WIDTH_PR_MAP_SHIFT - inst->stages + 5;
90 //use larger width in tanh map for pause regions
94 //widthPrior = widthPrior * 2.0;
97 tmp32no1 = WEBRTC_SPL_SHIFT_W32(tmp32no1, nShifts); // Q14
98 // compute indicator function: sigmoid map
99 tableIndex = (int16_t)(tmp32no1 >> 14);
100 if ((tableIndex < 16) && (tableIndex >= 0)) {
101 tmp16no2 = kIndicatorTable[tableIndex];
102 tmp16no1 = kIndicatorTable[tableIndex + 1] - kIndicatorTable[tableIndex];
103 frac = (int16_t)(tmp32no1 & 0x00003fff); // Q14
104 tmp16no2 += (int16_t)WEBRTC_SPL_MUL_16_16_RSFT(tmp16no1, frac, 14);
106 tmpIndFX = 8192 - tmp16no2; // Q14
108 tmpIndFX = 8192 + tmp16no2; // Q14
111 indPriorFX = WEBRTC_SPL_MUL_16_16(inst->weightLogLrt, tmpIndFX); // 6*Q14
113 //spectral flatness feature
114 if (inst->weightSpecFlat) {
115 tmpU32no1 = WEBRTC_SPL_UMUL(inst->featureSpecFlat, 400); // Q10
116 tmpIndFX = 16384; // Q14(1.0)
117 //use larger width in tanh map for pause regions
118 tmpU32no2 = inst->thresholdSpecFlat - tmpU32no1; //Q10
120 if (inst->thresholdSpecFlat < tmpU32no1) {
122 tmpU32no2 = tmpU32no1 - inst->thresholdSpecFlat;
123 //widthPrior = widthPrior * 2.0;
126 tmpU32no1 = WebRtcSpl_DivU32U16(tmpU32no2 << nShifts, 25); // Q14
127 // compute indicator function: sigmoid map
129 // indicator1 = 0.5 * (tanh(sgnMap * widthPrior *
130 // (threshPrior1 - tmpFloat1)) + 1.0);
131 tableIndex = (int16_t)(tmpU32no1 >> 14);
132 if (tableIndex < 16) {
133 tmp16no2 = kIndicatorTable[tableIndex];
134 tmp16no1 = kIndicatorTable[tableIndex + 1] - kIndicatorTable[tableIndex];
135 frac = (int16_t)(tmpU32no1 & 0x00003fff); // Q14
136 tmp16no2 += (int16_t)WEBRTC_SPL_MUL_16_16_RSFT(tmp16no1, frac, 14);
138 tmpIndFX = 8192 + tmp16no2; // Q14
140 tmpIndFX = 8192 - tmp16no2; // Q14
143 indPriorFX += WEBRTC_SPL_MUL_16_16(inst->weightSpecFlat, tmpIndFX); // 6*Q14
146 //for template spectral-difference
147 if (inst->weightSpecDiff) {
149 if (inst->featureSpecDiff) {
150 normTmp = WEBRTC_SPL_MIN(20 - inst->stages,
151 WebRtcSpl_NormU32(inst->featureSpecDiff));
152 assert(normTmp >= 0);
153 tmpU32no1 = inst->featureSpecDiff << normTmp; // Q(normTmp-2*stages)
154 tmpU32no2 = inst->timeAvgMagnEnergy >> (20 - inst->stages - normTmp);
156 // Q(20 - inst->stages)
157 tmpU32no1 /= tmpU32no2;
159 tmpU32no1 = (uint32_t)(0x7fffffff);
162 tmpU32no3 = (inst->thresholdSpecDiff << 17) / 25;
163 tmpU32no2 = tmpU32no1 - tmpU32no3;
165 tmpIndFX = 16384; // Q14(1.0)
166 //use larger width in tanh map for pause regions
167 if (tmpU32no2 & 0x80000000) {
169 tmpU32no2 = tmpU32no3 - tmpU32no1;
170 //widthPrior = widthPrior * 2.0;
173 tmpU32no1 = tmpU32no2 >> nShifts;
174 // compute indicator function: sigmoid map
176 indicator2 = 0.5 * (tanh(widthPrior * (tmpFloat1 - threshPrior2)) + 1.0);
178 tableIndex = (int16_t)(tmpU32no1 >> 14);
179 if (tableIndex < 16) {
180 tmp16no2 = kIndicatorTable[tableIndex];
181 tmp16no1 = kIndicatorTable[tableIndex + 1] - kIndicatorTable[tableIndex];
182 frac = (int16_t)(tmpU32no1 & 0x00003fff); // Q14
183 tmp16no2 += (int16_t)WEBRTC_SPL_MUL_16_16_RSFT_WITH_ROUND(
186 tmpIndFX = 8192 + tmp16no2;
188 tmpIndFX = 8192 - tmp16no2;
191 indPriorFX += WEBRTC_SPL_MUL_16_16(inst->weightSpecDiff, tmpIndFX); // 6*Q14
194 //combine the indicator function with the feature weights
196 // indPrior = 1 - (weightIndPrior0 * indicator0 + weightIndPrior1 *
197 // indicator1 + weightIndPrior2 * indicator2);
198 indPriorFX16 = WebRtcSpl_DivW32W16ResW16(98307 - indPriorFX, 6); // Q14
199 // done with computing indicator function
201 //compute the prior probability
203 // inst->priorNonSpeechProb += PRIOR_UPDATE *
204 // (indPriorNonSpeech - inst->priorNonSpeechProb);
205 tmp16 = indPriorFX16 - inst->priorNonSpeechProb; // Q14
206 inst->priorNonSpeechProb += (int16_t)WEBRTC_SPL_MUL_16_16_RSFT(
207 PRIOR_UPDATE_Q14, tmp16, 14); // Q14
209 //final speech probability: combine prior model with LR factor:
211 memset(nonSpeechProbFinal, 0, sizeof(uint16_t) * inst->magnLen);
213 if (inst->priorNonSpeechProb > 0) {
214 for (i = 0; i < inst->magnLen; i++) {
216 // invLrt = exp(inst->logLrtTimeAvg[i]);
217 // invLrt = inst->priorSpeechProb * invLrt;
218 // nonSpeechProbFinal[i] = (1.0 - inst->priorSpeechProb) /
219 // (1.0 - inst->priorSpeechProb + invLrt);
220 // invLrt = (1.0 - inst->priorNonSpeechProb) * invLrt;
221 // nonSpeechProbFinal[i] = inst->priorNonSpeechProb /
222 // (inst->priorNonSpeechProb + invLrt);
223 if (inst->logLrtTimeAvgW32[i] < 65300) {
224 tmp32no1 = (inst->logLrtTimeAvgW32[i] * 23637) >> 14; // Q12
225 intPart = (int16_t)(tmp32no1 >> 12);
229 frac = (int16_t)(tmp32no1 & 0x00000fff); // Q12
231 // Quadratic approximation of 2^frac
232 tmp32no2 = (frac * frac * 44) >> 19; // Q12.
233 tmp32no2 += WEBRTC_SPL_MUL_16_16_RSFT(frac, 84, 7); // Q12
234 invLrtFX = (1 << (8 + intPart)) +
235 WEBRTC_SPL_SHIFT_W32(tmp32no2, intPart - 4); // Q8
237 normTmp = WebRtcSpl_NormW32(invLrtFX);
238 normTmp2 = WebRtcSpl_NormW16((16384 - inst->priorNonSpeechProb));
239 if (normTmp + normTmp2 >= 7) {
240 if (normTmp + normTmp2 < 15) {
241 invLrtFX >>= 15 - normTmp2 - normTmp;
242 // Q(normTmp+normTmp2-7)
243 tmp32no1 = invLrtFX * (16384 - inst->priorNonSpeechProb);
244 // Q(normTmp+normTmp2+7)
245 invLrtFX = WEBRTC_SPL_SHIFT_W32(tmp32no1, 7 - normTmp - normTmp2);
248 tmp32no1 = invLrtFX * (16384 - inst->priorNonSpeechProb);
250 invLrtFX = tmp32no1 >> 8; // Q14.
253 tmp32no1 = (int32_t)inst->priorNonSpeechProb << 8; // Q22
255 nonSpeechProbFinal[i] = tmp32no1 /
256 (inst->priorNonSpeechProb + invLrtFX); // Q8