I'm trying to compute the dominant frequency from a note played on my bass guitar i.e. i expect to get 110hz when playing the A string. The majorPeak() function outputs weird numbers like 2.39 or other low numbers that seems almost random. I copied most of the code from a tutorial (might've been arduinoFFT docs):
#include <arduinoFFT.h>
const uint16_t samples = 128; //This value MUST ALWAYS be a power of 2
const float samplingFrequency = 2048;
unsigned int samplingPeriod;
unsigned long microSeconds;
double vReal[samples];
double vImag[samples];
ArduinoFFT<double> FFT = ArduinoFFT<double>(vReal, vImag, samples, samplingFrequency); /* Create FFT object */
void setup() {
Serial.begin(115200);
samplingPeriod = round(1000000*(1.0/samplingFrequency));
}
void loop() {
for(int i=0; i<samples; i++){
microSeconds = micros();
vReal[i] = analogRead(A0);
vImag[i] = 0;
Serial.print(vReal[i]);
Serial.print(" ");
Serial.println(i);
while(micros() < (microSeconds + samplingPeriod)){/*do nothing*/}
}
// Get samples
FFT.windowing(vReal, samples, FFTWindow::Hamming, FFTDirection::Forward); /* Weigh data */
FFT.compute(vReal, vImag, samples, FFTDirection::Forward); /* Compute FFT */
FFT.complexToMagnitude(); /* Compute magnitudes */
double x = FFT.majorPeak();
Serial.println(x);
while(1);
}
I'd appreciate any help.