Multiple setup and loop to configure I2S for various spectrums

Hi,

I'm using Arduino MKR1000 and Adafruit SPH0645 I2S MEMS microphone breakout to analyze sound.

Since Arduino is not capable to calculate real time FFT with a certain precision, I want to assign different FFTsize for various frequency intervals. There is a ArduinoSound library exist which complete FFT operations, but when I choose 12kHz (or more) sample rate and 128 FFTsize, Arduino stops calculating. I'm not sure it is related with out of memory or processor speed.

I have to analyze until 15kHz (so 30 kHz sample rate), but I do not want only one FFTsize.

Until 1kHz, I want to analyze every 100 hertz (or different but close could be 120-240 etc) Between 1kHz 10kHz, I want to analyze every 500 hertz (or same with above 600 Hz etc) And finally until 15 Khz, I want to analyze every 800 hertz (or 1200 Hz etc)

So I want to get approximately these frequencies (do not stuck with the numbers):

120-240-360-480-600-720-840-960-1400-1900-2300-2700-3100-3500-3900-4300-4700-5100-5500-5900-6300-6700-7100-7500-7900-8300-8700-9100-9500-9900-10800-11800-12800-13800-14800

Between 0-1kHz FFTsize = 16

Between 1-10kHz FFTsize = 32 (or 64)

Between 10-15kHz FFTsize = 8 (or 16)

My initial code is:

#include 

// sample rate for the input
const int sampleRate = 8000;

// size of the FFT to compute
const int fftSize = 128;

// size of the spectrum output, half of FFT size
const int spectrumSize = fftSize / 2;

// array to store spectrum output
int spectrum[spectrumSize];

// create an FFT analyzer to be used with the I2S input
FFTAnalyzer fftAnalyzer(fftSize);

void setup() {
// Open serial communications and wait for port to open:
  // A baud rate of 115200 is used instead of 9600 for a faster data rate
  // on non-native USB ports
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // setup the I2S audio input for the sample rate with 32-bits per sample
  if (!AudioInI2S.begin(sampleRate, 32)) {
    Serial.println("Failed to initialize I2S input!");
    while (1); // do nothing
  }

  // configure the I2S input as the input for the FFT analyzer
  if (!fftAnalyzer.input(AudioInI2S)) {
    Serial.println("Failed to set FFT analyzer input!");
    while (1); // do nothing
  }
}

void loop() {
  // check if a new analysis is available
  if (fftAnalyzer.available()) {
    // read the new spectrum
    fftAnalyzer.read(spectrum, spectrumSize);

    // print out the spectrum
    for (int i = 0; i < spectrumSize; i++) {
      Serial.print((i * sampleRate) / fftSize); // the starting frequency
      Serial.print("\t"); // 
      Serial.println(spectrum[i]); // the spectrum value
    }
  }
}

As you see in void setup() part, the code is configuring the I2S sample rate by taking the value at line 4. But I want to use 3 different sampleRate and fftSize. If you need more clearification, please ask. I thought I can solve this issue with 3 different setup and loop, but I do not know how to configure my code to achieve that. If you also show how each sampleRate can be only valid for determined interval (like 1000-10000), I will also reduce the computation. Because sampleRate 2000 will not ignore first 1000Hz according to my code.

I also uploaded my modified code, which is wrong but explains the mechanism. I need to divide it into multiple sections. I know I can do it with 3 different code and Arduino, so how can I do it with only one.

#include 

// sample rate for the input
const int sampleRate1 = 2000;
const int sampleRate2 = 20000;
const int sampleRate3 = 30000;

// size of the FFT to compute
const int fftSize1 = 32;
const int fftSize2 = 64;
const int fftSize3 = 16;

// size of the spectrum output, half of FFT size
const int spectrumSize1 = fftSize1 / 2;
const int spectrumSize2 = fftSize2 / 2;
const int spectrumSize3 = fftSize3 / 2;

// array to store spectrum output
int spectrum1[spectrumSize1];
int spectrum2[spectrumSize2];
int spectrum3[spectrumSize3];

// create an FFT analyzer to be used with the I2S input
FFTAnalyzer fftAnalyzer1(fftSize1);
FFTAnalyzer fftAnalyzer2(fftSize2);
FFTAnalyzer fftAnalyzer3(fftSize3);

void setup() {
// Open serial communications and wait for port to open:
  // A baud rate of 115200 is used instead of 9600 for a faster data rate
  // on non-native USB ports
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // setup the I2S audio input for the sample rate with 32-bits per sample
  if (!AudioInI2S.begin(sampleRate1, 32)) {
    Serial.println("Failed to initialize I2S input!");
    while (1); // do nothing
  }
    if (!AudioInI2S.begin(sampleRate2, 32)) {
    Serial.println("Failed to initialize I2S input!");
    while (1); // do nothing
  }
    if (!AudioInI2S.begin(sampleRate3, 32)) {
    Serial.println("Failed to initialize I2S input!");
    while (1); // do nothing
  }

  // configure the I2S input as the input for the FFT analyzer
  if (!fftAnalyzer1.input(AudioInI2S)) {
    Serial.println("Failed to set FFT analyzer input!");
    while (1); // do nothing
  }
    if (!fftAnalyzer2.input(AudioInI2S)) {
    Serial.println("Failed to set FFT analyzer input!");
    while (1); // do nothing
  }
    if (!fftAnalyzer3.input(AudioInI2S)) {
    Serial.println("Failed to set FFT analyzer input!");
    while (1); // do nothing
  }
}

void loop() {
  {
  // check if a new analysis is available 
  if (fftAnalyzer1.available()) {
    // read the new spectrum
    fftAnalyzer1.read(spectrum1, spectrumSize1); }
      // check if a new analysis is available
  if (fftAnalyzer2.available()) {
    // read the new spectrum
    fftAnalyzer2.read(spectrum2, spectrumSize2); }
      // check if a new analysis is available
  if (fftAnalyzer3.available()) {
    // read the new spectrum
    fftAnalyzer3.read(spectrum3, spectrumSize3); }

    // print out the spectrum
    for (int i = 0; i < spectrumSize1; i++) {
      Serial.print((i * sampleRate1) / fftSize1); // the starting frequency
      Serial.print("\t"); // 
      Serial.println(spectrum1[i]); // the spectrum value
    }
        for (int i = 0; i < spectrumSize2; i++) {
      Serial.print((i * sampleRate2) / fftSize2); // the starting frequency
      Serial.print("\t"); // 
      Serial.println(spectrum2[i]); // the spectrum value
    }
        for (int i = 0; i < spectrumSize3; i++) {
      Serial.print((i * sampleRate3) / fftSize3); // the starting frequency
      Serial.print("\t"); // 
      Serial.println(spectrum3[i]); // the spectrum value
    }
  }
}

Why have you started a new thread for the same problem. >:(

http://forum.arduino.cc/index.php?topic=575486.msg3919760#msg3919760

Grumpy_Mike: Why have you started a new thread for the same problem. >:(

http://forum.arduino.cc/index.php?topic=575486.msg3919760#msg3919760

Project is the same but my problem is different here, I want to make 3 setups and 3 loops. Other post was about matching the bits to using the microphone with ArduinoSound library .