From 0e7099244cc4044eca9e20856291bd01ddda8a58 Mon Sep 17 00:00:00 2001 From: "aluebs@webrtc.org" Date: Tue, 21 Oct 2014 22:14:10 +0000 Subject: [PATCH] Break out WebRtcNs_Energy function in ns_core This is done in order to make the code more readible and maintainable. This generates bit-exact output. BUG=webrtc:3811 R=bjornv@webrtc.org, kwiberg@webrtc.org Review URL: https://webrtc-codereview.appspot.com/23099004 git-svn-id: http://webrtc.googlecode.com/svn/trunk@7487 4adac7df-926f-26a2-2b94-8c16560cd09d --- webrtc/modules/audio_processing/ns/ns_core.c | 27 ++++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/webrtc/modules/audio_processing/ns/ns_core.c b/webrtc/modules/audio_processing/ns/ns_core.c index daeb134dfd..64af5e9917 100644 --- a/webrtc/modules/audio_processing/ns/ns_core.c +++ b/webrtc/modules/audio_processing/ns/ns_core.c @@ -811,6 +811,22 @@ static void IFFT(NSinst_t* const self, } } +// Calculates the energy of a buffer. +// Inputs: +// * |buffer| is the buffer over which the energy is calculated. +// * |length| is the length of the buffer. +// Returns the calculated energy. +static float Energy(const float* buffer, int length) { + int i; + float energy = 0.f; + + for (i = 0; i < length; ++i) { + energy += buffer[i] * buffer[i]; + } + + return energy; +} + int WebRtcNs_AnalyzeCore(NSinst_t* inst, float* speechFrame) { int i; const int kStartBand = 5; // Skip first frequency bins during estimation. @@ -845,11 +861,10 @@ int WebRtcNs_AnalyzeCore(NSinst_t* inst, float* speechFrame) { UpdateBuffer(speechFrame, inst->blockLen, inst->anaLen, inst->analyzeBuf); // windowing - energy = 0.0; for (i = 0; i < inst->anaLen; i++) { winData[i] = inst->window[i] * inst->analyzeBuf[i]; - energy += winData[i] * winData[i]; } + energy = Energy(winData, inst->anaLen); if (energy == 0.0) { // we want to avoid updating statistics in this case: // Updating feature statistics when we have zeros only will cause @@ -1129,11 +1144,10 @@ int WebRtcNs_ProcessCore(NSinst_t* inst, } // windowing - energy1 = 0.0; for (i = 0; i < inst->anaLen; i++) { winData[i] = inst->window[i] * inst->dataBuf[i]; - energy1 += winData[i] * winData[i]; } + energy1 = Energy(winData, inst->anaLen); if (energy1 == 0.0) { // synthesize the special case of zero input // read out fully processed segment @@ -1245,10 +1259,7 @@ int WebRtcNs_ProcessCore(NSinst_t* inst, factor1 = 1.f; factor2 = 1.f; - energy2 = 0.0; - for (i = 0; i < inst->anaLen; i++) { - energy2 += winData[i] * winData[i]; - } + energy2 = Energy(winData, inst->anaLen); gain = (float)sqrt(energy2 / (energy1 + 1.f)); // scaling for new version