How to perform FFT in the Arduino Zero?

I have a code for the Arduino Zero below and it throws the error when I try to compile. What am I doing wrong?

#define LIN_OUT 1 
#define MERR 10 
#define PID_ON 1
#define TRI_PERIOD 100
#define F_BLACKMAN 0.02463994238109641755656975202572

//Include the library for the FFT zero

#include <arm_common_tables.h>
#include <Adafruit_ZeroFFT.h>
#define DATA_SIZE 256

// Include library for the AnalogReadfast for the Arduino Zero

#include <Streaming.h>
#include <StopWatch.h>
#include "avdweb_AnalogReadFast.h"

//Photodiode array variables

byte CLKpin = 4;          //Arduino pin delivering the clock pulses to pin 3 (analog output 1) of TSL1402R
byte SIpin  = 5;         //Arduino pin delivering the SI(Serial-Input) pulse to pin 2 of the TSL1402R
byte AOpin0 = 0;        //Arduino pin connected to pin 4 (analog out put 1) of TSL1402R
byte AOpin1 = 1;       // Arduino pin connected to pin 8 (analog out put 2) of TSL1402R

//Define the variables for determining the displacement form the FFT using the Arduino Zero

int maxindex;             //Index of the maximum of the FFT
float maxvalue;           //for comparing values of FFT to find global maximum
double kmax;              //Spatial wavenumber corresponding to dominant fringe modulation Fourier component
float Am;                 //Real part of Fourier coefficient corresponding to kmax
float Bm;                 //Imag part of Fourier coefficient corresponding to kmax
float theta;              //Phase angle of Fourier transform at kmax
int Nwrap = 0;            // Number of phase wraps
float displacement;       //Displacement in nanometers
int w = 0;                // See what does it mean?
int prevw = 0;            // See what does it mean?
int wdiff = 0;            //See what does it mean?

// Since we are not using the DAC board, we discard the variables related to the DAC board here.

//PID variables to account for the error

unsigned long lastTime;       // why dont we initialize it?
double Output = 0;            //Output value for PID (voltage to Piezoelectric actuator in millivolts)
double Setpoint;              //Desired value of displacement(nm)
double kp = 0;                // 0 proportional gain for PID
double ki = -2;               // (1000 /(138.5)) Integral gain for PID( what is the meaning of this fraction?)
double kd = 0;                // 0 derivative gain for PID
double error;                 //Error signal; difference between Setpoint and Displacement
double preverror;             //Stores previous value of error for derivative control
float esum;                   //Integral of error signal over time
float D;                      //Derivative of error signal
unsigned long time0 = 0;      // used in calculating derivative term
float MSerror = 0;            // Mean Squared error
float RMSerror = 0;           //Root mean squared error
float elist[MERR];            // Array of last MERR error values
int jerr = 0;                 // indexed array of integral error term
float runningRMSerror = 0;    // RMS error calculated from elist
float m = 0;                  // extra variables for one time calculatons
float pchange = 0;
float lastp = 0;
float k = 1;
int p = 1;
float M = 0;                // Still i need to define all these uncommented variable

void setup() {
void loop() {


// Photodiode functions ==============================================================

void ClockPulse() { // For sending clock pulses during reading of photodiode
 digitalWrite(CLKpin, HIGH);
 digitalWrite(CLKpin, LOW);

void INIT_PHOTO() { // Intialization of photodiode
 // Initialize two Arduino pins as digital output:
   pinMode(CLKpin, OUTPUT);
   pinMode(SIpin, OUTPUT);
   for ( int i = 0; i < 14; i++ )
     digitalWrite(i, LOW);
 // Clock out any existing SI pulse through the ccd register:
   for (int i = 0; i < 260; i++)
 // Create a new SI pulse and clock out that same SI pulse through the sensor register:
   digitalWrite(SIpin, HIGH);
   digitalWrite(SIpin, LOW);
   for (int i = 0; i < 260; i++)
void READ_PHOTO() { // Read the photodiode
 // Stop the ongoing integration of light quanta from each photodiode by clocking in a SI pulse
 // into the sensors register:
 digitalWrite(SIpin, HIGH);
 digitalWrite(SIpin, LOW);

 for (int i = 0; i < 128; i++)
   delayMicroseconds(20);  // We add a delay to stabilize the AO output from the sensor
   ZeroFFT(2 * i,DATA_SIZE) = analogReadFast(AOpin0);        // Output of first half of photodiode array
   ZeroFFT(2 * i + 1,DATA_SIZE) = 0;                                   // Store zero in the "imaginary" part of the array
   ZeroFFT(2 * i + 256,DATA_SIZE) = analogReadFast(AOpin1);  // Output of second half of photodiode 
   ZeroFFT(2 * i + 257,DATA_SIZE) = 0;                   // Store zero in the "imaginary" part of the array

Here is the error:

Arduino: 1.8.5 (Mac OS X), Board: "Arduino/Genuino Zero (Programming Port)"

In file included from /Users/RAJU/Documents/Arduino/DIS_SET_V06_Arduino_Zero/DIS_SET_V06_Arduino_Zero.ino:16:0:
/Users/RAJU/Documents/Arduino/libraries/Streaming/Streaming.h:102:15: warning: unused parameter 'arg' [-Wunused-parameter]
inline Print &operator <<(Print &obj, _EndLineCode arg) 

DIS_SET_V06_Arduino_Zero:120: error: expected unqualified-id before 'for'
 for (int i = 0; i < 128; i++)
DIS_SET_V06_Arduino_Zero:120: error: 'i' does not name a type
 for (int i = 0; i < 128; i++)
DIS_SET_V06_Arduino_Zero:120: error: 'i' does not name a type
 for (int i = 0; i < 128; i++)
exit status 1
expected unqualified-id before 'for'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Please edit your post to add code tags, as described in "How to use this forum".

The compiler is complaining because for (int i = 0; i < 128; i++) is outside every function.

Use code tags next time.

Hi, I edited the code tags. Now do you have any idea why it is throwing out the error. Just to remind the board used here is for the Arduino Zero Board

Yes, I have a good idea why it is throwing out the error. You put the last for() outside all the functions. Which function does it belong in?

Can you please make this little bit more clearer?

A function usually starts with "void". There are other types of functions but the common ones you will see a lot all say "void".

[b]void loop() { }[/b] is a function. The "inside" of the function is between the { and the }. Usually you put code inside the function to make it do things.

Certain things belong outside functions. Global variables are declared outside functions, so that all functions can access the global variables. But you can't do anything with those variables outside the functions. The declaration basically says that the variable exists and gives it a name and a type. The functions are the things that put information in or out of the globals.

Functions must also be outside functions.

So, scroll down to the end of your code. The last for() { } is outside all functions. Where should it go? It can't stay where it is.

Oh! thank you for pointing out it! I will try it and let you know.

Oh! thank you for pointing out it! I was asking is the way of performing the FFT correct in this code for the Arduino Zero?

I don't have a Zero. I don't have the other hardware you have. I have no idea what you want the program to do.

So there is no way I can tell you the program is correct. If I knew what you expect it to do then I can make some suggestions but you are the only one who can verify correctness.

Thank you for your reply. I want to perform the FFT using the Arduino Zero hardware. Thats all. Is there anyone with Arduino Zero to run this code and help me out?

You can perform the FFT all you want but usually you are trying to analyze a specific input into a specific output for a specific purpose.

Your code seems to refer to photodiodes. Is that the input?

Yeah you are right. Its like this the photodiode(TSL1402R) has 256 pixels. The light from the source falls on these pixels. As you see in the code, these pixels integrate the light as the fft_input[2*i]. This is how I did the FFT for the Arduino Uno. Now I switched form the Arduino Uno to Zero, then I can not use the same code exactly which is obvious. My question is does anybody know how to perform the FFT in the Arduino Zero if you have the input array of the variables?

Lots of people know how to use the Arduino Zero FFT library, and most of them were very wise to start by studying and running the examples that come with it.

You would be wise to follow in their footsteps!

I also know the example code for the ZeroFFT library in the Arduino Zero. The problem is that its not obvious to create the array of data in my case. In the example code, they have the header file < signal.h> and the DATA_SIZE = 256. The signal is the array of data for the sine wave. For more details, this “Adafruit Zero FFT Library - Arduino Libraries” is the link for the FFT library. The syntax ZeroFFT(signal,DATA_SIZE) performs the FFT. I am wondering if anybody would be able to suggest me some idea in my case. Thank you everyone for the comments and suggestions.

Simply replace the data in the sample array with your data, element by element.

One possibility follows; a wise person could certainly think of others!

 for (i=0; i<256; i++) signal[i]=some_other_data[i];


  signal = some_other_data;

Thank for your reply. I tried all these options. Its not working at all. The thing is I can not replace the example array with my experimental data. The nature of my experiment does not suit with this idea because the photo diode collects the data in a loop and the array of the data is different for each loop iteration.

Hi all,

Can we perform the complex Fast Fourier Transform(FFT) using the Arduino Zero? Any link related to this topic might be helpful? I searched many times and could not find the concrete and helpful answer.

I searched many times and could not find the concrete and helpful answer.

The first entry from a Google search - Adafruit Zero FFT Library - Arduino Libraries . In what way is it not helpful ?