Calculate amplitude of a signal after application FFT

I'm using Arduino Due and sensor for noise and I applied FFT library to extract frequency and it's working perfectly. But I don't know how to calculate the amplitudes and print them on console?

Here is the code:

#include "arduinoFFT.h"

#define SAMPLES 32             
#define SAMPLING_FREQUENCY 1000 

arduinoFFT FFT = arduinoFFT();

unsigned int sampling_period_us;
unsigned long microseconds;

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

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

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

void loop() {

    /*SAMPLING*/
    for(int i=0; i<SAMPLES; i++)
    {
        microseconds = micros();    

        vReal[i] = analogRead(0);
        vImag[i] = 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);


    Serial.println(peak);     

    for(int i=0; i<(SAMPLES/2); i++)
    {


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

    delay(1000);  
    while(1);       
}
arduinofftarduino-due

Answers

answered 2 months ago Nitro #1

The serial plotter does not have a number of sample to plot functionality.

If you want to view the amplitudes for each window correctly, you will need to use the following code

for(int i=0; i<(SAMPLES/2); i++)
{
//  Serial.print((i * 1.0 * SAMPLING_FREQUENCY) / SAMPLES, 1);
    for(int i=0; i<20; i++)//Adjust the 20 to make space between each samples.
        Serial.print(0);
    Serial.print(vReal[i]); 
}

And then adjust the size of the serial plotter to fit your window.

I hope this helps.

comments powered by Disqus