Skip to content

Commit 4a20f0e

Browse files
committed
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
1 parent bea859f commit 4a20f0e

File tree

1 file changed

+6
-0
lines changed
  • Arduino_package/hardware/libraries/AudioCodec/examples/InputFFT

1 file changed

+6
-0
lines changed

Arduino_package/hardware/libraries/AudioCodec/examples/InputFFT/InputFFT.ino

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212

1313
#define SAMPLERATE 16000
1414
#define SAMPLECOUNT 128
15+
#define GAIN_FACTOR 1.0 // Gain factor (can be adjusted), Cannot be 0 as it will mute the signal
1516

1617
int16_t audio_buffer[SAMPLECOUNT] = {0};
1718
float fft_buffer[SAMPLECOUNT/2] = {0};
1819
uint16_t freq_bins[SAMPLECOUNT/2] = {0};
1920
int i = 0;
21+
int audioBufferMaxValue = INT16_MAX; // 16-bit signed audio data (Refer to int16_t audio_buffer[SAMPLECOUNT])
2022

2123
FFT fft;
2224

@@ -38,6 +40,10 @@ void setup() {
3840
void loop() {
3941
if (Codec.readAvaliable()) {
4042
Codec.readDataPage(audio_buffer, SAMPLECOUNT); // read latest received data from buffer
43+
for (int i = 0; i < SAMPLECOUNT; i++) {
44+
audio_buffer[i] *= constrain(audio_buffer[i] * GAIN_FACTOR, -audioBufferMaxValue, audioBufferMaxValue); // Clamping to avoid overflow and amplify each sample
45+
}
46+
4147
fft.calculate(audio_buffer, fft_buffer, SAMPLECOUNT);
4248
for (i = 0; i < (SAMPLECOUNT/2); i++) {
4349
if (fft_buffer[i] > 0.01) {

0 commit comments

Comments
 (0)