From 610149af42eecbe95413a80e3be5627914e4830b Mon Sep 17 00:00:00 2001 From: Adrien Michel Date: Sun, 1 Dec 2024 03:10:07 +0100 Subject: [PATCH] phase_sync : find peak and average at the same time (lower complexity) --- src/backend/phasereference.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/backend/phasereference.cpp b/src/backend/phasereference.cpp index e87fdd27..f868c07c 100644 --- a/src/backend/phasereference.cpp +++ b/src/backend/phasereference.cpp @@ -97,14 +97,13 @@ int32_t PhaseReference::findIndex(DSPCOMPLEX *v, const float threshold = 3; /** - * We compute the average signal value ... + * We compute the average signal value + * and find the peak value */ - for (size_t i = 0; i < Tu; i++) - sum += abs(res_buffer[i]); - DSPFLOAT max = -10000; for (size_t i = 0; i < Tu; i++) { const float value = abs(res_buffer[i]); + sum += value; impulseResponseBuffer[i] = value; if (value > max) { @@ -112,6 +111,9 @@ int32_t PhaseReference::findIndex(DSPCOMPLEX *v, max = value; } } + /* avoid zero divide in case signal v is 0 */ + if(sum==0) + return -1; /** * that gives us a basis for defining the threshold */