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
diff --git a/webrtc/modules/audio_processing/ns/ns_core.c b/webrtc/modules/audio_processing/ns/ns_core.c
index daeb134..64af5e9 100644
--- a/webrtc/modules/audio_processing/ns/ns_core.c
+++ b/webrtc/modules/audio_processing/ns/ns_core.c
@@ -811,6 +811,22 @@
}
}
+// 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 @@
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 @@
}
// 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 @@
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