FFT Code Help

Hello! Im trying to use FFT on my Arduino Uno, using the arduinofft by kosme.
however when im using the serial plotter it will only show me a very short graph of the signal.
setting the code to run every 1 second will only result in showing the same thing every second.
Im putting a 1kHz sinus on the A0 port but it will only give me as shown in the Naamloos.jpg file.
does anybody have an idea why this is showing up like this?

Any help appreciated. Thanks.

*/

/*
  In this example, the Arduino simulates the sampling of a sinusoidal 1000 Hz
  signal with an amplitude of 100, sampled at 5000 Hz. Samples are stored
  inside the vReal array. The samples are windowed according to Hamming
  function. The FFT is computed using the windowed samples. Then the magnitudes
  of each of the frequencies that compose the signal are calculated. Finally,
  the frequency spectrum magnitudes are printed. If you use the Arduino IDE
  serial plotter, you will see a single spike corresponding to the 1000 Hz
  frecuency.
*/

#include "arduinoFFT.h"

arduinoFFT FFT = arduinoFFT(); /* Create FFT object */
/*
These values can be changed in order to evaluate the functions
*/
const uint16_t samples = 64; //This value MUST ALWAYS be a power of 2
const double signalFrequency = 1000;
const double samplingFrequency = 5000;
const uint8_t amplitude = 100;
/*
These are the input and output vectors
Input vectors receive computed results from FFT
*/
double vReal[samples];
double vImag[samples];

#define SCL_INDEX 0x00
#define SCL_TIME 0x01
#define SCL_FREQUENCY 0x02
#define SCL_PLOT 0x03

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

void loop()
{
  /* Build raw data */
  double cycles = (((samples-1) * signalFrequency) / samplingFrequency); //Number of signal cycles that the sampling will read
  for (uint16_t i = 0; i < samples; i++)
  {
    vReal[i] = analogRead(0);
    //int8_t((amplitude * (sin((i * (twoPi * cycles)) / samples))) / 2.0);/* Build data with positive and negative values*/
    //vReal[i] = uint8_t((amplitude * (sin((i * (twoPi * cycles)) / samples) + 1.0)) / 2.0);/* Build data displaced on the Y axis to include only positive values*/
    vImag[i] = 0.0; //Imaginary part must be zeroed in case of looping to avoid wrong calculations and overflows
  }
  FFT.Windowing(vReal, samples, FFT_WIN_TYP_HAMMING, FFT_FORWARD);	/* Weigh data */
  FFT.Compute(vReal, vImag, samples, FFT_FORWARD); /* Compute FFT */
  FFT.ComplexToMagnitude(vReal, vImag, samples); /* Compute magnitudes */
  PrintVector(vReal, samples>>1, SCL_PLOT);
  double x = FFT.MajorPeak(vReal, samples, samplingFrequency);
  while(1); /* Run Once */
  // delay(2000); /* Repeat after delay */
}

void PrintVector(double *vData, uint16_t bufferSize, uint8_t scaleType)
{
  for (uint16_t i = 0; i < bufferSize; i++)
  {
    double abscissa;
    /* Print abscissa value */
    switch (scaleType)
    {
      case SCL_INDEX:
        abscissa = (i * 1.0);
	      break;
      case SCL_TIME:
        abscissa = ((i * 1.0) / samplingFrequency);
        break;
      case SCL_FREQUENCY:
        abscissa = ((i * 1.0 * samplingFrequency) / samples);
	      break;
    }
    if(scaleType!=SCL_PLOT)
    {
      Serial.print(abscissa, 6);
      if(scaleType==SCL_FREQUENCY)
        Serial.print("Hz");
      Serial.print(" ");
    }
    Serial.println(vData[i], 4);
  }
  Serial.println();
}



What are you thinking it should look like?

The plot is correct.

To understand what it is telling you requires you to know and understand the relationship between the sample frequency and the input signal, which incidentally has a DC component (at zero frequency).

Do not connect an AC signal generator directly to the analog input, as that can damage it.

setting the code to run every 1 second will only result in showing the same thing every second.
Im putting a 1kHz sinus on the A0 port

Of course... The spectrum of a 1kHz signal doesn't change from one moment to the next. :wink:

With music (or even speech) the amplitude & frequency content WILL change get moment-to-moment.

Note that in the real world, FFT isn't "perfect". And, with the Arduino's limited sampling speed and limited processing power it's not that precise and it shouldn't be uses for scientific or engineering purposes. Usually, people use it as a spectrum analyzer effect or for other lighting effects.

…I don't know if this will help you, but [u]Audacity[/u] has a [u]Plot Spectrum[/u] feature as well as a [u]Spectrogram View[/u]. It won't work in real-time. You need an audio file on your computer. But you can [u]Record[/u] audio or [u]Generate a tone/waveform[/u], and then analyze it.