Performing FFT on audio data received from A2DP (esp32)

809 views Asked by At

So far i have tried using some code snippets from https://github.com/thingpulse/esp32-icon64-a2dp but to no avail, the values are in the range of tens of millions. (I am interested in the amplitude of each frequency)

code:

#define SAMPLES 512

double vReal[SAMPLES];
double vImag[SAMPLES];
int16_t sample_l_int;
int16_t sample_r_int;

void read_data_stream(const uint8_t *data, uint32_t length) {

  int byteOffset = 0;
  for (int i = 0; i < SAMPLES; i++) {
    sample_l_int = (int16_t)(((*(data + byteOffset + 1) << 8) | *(data + byteOffset)));
    sample_r_int = (int16_t)(((*(data + byteOffset + 3) << 8) | *(data + byteOffset + 2)));
    vReal[i] = (sample_l_int + sample_r_int) / 2.0f;
    vImag[i] = 0;
    byteOffset = byteOffset + 4;
  }

  FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
  FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD);
  FFT.ComplexToMagnitude(vReal, vImag, SAMPLES);

  for (int i = 2; i < (SAMPLES / 2); i++) {
    if (vReal[i] > 2000) { // Add a crude noise filter
      Serial.println(vReal[i]);
    }
  }
}

void setup() {
  Serial.begin(115200);
  a2dp_sink.start(DEVICE_NAME);
  a2dp_sink.set_stream_reader(read_data_stream);
}

void loop() {
}

plot results: https://i.stack.imgur.com/4Fb3F.jpg

I used 1k sine wave for testing purposes, as seen in the plot, the peaks gradually rise in intensity which seems correct, as the audio transmitted fades in.

note: the data length varies between devices, in this case it's 2560, moreover as you can see the SAMPLES define is 512, which as i understand lowers the FFTs resolution (?)

0

There are 0 answers