Hello,
I'm trying to analyze frequencies of a sound played by a DFPlayer module but I do not get expected results.
The DFPlayer works well and I can hear the sound playing.
I connected SPK1 output directly to A0 (I read it was a non-inverted mono sum with half-supply DC so no negative values) and I tried connecting different pins to the AREF : SPK2, 3.3v, 5v but results were always bad.
I play single frequency sinus wave sound to test my circuit : 5 differents wav files : 200Hz, 400Hz, 500Hz, 600Hz and 800Hz.
Results are wrong for each of them.
Here are some results for 500 Hz: upper line magnitude, bottom line frequencies
Computed magnitudes:
159 | 174 | 40 | 26 | 9 | 29 | 26 | 76 | 42 | 38 | 11 | 46 | 27 | 9 | 29 | 39 | 8 | 14 | 16 | 44 | 17 | 21 | 40 | 24 | 17 | 6 | 21 | 9 | 11 | 17 | 19 | 25 |
0 | 62 | 125 | 187 | 250 | 312 | 375 | 437 | 500 | 562 | 625 | 687 | 750 | 812 | 875 | 937 | 1000 | 1062 | 1125 | 1187 | 1250 | 1312 | 1375 | 1437 | 1500 | 1562 | 1625 | 1687 | 1750 | 1812 | 1875 | 1937 |
=====
Peak : 38.258132
For 800Hz
Computed magnitudes:
212 | 300 | 236 | 59 | 34 | 83 | 145 | 197 | 157 | 61 | 14 | 33 | 31 | 14 | 14 | 3 | 11 | 24 | 51 | 50 | 19 | 30 | 30 | 14 | 4 | 20 | 29 | 26 | 20 | 40 | 54 | 48 |
0 | 62 | 125 | 187 | 250 | 312 | 375 | 437 | 500 | 562 | 625 | 687 | 750 | 812 | 875 | 937 | 1000 | 1062 | 1125 | 1187 | 1250 | 1312 | 1375 | 1437 | 1500 | 1562 | 1625 | 1687 | 1750 | 1812 | 1875 | 1937 |
=====
Peak : 68.341506
==============================
And for 200Hz
Computed magnitudes:
415 | 805 | 164 | 87 | 82 | 34 | 153 | 267 | 248 | 33 | 29 | 37 | 43 | 66 | 26 | 86 | 53 | 67 | 60 | 25 | 24 | 41 | 32 | 20 | 44 | 76 | 81 | 81 | 37 | 38 | 65 | 42 |
0 | 62 | 125 | 187 | 250 | 312 | 375 | 437 | 500 | 562 | 625 | 687 | 750 | 812 | 875 | 937 | 1000 | 1062 | 1125 | 1187 | 1250 | 1312 | 1375 | 1437 | 1500 | 1562 | 1625 | 1687 | 1750 | 1812 | 1875 | 1937 |
=====
Peak : 55.758441
==============================
I tested the FFT with raw data that I generated with the sin function like in the arduinoFFT exemple. I get expected results so I think the FFT code is good and the problem comes from the audio signal but I don't see how, noise ? bad wiring ?
I tried with and without dcRemoval, I tried with and without windowing, it did not improve results.
Somehow modifying the volume has an impact but still even at maximum or minium I haven't convenient results.
Below is my code and a schema :
#include <arduinoFFT.h>
#include <SPI.h>
#define SAMPLES 64 //Must be a power of 2
#define xres 8 // Total number of columns in the display, must be <= SAMPLES/2
#define yres 8 // Total number of rows in the display
const uint16_t samples = SAMPLES;
double samplingFrequency = 4000;
void loop() {
// ++ Sampling
for (int
i = 0;
i < SAMPLES; i++) {
while (!(ADCSRA & 0x10))
; // wait for ADC to complete current conversion ie ADIF bit set
ADCSRA = 0b11110101; // clear ADIF bit so that ADC can do next operation (0xf5)
// int value = ADC - avgDC; // Read from ADC and subtract DC offset caused value
int value = ADC; // Read from ADC and subtract DC offset caused value
vReal[i] = value; // Copy to bins after compressing
vImag[i] = 0;
}
FFT.dcRemoval();
FFT.windowing(FFTWindow::Hamming, FFTDirection::Forward);
FFT.compute(FFTDirection::Forward);
FFT.complexToMagnitude();
Serial.println("Computed magnitudes:");
PrintVector(vReal, (samples >> 1), SCL_FREQUENCY);
double x = FFT.majorPeak();
Serial.println("=====");
Serial.print("Peak : ");
Serial.println(x, 6);
Serial.println("==============================");
Serial.println();
delay(1000);
}
Thank you for your help




