In my Arduino Audio Tools library I am providing a common API to do FFT against different implementations. I was already testing some of the implementations.

On ARM microcontrollers we can use the CMSIS DSP library which usually comes automatically with the Arduino Core. This is not the case for the Arduino UNO R4, but we can install the Arduino CMSIS DSP library with the Arduino library manager.

Here is the adapted test sketch that generates a sine tone to be analysed by FFT: I just needed to reduce the length parameter from 4k to 1k because of the limited available RAM.

Example Sketch

#include "AudioTools.h"
#include "AudioLibs/AudioCmsisFFT.h" // using CMSIS DSP

AudioCmsisFFT fft; // or AudioKissFFT
SineWaveGenerator<int16_t> sineWave(32000);
GeneratedSoundStream<int16_t> in(sineWave);
StreamCopy copier(fft, in);
AudioInfo info(8000, 1, 16);
float value = 0;

// display fft result
void fftResult(AudioFFTBase &fft) {
  float diff;
  auto result = fft.result();
  if (result.magnitude > 100) {
    Serial.print(result.frequency);
    Serial.print(" ");
    Serial.print(result.magnitude);
    Serial.print(" => ");
    Serial.print(result.frequencyAsNote(diff));
    Serial.print(" diff: ");
    Serial.print(diff);
    Serial.print(" - time ms ");
    Serial.print(fft.resultTime() - fft.resultTimeBegin());
    Serial.println();
  }
}

void setup() {
  Serial.begin(115200);
  AudioLogger::instance().begin(Serial, AudioLogger::Warning);

  while(!Serial);

  // set the frequency
  sineWave.setFrequency(N_B4);

  // Setup sine wave
  auto cfg = in.defaultConfig();
  cfg.copyFrom(info);
  in.begin(cfg);

  // Setup FFT
  auto tcfg = fft.defaultConfig();
  tcfg.copyFrom(info);
  tcfg.length = 1024;
  tcfg.callback = &fftResult;
  fft.begin(tcfg);
}

void loop() { copier.copy(); }

Dependencies

You need to install the following libraries:

Here you can also find some of my other posts related to the Arduino Uno R4

Categories: ArduinoMachine Sound

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *