From 70bfbd5a4334c3e96b6fdd973f732b469e65c1ec Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Wed, 10 Apr 2024 13:42:21 +0200 Subject: [PATCH] ar_energy: fix a math error fixing mis-optimized math when calculating energy. energy = sum(amplitude^2). this is not the same as sum(amplitude)^2. Example: 1+5+7 = 13; 13 * 13 = 169 1*1 + 5*5 + 7*7 = 75 --- .../usermod_v2_auto_playlist/usermod_v2_auto_playlist.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/usermods/usermod_v2_auto_playlist/usermod_v2_auto_playlist.h b/usermods/usermod_v2_auto_playlist/usermod_v2_auto_playlist.h index 83407e5f..56ed93c2 100644 --- a/usermods/usermod_v2_auto_playlist/usermod_v2_auto_playlist.h +++ b/usermods/usermod_v2_auto_playlist/usermod_v2_auto_playlist.h @@ -15,7 +15,7 @@ class AutoPlaylistUsermod : public Usermod { #if 0 // experimental parameters by softhack007 - more balanced but need testing const uint_fast32_t MAX_DISTANCE_TRACKER = 184; // maximum accepted distance_tracker - const uint_fast32_t ENERGY_SCALE = 24000; + const uint_fast32_t ENERGY_SCALE = 14000; const float FILTER_SLOW1 = 0.0075f; // for "slow" energy const float FILTER_SLOW2 = 0.005f; // for "slow" lfc / zcr const float FILTER_FAST1 = 0.2f; // for "fast" energy @@ -112,10 +112,10 @@ class AutoPlaylistUsermod : public Usermod { energy = 0; for (int i=0; i < NUM_GEQ_CHANNELS; i++) { - energy += fftResult[i]; + uint_fast32_t amplitude = fftResult[i]; + energy += amplitude * amplitude; } - energy *= energy; energy /= ENERGY_SCALE; // scale down so we get 0 sometimes uint8_t lfc = fftResult[0];