Hi guys, I'm pretty inexperienced in this but I'm trying to compute FFT on an electrical signal. Could you tell me what I might be doing wrong with my code.
Methodology:
The code creates an array where it stores the samples from the ADC conversion and then uses them for the FFT. Thereafter I do a simple multiplication.
Below are the 2 code files. 1 is the main code, and the other is related to the famous fix_ftt (really long) code for the FFT algorithm done in 1989
#include "fix_fft.h"
#include <math.h>
#include <arduino.h>
#define LOG2N 10 //base2 log of N [N being number of points and length of spectrum]
#define NPTS 1024 // Number of points or length of spectrum in points
#define FS 2000 // 2kHz is the sampling frequency
int real[NPTS], imag[NPTS];
void sampleAdc(void);
void startFFT(void);
void setup()
{
Serial.begin(9600); //baud rate and data transfer speed set
delay(1000); // delay to give serial monitor tme to start up
Serial.println("Start");
}
void loop()
{
sampleAdc(); //calls the sampleADC function below which does the ADC
for (int i = 0; i < NPTS; i++) //this loop initializes 1024 samples and prints each as an element of a real array
{
Serial.println(real[i]);
}
startFFT();
}
void sampleAdc (void)
{
int i = 0;
int ADC_CH;
while (i < NPTS)
{ //read ADC pin NPTS times at hi
real[i] = analogRead(A0);
i++;
}
} //end of void sampleAdc
//do fft to the sampled signal
//output shows peak frequency
void startFFT (void) //Computes FFT of sampled signal,identifies peak frequency, calculates approximate top speed and outputs it
{
// absolute
int i;
short fix_fft(real, imag, LOG2N, 0); //performs fft on sampled points
for(i = 0; i < NPTS/2; i++) //get the power amplitude in
{
real[1] = sqrt((int)real[i] *(int)real[i]+ (int) imag[i] * (int)imag[i]);
}
//find the peak
int peakHz = 0;
int peaki = 0;
for (i = 1; i < NPTS/2; i++) //bin 0 holds the summation
{
if (real[i] > peakHz)
{
peakHz = real[i];
peaki = i;
}
}
peaki = peaki * FS/NPTS;
int topSpeed = peaki/70;
Serial.print ("Top speed : ");
Serial.println (topSpeed);
Serial.println ("");
} // end of FFT function
#ifdef fix_fft_h
#define fix_fft_h
#include <arduino.h>
int fix_fft(short fr[], short fi[], short m, short inverse);
int fix_fftr(short f[], int m, int inverse);
#endif