Skip to content

Update InputFFT.ino #265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 26, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand Down