Analyse the Spectrum with ArduinoFFT

1. The ISD1820 is microphone module, the analogue value shall be turned into digital value for processing purpose. 

Pin A0 is set as INPUT, fetching data form ISD1802.

int inputPin = A0;

The following codes do the job with adcTune function, the analogueRead(A0) pins is set for sound wave catching.

void adcTune(double *vReal,double *vImag ,int *readings)
  // read from the sensor AND advance to the next position in the array:
  //for (int  readIndex= 0; readIndex < numReadings; readIndex++) {    readings[readIndex] = analogRead(inputPin);  }
  for (int  readIndex= 0; readIndex < numReadings; readIndex++){
    delay(CYCLETIME); // delay fpr cycle
    readings[readIndex] = analogRead(inputPin); 
      for (int i = 0; i < numReadings; i++) {
      vReal[i] = readings[i] ;/* Build data with positive and negative values*/
      vImag[i] = 0.0; 


2. The easiest solution to process the digital data is by FFT algorithm. There would be considerable amount of calculation for fast process, especially for low performance Cortex-M0 core. The arduinoFFT library can handle the calculation in short time. There have been many technique in fast calculation on Complex data, and the result is good.

In this test, only peak frequency is printed. In fact , the spectrum is available for further encryption. 

In function fftTune(), there are three sub-steps like FFT.windowing(), FFT.Compute(), FFT.ComplexToMagnitude(). After that, FFT.MajorPeak() can pick the peak frequency within the spectrum.

void fftTune(double *vReal,double *vImag, uint16_t samples)
         FFT.Windowing(vReal, samples, FFT_WIN_TYP_HAMMING, FFT_FORWARD);  /* Weigh data */
        FFT.Compute(vReal, vImag, samples, FFT_FORWARD); /* Compute FFT */
        FFT.ComplexToMagnitude(vReal, vImag, samples); /* Compute magnitudes */
        Serial.println("Computed magnitudes:");
        PrintVector(vReal, (samples >> 1), SCL_FREQUENCY);
        double x = FFT.MajorPeak(vReal, samples, SAMPLINGFREQUENCY);