Arduino FFT

Hi I was trying to do Arduino FFT using an Arduino and a MAX4466 Electret Microphone Amplifier Adjustable Gain.

I'm trying the following code but I keep getting random numbers can somebody help me to fix this code and correctly do FFT. By the way i'm new to Arduino so please give me tips as well. Thanks.

#include "arduinoFFT.h"

#define SAMPLES 128 //Must be a power of 2
#define SAMPLING_FREQUENCY 1000 //Hz, must be less than 10000 due to ADC

arduinoFFT FFT = arduinoFFT();

unsigned int sampling_period_us;
unsigned long microseconds;

double vReal[SAMPLES];
double vImag[SAMPLES];

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

sampling_period_us = round(1000000*(1.0/SAMPLING_FREQUENCY));
}

void loop() {

/SAMPLING/
for(int i=0; i<SAMPLES; i++)
{
microseconds = micros(); //Overflows after around 70 minutes!

vReal = analogRead(0);
_ vImag = 0;_

* while(micros() < (microseconds + sampling_period_us)){
_
}_
_
}*_

_ /FFT/
* FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD);
_
FFT.ComplexToMagnitude(vReal, vImag, SAMPLES);*

* double peak = FFT.MajorPeak(vReal, SAMPLES, SAMPLING_FREQUENCY);*

_ /PRINT RESULTS/_
* //Serial.println(peak); //Print out what frequency is the most dominant.*

* for(int i=0; i<(SAMPLES/2); i++)*
* {*
_ /View all these three lines in serial terminal to see which frequencies has which amplitudes/_

//Serial.print((i * 1.0 * SAMPLING_FREQUENCY) / SAMPLES, 1);
* //Serial.print(" ");*
_ Serial.println(vReal*, 1); //View only this line in serial plotter to visualize the bins*
* }*_

* //delay(1000); //Repeat the process every second OR:*
* while(1); //Run code once*
}

I took this from: FFT I would take another look, I do not think the Arduino can process the math fast enough for what you want to do in real time or did I miss something.
In the first step, a section of the signal is scanned and stored in the memory for further processing. Two parameters are relevant:
The sampling rate or sampling frequency fs of the measuring system (e.g. 48 kHz). This is the average number of samples obtained in one second (samples per second).
The selected number of samples; the blocklength BL. This is always an integer power to the base 2 in the FFT (e.g., 2^10 = 1024 samples) From the two basic parameters fs and BL, further parameters of the measurement can be determined.
Bandwidth fn (= Nyquist frequency). This value indicates the theoretical maximum frequency that can be determined by the FFT.
fn = fs / 2
For example at a sampling rate of 48 kHz, frequency components up to 24 kHz can be theoretically determined. In the case of an analog system, the practically achievable value is usually somewhat below this, due to analog filters - e.g. at 20 kHz.
Measurement duration D. The measurement duration is given by the sampling rate fs and the blocklength BL.
D = BL / fs.
At fs = 48 kHz and BL = 1024, this yields 1024/48000 Hz = 21.33 ms
Frequency resolution df. The frequency resolution indicates the frequency spacing between two measurement results.
df = fs / BL
At fs = 48 kHz and BL = 1024, this gives a df of 48000 Hz / 1024 = 46.88 Hz.
In practice, the sampling frequency fs is usually a variable given by the system. However, by selecting the blocklength BL, the measurement duration and frequency resolution can be defined. The following applies:
A small blocklength results in fast measurement repetitions with a coarse frequency resolution.
A large blocklength results in slower measuring repetitions with fine frequency resolution.
I like Google! This response is to help you get started in solving your problem, not solve it for you.
Good Luck & Have Fun!
Gil

Please use code tags when posting code, as described in the How to use this forum post.

The example code has either been edited so that it can't possibly work, or your failure to use code tags resulted in an apparent coding error.

This is not the way data are stored in an array:

        vReal = analogRead(0);

I suggest to start over and study the unmodified ArduinoFFT examples.

    sampling_period_us = round(1000000*(1.0/SAMPLING_FREQUENCY));

1000000 isn't a valid integer constant for an 8 bit Arduino. Force the constant to be long, thus:

   sampling_period_us = round(1000000L*(1.0/SAMPLING_FREQUENCY));

Sometimes it will matter, sometimes it won't, just always do it when the value is not representable as a 16 bit int so you don't get caught out.

Of course here you could use a float constant directly:

   sampling_period_us = round(1e6 / SAMPLING_FREQUENCY);