blob: 4c568fafb715a8f02c515990fae3dc0011c5c719 [file] [log] [blame]
Richard Burton00553462021-11-10 16:27:14 +00001/*
2 * Copyright (c) 2021 Arm Limited. All rights reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17#include "RNNoiseProcess.hpp"
alexander31ae9f02022-02-10 16:15:54 +000018#include "log_macros.h"
19
Richard Burton00553462021-11-10 16:27:14 +000020#include <algorithm>
21#include <cmath>
22#include <cstring>
23
24namespace arm {
25namespace app {
26namespace rnn {
27
28#define VERIFY(x) \
29do { \
30 if (!(x)) { \
31 printf_err("Assert failed:" #x "\n"); \
32 exit(1); \
33 } \
34} while(0)
35
36RNNoiseProcess::RNNoiseProcess() :
37 m_halfWindow(FRAME_SIZE, 0),
38 m_dctTable(NB_BANDS * NB_BANDS),
39 m_analysisMem(FRAME_SIZE, 0),
40 m_cepstralMem(CEPS_MEM, vec1D32F(NB_BANDS, 0)),
41 m_memId{0},
42 m_synthesisMem(FRAME_SIZE, 0),
43 m_pitchBuf(PITCH_BUF_SIZE, 0),
44 m_lastGain{0.0},
45 m_lastPeriod{0},
46 m_memHpX{},
47 m_lastGVec(NB_BANDS, 0)
48{
49 constexpr uint32_t numFFt = 2 * FRAME_SIZE;
50 static_assert(numFFt != 0, "Num FFT can't be 0");
51
52 math::MathUtils::FftInitF32(numFFt, this->m_fftInstReal, FftType::real);
53 math::MathUtils::FftInitF32(numFFt, this->m_fftInstCmplx, FftType::complex);
54 this->InitTables();
55}
56
57void RNNoiseProcess::PreprocessFrame(const float* audioData,
58 const size_t audioLen,
59 FrameFeatures& features)
60{
61 /* Note audioWindow is modified in place */
62 const arrHp aHp {-1.99599, 0.99600 };
63 const arrHp bHp {-2.00000, 1.00000 };
64
65 vec1D32F audioWindow{audioData, audioData + audioLen};
66
67 this->BiQuad(bHp, aHp, this->m_memHpX, audioWindow);
68 this->ComputeFrameFeatures(audioWindow, features);
69}
70
71void RNNoiseProcess::PostProcessFrame(vec1D32F& modelOutput, FrameFeatures& features, vec1D32F& outFrame)
72{
George Gekova2b0fc22021-11-08 16:30:43 +000073 std::vector<float> outputBands = modelOutput;
74 std::vector<float> gain(FREQ_SIZE, 0);
Richard Burton00553462021-11-10 16:27:14 +000075
76 if (!features.m_silence) {
George Gekova2b0fc22021-11-08 16:30:43 +000077 PitchFilter(features, outputBands);
Richard Burton00553462021-11-10 16:27:14 +000078 for (size_t i = 0; i < NB_BANDS; i++) {
79 float alpha = .6f;
George Gekova2b0fc22021-11-08 16:30:43 +000080 outputBands[i] = std::max(outputBands[i], alpha * m_lastGVec[i]);
81 m_lastGVec[i] = outputBands[i];
Richard Burton00553462021-11-10 16:27:14 +000082 }
George Gekova2b0fc22021-11-08 16:30:43 +000083 InterpBandGain(gain, outputBands);
Richard Burton00553462021-11-10 16:27:14 +000084 for (size_t i = 0; i < FREQ_SIZE; i++) {
George Gekova2b0fc22021-11-08 16:30:43 +000085 features.m_fftX[2 * i] *= gain[i]; /* Real. */
86 features.m_fftX[2 * i + 1] *= gain[i]; /*imaginary. */
Richard Burton00553462021-11-10 16:27:14 +000087
88 }
89
90 }
91
92 FrameSynthesis(outFrame, features.m_fftX);
93}
94
95void RNNoiseProcess::InitTables()
96{
97 constexpr float pi = M_PI;
98 constexpr float halfPi = M_PI / 2;
99 constexpr float halfPiOverFrameSz = halfPi/FRAME_SIZE;
100
101 for (uint32_t i = 0; i < FRAME_SIZE; i++) {
102 const float sinVal = math::MathUtils::SineF32(halfPiOverFrameSz * (i + 0.5f));
103 m_halfWindow[i] = math::MathUtils::SineF32(halfPi * sinVal * sinVal);
104 }
105
106 for (uint32_t i = 0; i < NB_BANDS; i++) {
107 for (uint32_t j = 0; j < NB_BANDS; j++) {
108 m_dctTable[i * NB_BANDS + j] = math::MathUtils::CosineF32((i + 0.5f) * j * pi / NB_BANDS);
109 }
110 m_dctTable[i * NB_BANDS] *= math::MathUtils::SqrtF32(0.5f);
111 }
112}
113
114void RNNoiseProcess::BiQuad(
115 const arrHp& bHp,
116 const arrHp& aHp,
117 arrHp& memHpX,
118 vec1D32F& audioWindow)
119{
120 for (float& audioElement : audioWindow) {
121 const auto xi = audioElement;
122 const auto yi = audioElement + memHpX[0];
123 memHpX[0] = memHpX[1] + (bHp[0] * xi - aHp[0] * yi);
124 memHpX[1] = (bHp[1] * xi - aHp[1] * yi);
125 audioElement = yi;
126 }
127}
128
129void RNNoiseProcess::ComputeFrameFeatures(vec1D32F& audioWindow,
130 FrameFeatures& features)
131{
132 this->FrameAnalysis(audioWindow,
133 features.m_fftX,
134 features.m_Ex,
135 this->m_analysisMem);
136
George Gekova2b0fc22021-11-08 16:30:43 +0000137 float energy = 0.0;
Richard Burton00553462021-11-10 16:27:14 +0000138
139 vec1D32F Ly(NB_BANDS, 0);
140 vec1D32F p(WINDOW_SIZE, 0);
141 vec1D32F pitchBuf(PITCH_BUF_SIZE >> 1, 0);
142
143 VERIFY(PITCH_BUF_SIZE >= this->m_pitchBuf.size());
144 std::copy_n(this->m_pitchBuf.begin() + FRAME_SIZE,
145 PITCH_BUF_SIZE - FRAME_SIZE,
146 this->m_pitchBuf.begin());
147
148 VERIFY(FRAME_SIZE <= audioWindow.size() && PITCH_BUF_SIZE > FRAME_SIZE);
149 std::copy_n(audioWindow.begin(),
150 FRAME_SIZE,
151 this->m_pitchBuf.begin() + PITCH_BUF_SIZE - FRAME_SIZE);
152
153 this->PitchDownsample(pitchBuf, PITCH_BUF_SIZE);
154
155 VERIFY(pitchBuf.size() > PITCH_MAX_PERIOD/2);
156 vec1D32F xLp(pitchBuf.size() - PITCH_MAX_PERIOD/2, 0);
157 std::copy_n(pitchBuf.begin() + PITCH_MAX_PERIOD/2, xLp.size(), xLp.begin());
158
159 int pitchIdx = this->PitchSearch(xLp, pitchBuf,
160 PITCH_FRAME_SIZE, (PITCH_MAX_PERIOD - (3*PITCH_MIN_PERIOD)));
161
162 pitchIdx = this->RemoveDoubling(
163 pitchBuf,
164 PITCH_MAX_PERIOD,
165 PITCH_MIN_PERIOD,
166 PITCH_FRAME_SIZE,
167 PITCH_MAX_PERIOD - pitchIdx);
168
169 size_t stIdx = PITCH_BUF_SIZE - WINDOW_SIZE - pitchIdx;
170 VERIFY((static_cast<int>(PITCH_BUF_SIZE) - static_cast<int>(WINDOW_SIZE) - pitchIdx) >= 0);
171 std::copy_n(this->m_pitchBuf.begin() + stIdx, WINDOW_SIZE, p.begin());
172
173 this->ApplyWindow(p);
174 this->ForwardTransform(p, features.m_fftP);
175 this->ComputeBandEnergy(features.m_fftP, features.m_Ep);
176 this->ComputeBandCorr(features.m_fftX, features.m_fftP, features.m_Exp);
177
178 for (uint32_t i = 0 ; i < NB_BANDS; ++i) {
179 features.m_Exp[i] /= math::MathUtils::SqrtF32(
180 0.001f + features.m_Ex[i] * features.m_Ep[i]);
181 }
182
183 vec1D32F dctVec(NB_BANDS, 0);
184 this->DCT(features.m_Exp, dctVec);
185
186 features.m_featuresVec = vec1D32F (NB_FEATURES, 0);
187 for (uint32_t i = 0; i < NB_DELTA_CEPS; ++i) {
188 features.m_featuresVec[NB_BANDS + 2*NB_DELTA_CEPS + i] = dctVec[i];
189 }
190
191 features.m_featuresVec[NB_BANDS + 2*NB_DELTA_CEPS] -= 1.3;
192 features.m_featuresVec[NB_BANDS + 2*NB_DELTA_CEPS + 1] -= 0.9;
193 features.m_featuresVec[NB_BANDS + 3*NB_DELTA_CEPS] = 0.01 * (static_cast<int>(pitchIdx) - 300);
194
195 float logMax = -2.f;
196 float follow = -2.f;
197 for (uint32_t i = 0; i < NB_BANDS; ++i) {
198 Ly[i] = log10f(1e-2f + features.m_Ex[i]);
199 Ly[i] = std::max<float>(logMax - 7, std::max<float>(follow - 1.5, Ly[i]));
200 logMax = std::max<float>(logMax, Ly[i]);
201 follow = std::max<float>(follow - 1.5, Ly[i]);
George Gekova2b0fc22021-11-08 16:30:43 +0000202 energy += features.m_Ex[i];
Richard Burton00553462021-11-10 16:27:14 +0000203 }
204
205 /* If there's no audio avoid messing up the state. */
206 features.m_silence = true;
George Gekova2b0fc22021-11-08 16:30:43 +0000207 if (energy < 0.04) {
Richard Burton00553462021-11-10 16:27:14 +0000208 return;
209 } else {
210 features.m_silence = false;
211 }
212
213 this->DCT(Ly, features.m_featuresVec);
214 features.m_featuresVec[0] -= 12.0;
215 features.m_featuresVec[1] -= 4.0;
216
217 VERIFY(CEPS_MEM > 2);
218 uint32_t stIdx1 = this->m_memId < 1 ? CEPS_MEM + this->m_memId - 1 : this->m_memId - 1;
219 uint32_t stIdx2 = this->m_memId < 2 ? CEPS_MEM + this->m_memId - 2 : this->m_memId - 2;
George Gekova2b0fc22021-11-08 16:30:43 +0000220 VERIFY(stIdx1 < this->m_cepstralMem.size());
221 VERIFY(stIdx2 < this->m_cepstralMem.size());
Richard Burton00553462021-11-10 16:27:14 +0000222 auto ceps1 = this->m_cepstralMem[stIdx1];
223 auto ceps2 = this->m_cepstralMem[stIdx2];
224
225 /* Ceps 0 */
226 for (uint32_t i = 0; i < NB_BANDS; ++i) {
227 this->m_cepstralMem[this->m_memId][i] = features.m_featuresVec[i];
228 }
229
230 for (uint32_t i = 0; i < NB_DELTA_CEPS; ++i) {
231 features.m_featuresVec[i] = this->m_cepstralMem[this->m_memId][i] + ceps1[i] + ceps2[i];
232 features.m_featuresVec[NB_BANDS + i] = this->m_cepstralMem[this->m_memId][i] - ceps2[i];
233 features.m_featuresVec[NB_BANDS + NB_DELTA_CEPS + i] =
234 this->m_cepstralMem[this->m_memId][i] - 2 * ceps1[i] + ceps2[i];
235 }
236
237 /* Spectral variability features. */
238 this->m_memId += 1;
239 if (this->m_memId == CEPS_MEM) {
240 this->m_memId = 0;
241 }
242
243 float specVariability = 0.f;
244
245 VERIFY(this->m_cepstralMem.size() >= CEPS_MEM);
246 for (size_t i = 0; i < CEPS_MEM; ++i) {
247 float minDist = 1e15;
248 for (size_t j = 0; j < CEPS_MEM; ++j) {
249 float dist = 0.f;
250 for (size_t k = 0; k < NB_BANDS; ++k) {
251 VERIFY(this->m_cepstralMem[i].size() >= NB_BANDS);
252 auto tmp = this->m_cepstralMem[i][k] - this->m_cepstralMem[j][k];
253 dist += tmp * tmp;
254 }
255
256 if (j != i) {
257 minDist = std::min<float>(minDist, dist);
258 }
259 }
260 specVariability += minDist;
261 }
262
263 VERIFY(features.m_featuresVec.size() >= NB_BANDS + 3 * NB_DELTA_CEPS + 1);
264 features.m_featuresVec[NB_BANDS + 3 * NB_DELTA_CEPS + 1] = specVariability / CEPS_MEM - 2.1;
265}
266
267void RNNoiseProcess::FrameAnalysis(
268 const vec1D32F& audioWindow,
269 vec1D32F& fft,
270 vec1D32F& energy,
271 vec1D32F& analysisMem)
272{
273 vec1D32F x(WINDOW_SIZE, 0);
274
275 /* Move old audio down and populate end with latest audio window. */
276 VERIFY(x.size() >= FRAME_SIZE && analysisMem.size() >= FRAME_SIZE);
277 VERIFY(audioWindow.size() >= FRAME_SIZE);
278
279 std::copy_n(analysisMem.begin(), FRAME_SIZE, x.begin());
280 std::copy_n(audioWindow.begin(), x.size() - FRAME_SIZE, x.begin() + FRAME_SIZE);
281 std::copy_n(audioWindow.begin(), FRAME_SIZE, analysisMem.begin());
282
283 this->ApplyWindow(x);
284
285 /* Calculate FFT. */
286 ForwardTransform(x, fft);
287
288 /* Compute band energy. */
289 ComputeBandEnergy(fft, energy);
290}
291
292void RNNoiseProcess::ApplyWindow(vec1D32F& x)
293{
294 if (WINDOW_SIZE != x.size()) {
295 printf_err("Invalid size for vector to be windowed\n");
296 return;
297 }
298
299 VERIFY(this->m_halfWindow.size() >= FRAME_SIZE);
300
301 /* Multiply input by sinusoidal function. */
302 for (size_t i = 0; i < FRAME_SIZE; i++) {
303 x[i] *= this->m_halfWindow[i];
304 x[WINDOW_SIZE - 1 - i] *= this->m_halfWindow[i];
305 }
306}
307
308void RNNoiseProcess::ForwardTransform(
309 vec1D32F& x,
310 vec1D32F& fft)
311{
312 /* The input vector can be modified by the fft function. */
313 fft.reserve(x.size() + 2);
314 fft.resize(x.size() + 2, 0);
315 math::MathUtils::FftF32(x, fft, this->m_fftInstReal);
316
317 /* Normalise. */
318 for (auto& f : fft) {
319 f /= this->m_fftInstReal.m_fftLen;
320 }
321
322 /* Place the last freq element correctly */
323 fft[fft.size()-2] = fft[1];
324 fft[1] = 0;
325
326 /* NOTE: We don't truncate out FFT vector as it already contains only the
327 * first half of the FFT's. The conjugates are not present. */
328}
329
330void RNNoiseProcess::ComputeBandEnergy(const vec1D32F& fftX, vec1D32F& bandE)
331{
332 bandE = vec1D32F(NB_BANDS, 0);
333
334 VERIFY(this->m_eband5ms.size() >= NB_BANDS);
335 for (uint32_t i = 0; i < NB_BANDS - 1; i++) {
336 const auto bandSize = (this->m_eband5ms[i + 1] - this->m_eband5ms[i])
337 << FRAME_SIZE_SHIFT;
338
339 for (uint32_t j = 0; j < bandSize; j++) {
340 const auto frac = static_cast<float>(j) / bandSize;
341 const auto idx = (this->m_eband5ms[i] << FRAME_SIZE_SHIFT) + j;
342
343 auto tmp = fftX[2 * idx] * fftX[2 * idx]; /* Real part */
344 tmp += fftX[2 * idx + 1] * fftX[2 * idx + 1]; /* Imaginary part */
345
346 bandE[i] += (1 - frac) * tmp;
347 bandE[i + 1] += frac * tmp;
348 }
349 }
350 bandE[0] *= 2;
351 bandE[NB_BANDS - 1] *= 2;
352}
353
354void RNNoiseProcess::ComputeBandCorr(const vec1D32F& X, const vec1D32F& P, vec1D32F& bandC)
355{
356 bandC = vec1D32F(NB_BANDS, 0);
357 VERIFY(this->m_eband5ms.size() >= NB_BANDS);
358
359 for (uint32_t i = 0; i < NB_BANDS - 1; i++) {
360 const auto bandSize = (this->m_eband5ms[i + 1] - this->m_eband5ms[i]) << FRAME_SIZE_SHIFT;
361
362 for (uint32_t j = 0; j < bandSize; j++) {
363 const auto frac = static_cast<float>(j) / bandSize;
364 const auto idx = (this->m_eband5ms[i] << FRAME_SIZE_SHIFT) + j;
365
366 auto tmp = X[2 * idx] * P[2 * idx]; /* Real part */
367 tmp += X[2 * idx + 1] * P[2 * idx + 1]; /* Imaginary part */
368
369 bandC[i] += (1 - frac) * tmp;
370 bandC[i + 1] += frac * tmp;
371 }
372 }
373 bandC[0] *= 2;
374 bandC[NB_BANDS - 1] *= 2;
375}
376
377void RNNoiseProcess::DCT(vec1D32F& input, vec1D32F& output)
378{
379 VERIFY(this->m_dctTable.size() >= NB_BANDS * NB_BANDS);
380 for (uint32_t i = 0; i < NB_BANDS; ++i) {
381 float sum = 0;
382
383 for (uint32_t j = 0, k = 0; j < NB_BANDS; ++j, k += NB_BANDS) {
384 sum += input[j] * this->m_dctTable[k + i];
385 }
386 output[i] = sum * math::MathUtils::SqrtF32(2.0/22);
387 }
388}
389
390void RNNoiseProcess::PitchDownsample(vec1D32F& pitchBuf, size_t pitchBufSz) {
391 for (size_t i = 1; i < (pitchBufSz >> 1); ++i) {
392 pitchBuf[i] = 0.5 * (
393 0.5 * (this->m_pitchBuf[2 * i - 1] + this->m_pitchBuf[2 * i + 1])
394 + this->m_pitchBuf[2 * i]);
395 }
396
397 pitchBuf[0] = 0.5*(0.5*(this->m_pitchBuf[1]) + this->m_pitchBuf[0]);
398
399 vec1D32F ac(5, 0);
400 size_t numLags = 4;
401
402 this->AutoCorr(pitchBuf, ac, numLags, pitchBufSz >> 1);
403
404 /* Noise floor -40db */
405 ac[0] *= 1.0001;
406
407 /* Lag windowing. */
408 for (size_t i = 1; i < numLags + 1; ++i) {
409 ac[i] -= ac[i] * (0.008 * i) * (0.008 * i);
410 }
411
412 vec1D32F lpc(numLags, 0);
413 this->LPC(ac, numLags, lpc);
414
415 float tmp = 1.0;
416 for (size_t i = 0; i < numLags; ++i) {
417 tmp = 0.9f * tmp;
418 lpc[i] = lpc[i] * tmp;
419 }
420
421 vec1D32F lpc2(numLags + 1, 0);
422 float c1 = 0.8;
423
424 /* Add a zero. */
425 lpc2[0] = lpc[0] + 0.8;
426 lpc2[1] = lpc[1] + (c1 * lpc[0]);
427 lpc2[2] = lpc[2] + (c1 * lpc[1]);
428 lpc2[3] = lpc[3] + (c1 * lpc[2]);
429 lpc2[4] = (c1 * lpc[3]);
430
431 this->Fir5(lpc2, pitchBufSz >> 1, pitchBuf);
432}
433
434int RNNoiseProcess::PitchSearch(vec1D32F& xLp, vec1D32F& y, uint32_t len, uint32_t maxPitch) {
435 uint32_t lag = len + maxPitch;
436 vec1D32F xLp4(len >> 2, 0);
437 vec1D32F yLp4(lag >> 2, 0);
438 vec1D32F xCorr(maxPitch >> 1, 0);
439
440 /* Downsample by 2 again. */
441 for (size_t j = 0; j < (len >> 2); ++j) {
442 xLp4[j] = xLp[2*j];
443 }
444 for (size_t j = 0; j < (lag >> 2); ++j) {
445 yLp4[j] = y[2*j];
446 }
447
448 this->PitchXCorr(xLp4, yLp4, xCorr, len >> 2, maxPitch >> 2);
449
450 /* Coarse search with 4x decimation. */
451 arrHp bestPitch = this->FindBestPitch(xCorr, yLp4, len >> 2, maxPitch >> 2);
452
453 /* Finer search with 2x decimation. */
454 const int maxIdx = (maxPitch >> 1);
455 for (int i = 0; i < maxIdx; ++i) {
456 xCorr[i] = 0;
457 if (std::abs(i - 2*bestPitch[0]) > 2 and std::abs(i - 2*bestPitch[1]) > 2) {
458 continue;
459 }
460 float sum = 0;
461 for (size_t j = 0; j < len >> 1; ++j) {
462 sum += xLp[j] * y[i+j];
463 }
464
465 xCorr[i] = std::max(-1.0f, sum);
466 }
467
468 bestPitch = this->FindBestPitch(xCorr, y, len >> 1, maxPitch >> 1);
469
470 int offset;
471 /* Refine by pseudo-interpolation. */
472 if ( 0 < bestPitch[0] && bestPitch[0] < ((maxPitch >> 1) - 1)) {
473 float a = xCorr[bestPitch[0] - 1];
474 float b = xCorr[bestPitch[0]];
475 float c = xCorr[bestPitch[0] + 1];
476
477 if ( (c-a) > 0.7*(b-a) ) {
478 offset = 1;
479 } else if ( (a-c) > 0.7*(b-c) ) {
480 offset = -1;
481 } else {
482 offset = 0;
483 }
484 } else {
485 offset = 0;
486 }
487
488 return 2*bestPitch[0] - offset;
489}
490
491arrHp RNNoiseProcess::FindBestPitch(vec1D32F& xCorr, vec1D32F& y, uint32_t len, uint32_t maxPitch)
492{
493 float Syy = 1;
494 arrHp bestNum {-1, -1};
495 arrHp bestDen {0, 0};
496 arrHp bestPitch {0, 1};
497
498 for (size_t j = 0; j < len; ++j) {
499 Syy += (y[j] * y[j]);
500 }
501
502 for (size_t i = 0; i < maxPitch; ++i ) {
503 if (xCorr[i] > 0) {
504 float xCorr16 = xCorr[i] * 1e-12f; /* Avoid problems when squaring. */
505
506 float num = xCorr16 * xCorr16;
507 if (num*bestDen[1] > bestNum[1]*Syy) {
508 if (num*bestDen[0] > bestNum[0]*Syy) {
509 bestNum[1] = bestNum[0];
510 bestDen[1] = bestDen[0];
511 bestPitch[1] = bestPitch[0];
512 bestNum[0] = num;
513 bestDen[0] = Syy;
514 bestPitch[0] = i;
515 } else {
516 bestNum[1] = num;
517 bestDen[1] = Syy;
518 bestPitch[1] = i;
519 }
520 }
521 }
522
523 Syy += (y[i+len]*y[i+len]) - (y[i]*y[i]);
524 Syy = std::max(1.0f, Syy);
525 }
526
527 return bestPitch;
528}
529
530int RNNoiseProcess::RemoveDoubling(
531 vec1D32F& pitchBuf,
532 uint32_t maxPeriod,
533 uint32_t minPeriod,
534 uint32_t frameSize,
535 size_t pitchIdx0_)
536{
537 constexpr std::array<size_t, 16> secondCheck {0, 0, 3, 2, 3, 2, 5, 2, 3, 2, 3, 2, 5, 2, 3, 2};
538 uint32_t minPeriod0 = minPeriod;
539 float lastPeriod = static_cast<float>(this->m_lastPeriod)/2;
540 float lastGain = static_cast<float>(this->m_lastGain);
541
542 maxPeriod /= 2;
543 minPeriod /= 2;
544 pitchIdx0_ /= 2;
545 frameSize /= 2;
546 uint32_t xStart = maxPeriod;
547
548 if (pitchIdx0_ >= maxPeriod) {
549 pitchIdx0_ = maxPeriod - 1;
550 }
551
552 size_t pitchIdx = pitchIdx0_;
George Gekova2b0fc22021-11-08 16:30:43 +0000553 const size_t pitchIdx0 = pitchIdx0_;
Richard Burton00553462021-11-10 16:27:14 +0000554
555 float xx = 0;
556 for ( size_t i = xStart; i < xStart+frameSize; ++i) {
557 xx += (pitchBuf[i] * pitchBuf[i]);
558 }
559
560 float xy = 0;
561 for ( size_t i = xStart; i < xStart+frameSize; ++i) {
562 xy += (pitchBuf[i] * pitchBuf[i-pitchIdx0]);
563 }
564
565 vec1D32F yyLookup (maxPeriod+1, 0);
566 yyLookup[0] = xx;
567 float yy = xx;
568
George Gekova2b0fc22021-11-08 16:30:43 +0000569 for ( size_t i = 1; i < yyLookup.size(); ++i) {
Richard Burton00553462021-11-10 16:27:14 +0000570 yy = yy + (pitchBuf[xStart-i] * pitchBuf[xStart-i]) -
571 (pitchBuf[xStart+frameSize-i] * pitchBuf[xStart+frameSize-i]);
572 yyLookup[i] = std::max(0.0f, yy);
573 }
574
575 yy = yyLookup[pitchIdx0];
576 float bestXy = xy;
577 float bestYy = yy;
578
579 float g = this->ComputePitchGain(xy, xx, yy);
580 float g0 = g;
581
582 /* Look for any pitch at pitchIndex/k. */
583 for ( size_t k = 2; k < 16; ++k) {
584 size_t pitchIdx1 = (2*pitchIdx0+k) / (2*k);
585 if (pitchIdx1 < minPeriod) {
586 break;
587 }
588
589 size_t pitchIdx1b;
590 /* Look for another strong correlation at T1b. */
591 if (k == 2) {
592 if ((pitchIdx1 + pitchIdx0) > maxPeriod) {
593 pitchIdx1b = pitchIdx0;
594 } else {
595 pitchIdx1b = pitchIdx0 + pitchIdx1;
596 }
597 } else {
598 pitchIdx1b = (2*(secondCheck[k])*pitchIdx0 + k) / (2*k);
599 }
600
601 xy = 0;
602 for ( size_t i = xStart; i < xStart+frameSize; ++i) {
603 xy += (pitchBuf[i] * pitchBuf[i-pitchIdx1]);
604 }
605
606 float xy2 = 0;
607 for ( size_t i = xStart; i < xStart+frameSize; ++i) {
608 xy2 += (pitchBuf[i] * pitchBuf[i-pitchIdx1b]);
609 }
610 xy = 0.5f * (xy + xy2);
George Gekova2b0fc22021-11-08 16:30:43 +0000611 VERIFY(pitchIdx1b < maxPeriod+1);
Richard Burton00553462021-11-10 16:27:14 +0000612 yy = 0.5f * (yyLookup[pitchIdx1] + yyLookup[pitchIdx1b]);
613
614 float g1 = this->ComputePitchGain(xy, xx, yy);
615
616 float cont;
617 if (std::abs(pitchIdx1-lastPeriod) <= 1) {
618 cont = lastGain;
619 } else if (std::abs(pitchIdx1-lastPeriod) <= 2 and 5*k*k < pitchIdx0) {
620 cont = 0.5f*lastGain;
621 } else {
622 cont = 0.0f;
623 }
624
625 float thresh = std::max(0.3, 0.7*g0-cont);
626
627 /* Bias against very high pitch (very short period) to avoid false-positives
628 * due to short-term correlation */
629 if (pitchIdx1 < 3*minPeriod) {
630 thresh = std::max(0.4, 0.85*g0-cont);
631 } else if (pitchIdx1 < 2*minPeriod) {
632 thresh = std::max(0.5, 0.9*g0-cont);
633 }
634 if (g1 > thresh) {
635 bestXy = xy;
636 bestYy = yy;
637 pitchIdx = pitchIdx1;
638 g = g1;
639 }
640 }
641
642 bestXy = std::max(0.0f, bestXy);
643 float pg;
644 if (bestYy <= bestXy) {
645 pg = 1.0;
646 } else {
647 pg = bestXy/(bestYy+1);
648 }
649
650 std::array<float, 3> xCorr {0};
651 for ( size_t k = 0; k < 3; ++k ) {
652 for ( size_t i = xStart; i < xStart+frameSize; ++i) {
653 xCorr[k] += (pitchBuf[i] * pitchBuf[i-(pitchIdx+k-1)]);
654 }
655 }
656
657 size_t offset;
658 if ((xCorr[2]-xCorr[0]) > 0.7*(xCorr[1]-xCorr[0])) {
659 offset = 1;
660 } else if ((xCorr[0]-xCorr[2]) > 0.7*(xCorr[1]-xCorr[2])) {
661 offset = -1;
662 } else {
663 offset = 0;
664 }
665
666 if (pg > g) {
667 pg = g;
668 }
669
670 pitchIdx0_ = 2*pitchIdx + offset;
671
672 if (pitchIdx0_ < minPeriod0) {
673 pitchIdx0_ = minPeriod0;
674 }
675
676 this->m_lastPeriod = pitchIdx0_;
677 this->m_lastGain = pg;
678
679 return this->m_lastPeriod;
680}
681
682float RNNoiseProcess::ComputePitchGain(float xy, float xx, float yy)
683{
684 return xy / math::MathUtils::SqrtF32(1+xx*yy);
685}
686
687void RNNoiseProcess::AutoCorr(
688 const vec1D32F& x,
689 vec1D32F& ac,
690 size_t lag,
691 size_t n)
692{
693 if (n < lag) {
694 printf_err("Invalid parameters for AutoCorr\n");
695 return;
696 }
697
698 auto fastN = n - lag;
699
700 /* Auto-correlation - can be done by PlatformMath functions */
701 this->PitchXCorr(x, x, ac, fastN, lag + 1);
702
703 /* Modify auto-correlation by summing with auto-correlation for different lags. */
704 for (size_t k = 0; k < lag + 1; k++) {
705 float d = 0;
706 for (size_t i = k + fastN; i < n; i++) {
707 d += x[i] * x[i - k];
708 }
709 ac[k] += d;
710 }
711}
712
713
714void RNNoiseProcess::PitchXCorr(
715 const vec1D32F& x,
716 const vec1D32F& y,
George Gekova2b0fc22021-11-08 16:30:43 +0000717 vec1D32F& xCorr,
Richard Burton00553462021-11-10 16:27:14 +0000718 size_t len,
719 size_t maxPitch)
720{
721 for (size_t i = 0; i < maxPitch; i++) {
722 float sum = 0;
723 for (size_t j = 0; j < len; j++) {
724 sum += x[j] * y[i + j];
725 }
George Gekova2b0fc22021-11-08 16:30:43 +0000726 xCorr[i] = sum;
Richard Burton00553462021-11-10 16:27:14 +0000727 }
728}
729
730/* Linear predictor coefficients */
731void RNNoiseProcess::LPC(
George Gekova2b0fc22021-11-08 16:30:43 +0000732 const vec1D32F& correlation,
Richard Burton00553462021-11-10 16:27:14 +0000733 int32_t p,
734 vec1D32F& lpc)
735{
George Gekova2b0fc22021-11-08 16:30:43 +0000736 auto error = correlation[0];
Richard Burton00553462021-11-10 16:27:14 +0000737
738 if (error != 0) {
739 for (int i = 0; i < p; i++) {
740
741 /* Sum up this iteration's reflection coefficient */
742 float rr = 0;
743 for (int j = 0; j < i; j++) {
George Gekova2b0fc22021-11-08 16:30:43 +0000744 rr += lpc[j] * correlation[i - j];
Richard Burton00553462021-11-10 16:27:14 +0000745 }
746
George Gekova2b0fc22021-11-08 16:30:43 +0000747 rr += correlation[i + 1];
Richard Burton00553462021-11-10 16:27:14 +0000748 auto r = -rr / error;
749
750 /* Update LP coefficients and total error */
751 lpc[i] = r;
752 for (int j = 0; j < ((i + 1) >> 1); j++) {
753 auto tmp1 = lpc[j];
754 auto tmp2 = lpc[i - 1 - j];
755 lpc[j] = tmp1 + (r * tmp2);
756 lpc[i - 1 - j] = tmp2 + (r * tmp1);
757 }
758
759 error = error - (r * r * error);
760
761 /* Bail out once we get 30dB gain */
George Gekova2b0fc22021-11-08 16:30:43 +0000762 if (error < (0.001 * correlation[0])) {
Richard Burton00553462021-11-10 16:27:14 +0000763 break;
764 }
765 }
766 }
767}
768
769void RNNoiseProcess::Fir5(
770 const vec1D32F &num,
771 uint32_t N,
772 vec1D32F &x)
773{
774 auto num0 = num[0];
775 auto num1 = num[1];
776 auto num2 = num[2];
777 auto num3 = num[3];
778 auto num4 = num[4];
779 auto mem0 = 0;
780 auto mem1 = 0;
781 auto mem2 = 0;
782 auto mem3 = 0;
783 auto mem4 = 0;
784 for (uint32_t i = 0; i < N; i++)
785 {
786 auto sum_ = x[i] + (num0 * mem0) + (num1 * mem1) +
787 (num2 * mem2) + (num3 * mem3) + (num4 * mem4);
788 mem4 = mem3;
789 mem3 = mem2;
790 mem2 = mem1;
791 mem1 = mem0;
792 mem0 = x[i];
793 x[i] = sum_;
794 }
795}
796
George Gekova2b0fc22021-11-08 16:30:43 +0000797void RNNoiseProcess::PitchFilter(FrameFeatures &features, vec1D32F &gain) {
Richard Burton00553462021-11-10 16:27:14 +0000798 std::vector<float> r(NB_BANDS, 0);
799 std::vector<float> rf(FREQ_SIZE, 0);
800 std::vector<float> newE(NB_BANDS);
801
802 for (size_t i = 0; i < NB_BANDS; i++) {
George Gekova2b0fc22021-11-08 16:30:43 +0000803 if (features.m_Exp[i] > gain[i]) {
Richard Burton00553462021-11-10 16:27:14 +0000804 r[i] = 1;
805 } else {
806
807
George Gekova2b0fc22021-11-08 16:30:43 +0000808 r[i] = std::pow(features.m_Exp[i], 2) * (1 - std::pow(gain[i], 2)) /
809 (.001 + std::pow(gain[i], 2) * (1 - std::pow(features.m_Exp[i], 2)));
Richard Burton00553462021-11-10 16:27:14 +0000810 }
811
812
813 r[i] = math::MathUtils::SqrtF32(std::min(1.0f, std::max(0.0f, r[i])));
814 r[i] *= math::MathUtils::SqrtF32(features.m_Ex[i] / (1e-8f + features.m_Ep[i]));
815 }
816
817 InterpBandGain(rf, r);
818 for (size_t i = 0; i < FREQ_SIZE - 1; i++) {
819 features.m_fftX[2 * i] += rf[i] * features.m_fftP[2 * i]; /* Real. */
820 features.m_fftX[2 * i + 1] += rf[i] * features.m_fftP[2 * i + 1]; /* Imaginary. */
821
822 }
823 ComputeBandEnergy(features.m_fftX, newE);
824 std::vector<float> norm(NB_BANDS);
825 std::vector<float> normf(FRAME_SIZE, 0);
826 for (size_t i = 0; i < NB_BANDS; i++) {
827 norm[i] = math::MathUtils::SqrtF32(features.m_Ex[i] / (1e-8f + newE[i]));
828 }
829
830 InterpBandGain(normf, norm);
831 for (size_t i = 0; i < FREQ_SIZE - 1; i++) {
832 features.m_fftX[2 * i] *= normf[i]; /* Real. */
833 features.m_fftX[2 * i + 1] *= normf[i]; /* Imaginary. */
834
835 }
836}
837
838void RNNoiseProcess::FrameSynthesis(vec1D32F& outFrame, vec1D32F& fftY) {
839 std::vector<float> x(WINDOW_SIZE, 0);
840 InverseTransform(x, fftY);
841 ApplyWindow(x);
842 for (size_t i = 0; i < FRAME_SIZE; i++) {
843 outFrame[i] = x[i] + m_synthesisMem[i];
844 }
845 memcpy((m_synthesisMem.data()), &x[FRAME_SIZE], FRAME_SIZE*sizeof(float));
846}
847
848void RNNoiseProcess::InterpBandGain(vec1D32F& g, vec1D32F& bandE) {
849 for (size_t i = 0; i < NB_BANDS - 1; i++) {
850 int bandSize = (m_eband5ms[i + 1] - m_eband5ms[i]) << FRAME_SIZE_SHIFT;
851 for (int j = 0; j < bandSize; j++) {
852 float frac = static_cast<float>(j) / bandSize;
853 g[(m_eband5ms[i] << FRAME_SIZE_SHIFT) + j] = (1 - frac) * bandE[i] + frac * bandE[i + 1];
854 }
855 }
856}
857
858void RNNoiseProcess::InverseTransform(vec1D32F& out, vec1D32F& fftXIn) {
859
860 std::vector<float> x(WINDOW_SIZE * 2); /* This is complex. */
861 vec1D32F newFFT; /* This is complex. */
862
863 size_t i;
864 for (i = 0; i < FREQ_SIZE * 2; i++) {
865 x[i] = fftXIn[i];
866 }
867 for (i = FREQ_SIZE; i < WINDOW_SIZE; i++) {
868 x[2 * i] = x[2 * (WINDOW_SIZE - i)]; /* Real. */
869 x[2 * i + 1] = -x[2 * (WINDOW_SIZE - i) + 1]; /* Imaginary. */
870 }
871
872 constexpr uint32_t numFFt = 2 * FRAME_SIZE;
Kshitij Sisodia661959c2021-11-24 10:39:52 +0000873 static_assert(numFFt != 0, "numFFt cannot be 0!");
Richard Burton00553462021-11-10 16:27:14 +0000874
875 vec1D32F fftOut = vec1D32F(x.size(), 0);
876 math::MathUtils::FftF32(x,fftOut, m_fftInstCmplx);
877
878 /* Normalize. */
879 for (auto &f: fftOut) {
880 f /= numFFt;
881 }
882
883 out[0] = WINDOW_SIZE * fftOut[0]; /* Real. */
884 for (i = 1; i < WINDOW_SIZE; i++) {
885 out[i] = WINDOW_SIZE * fftOut[(WINDOW_SIZE * 2) - (2 * i)]; /* Real. */
886 }
887}
888
889
890} /* namespace rnn */
891} /* namespace app */
892} /* namspace arm */