0
\$\begingroup\$

I'm trying to use the arduinoFFT library (https://github.com/kosme/arduinoFFT) with my ESP32. I made a basic program just to see if the FFT is done correctly. The problem is that I can't compile the code because it returns some errors.

This is my code:

#include <arduinoFFT.h>

#define SAMPLE_RATE 44100
#define FFT_SIZE 2048

arduinoFFT FFT;

double real[FFT_SIZE];
double imag[FFT_SIZE];
double magnitudes[FFT_SIZE / 2];

void setup() {
  Serial.begin(115200);
}

void loop() {
  // Générer un signal numérique (exemple : sinusoïde)
  double frequency = 1000; // Fréquence du signal en Hz
  double amplitude = 0.5; // Amplitude du signal (entre 0 et 1)
  double phase = 0.0; // Phase du signal (entre 0 et 2*PI)

  for (int i = 0; i < FFT_SIZE; i++) {
    double time = (double)i / SAMPLE_RATE;
    real[i] = amplitude * sin(2 * PI * frequency * time + phase);
    imag[i] = 0.0;
  }

  // Appliquer une fenêtre (rectangle)
  FFT.Windowing(real, FFT_SIZE, FFT_WIN_TYP_RECTANGLE);

  // Calculer la FFT
  FFT.Compute(real, imag);

  // Calculer les magnitudes
  FFT.ComplexToMagnitude(real, imag, magnitudes, FFT_SIZE);

  // Convertir en dB et afficher les valeurs
  for (int i = 0; i < FFT_SIZE / 2; i++) {
    double dB = 20 * log10(magnitudes[i]);
    Serial.println(dB);
  }

  delay(1000); // Attendre 1 seconde avant la prochaine itération
}

As you can see I created a class called FFT to call the functions from the arduinoFFT.h library but I get this error message:

error: no matching function for call to 'arduinoFFT::ComplexToMagnitude(double [2048], double [2048], double [1024], int)'

At first I thought it could work by changing the FFT with the real name of the library, so I used arduinoFFT.Windowing instead of FFT.Windowing but it is still not working. Do you have a solution?

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

The signature of ComplexToMagnitude you're trying to use is:

void ComplexToMagnitude(double *vReal, double *vImag, uint16_t samples);

This signature hasn't changed since the very first commit (20.07.2014), so I'm not sure where your code example comes from.

Change that line to:

FFT.ComplexToMagnitude(real, imag, FFT_SIZE);

The result will be written to real.


BTW, the function overload you're using is deprecated. You should be using void ComplexToMagnitude();.

void arduinoFFT::ComplexToMagnitude() {
  // vM is half the size of vReal and vImag
  for (uint16_t i = 0; i < this->_samples; i++) {
    this->_vReal[i] = sqrt(sq(this->_vReal[i]) + sq(this->_vImag[i]));
  }
}

void arduinoFFT::ComplexToMagnitude(double *vReal, double *vImag,
                                    uint16_t samples) {
// vM is half the size of vReal and vImag
#warning("This method is deprecated and may be removed on future revisions.")
  for (uint16_t i = 0; i < samples; i++) {
    vReal[i] = sqrt(sq(vReal[i]) + sq(vImag[i]));
  }
}
\$\endgroup\$
1
\$\begingroup\$

The problem isn't the name of the method. The problem is the signature - that is, the parameters that you give to the method.

Your code:

 FFT.ComplexToMagnitude(real, imag, magnitudes, FFT_SIZE);

Available methods in the arduinoFFT library:

void arduinoFFT::ComplexToMagnitude() {
  // vM is half the size of vReal and vImag
  for (uint16_t i = 0; i < this->_samples; i++) {
    this->_vReal[i] = sqrt(sq(this->_vReal[i]) + sq(this->_vImag[i]));
  }
}

void arduinoFFT::ComplexToMagnitude(double *vReal, double *vImag,
                                    uint16_t samples) {
// vM is half the size of vReal and vImag
#warning("This method is deprecated and may be removed on future revisions.")
  for (uint16_t i = 0; i < samples; i++) {
    vReal[i] = sqrt(sq(vReal[i]) + sq(vImag[i]));
  }
}

Your code wants to give the method an array for real and imaginary values and get the results back in a third array. There is no method in the arduinoFFT that accepts three arrays. The compiler can find methods with the correct name, but none of them accept the parameters you are trying to pass - that causes the compile error.

You can use the one that accepts two arrays. The magnitude is then in the "real" array.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.