Skip to content

Commit b27686b

Browse files
authored
audioreactive small optimization
* clear vImag[] using memset * zerocrossing detection: directly check sign bit
1 parent 164c9a1 commit b27686b

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

usermods/audioreactive/audio_reactive.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <driver/i2s.h>
88
#include <driver/adc.h>
99

10+
#include <math.h>
1011
#endif
1112

1213
#if defined(ARDUINO_ARCH_ESP32) && (defined(WLED_DEBUG) || defined(SR_DEBUG))
@@ -545,22 +546,22 @@ void FFTcode(void * parameter)
545546
}
546547
}
547548

549+
// set imaginary parts to 0
550+
memset(vImag, 0, sizeof(vImag));
551+
548552
// find highest sample in the batch, and count zero crossings
549553
float maxSample = 0.0f; // max sample from FFT batch
550554
uint_fast16_t newZeroCrossingCount = 0;
551555
for (int i=0; i < samplesFFT; i++) {
552-
// set imaginary parts to 0
553-
vImag[i] = 0;
554556
// pick our our current mic sample - we take the max value from all samples that go into FFT
555557
if ((vReal[i] <= (INT16_MAX - 1024)) && (vReal[i] >= (INT16_MIN + 1024))) //skip extreme values - normally these are artefacts
556558
if (fabsf((float)vReal[i]) > maxSample) maxSample = fabsf((float)vReal[i]);
557559

558560
// WLED-MM/TroyHacks: Calculate zero crossings
559561
//
560562
if (i < (samplesFFT-1)) {
561-
if((vReal[i] >= 0 && vReal[i+1] < 0) || (vReal[i] < 0 && vReal[i+1] >= 0)) {
563+
if (__builtin_signbit(vReal[i]) != __builtin_signbit(vReal[i+1])) // test sign bit: sign changed -> zero crossing
562564
newZeroCrossingCount++;
563-
}
564565
}
565566
}
566567
newZeroCrossingCount = (newZeroCrossingCount*2)/3; // reduce value so it typicially stays below 256

0 commit comments

Comments
 (0)