From 4a20f0ead2b5da402f4794a3f573c2b18e94b05d Mon Sep 17 00:00:00 2001 From: pammyleong Date: Wed, 26 Feb 2025 14:49:04 +0800 Subject: [PATCH] Update InputFFT.ino - Increase sensitivity by adding a gain factor for audio buffer - Add constrain to audio buffer to prevent overflow and loss of data --- .../libraries/AudioCodec/examples/InputFFT/InputFFT.ino | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Arduino_package/hardware/libraries/AudioCodec/examples/InputFFT/InputFFT.ino b/Arduino_package/hardware/libraries/AudioCodec/examples/InputFFT/InputFFT.ino index 98a9de50..6beade3b 100644 --- a/Arduino_package/hardware/libraries/AudioCodec/examples/InputFFT/InputFFT.ino +++ b/Arduino_package/hardware/libraries/AudioCodec/examples/InputFFT/InputFFT.ino @@ -12,11 +12,13 @@ #define SAMPLERATE 16000 #define SAMPLECOUNT 128 +#define GAIN_FACTOR 1.0 // Gain factor (can be adjusted), Cannot be 0 as it will mute the signal int16_t audio_buffer[SAMPLECOUNT] = {0}; float fft_buffer[SAMPLECOUNT/2] = {0}; uint16_t freq_bins[SAMPLECOUNT/2] = {0}; int i = 0; +int audioBufferMaxValue = INT16_MAX; // 16-bit signed audio data (Refer to int16_t audio_buffer[SAMPLECOUNT]) FFT fft; @@ -38,6 +40,10 @@ void setup() { void loop() { if (Codec.readAvaliable()) { Codec.readDataPage(audio_buffer, SAMPLECOUNT); // read latest received data from buffer + for (int i = 0; i < SAMPLECOUNT; i++) { + audio_buffer[i] *= constrain(audio_buffer[i] * GAIN_FACTOR, -audioBufferMaxValue, audioBufferMaxValue); // Clamping to avoid overflow and amplify each sample + } + fft.calculate(audio_buffer, fft_buffer, SAMPLECOUNT); for (i = 0; i < (SAMPLECOUNT/2); i++) { if (fft_buffer[i] > 0.01) {