Fast Fourier Transform Library & Arduino

From ESE205 Wiki
Jump to: navigation, search



As the name suggests the Fast Fourier Transform Library enables for the timely computation of a signal's discrete Fourier transform. Instructions on how to download the latest release can be found here. A fast Fourier transform (fFt) would be of interest to any wishing to take a signal or data set from the time domain to the frequency domain.

Materials & Prerequisites


All items one needs to utilize an FFT with an Arduino are:

    *USB connector


Next download latest open-source Arduino Software here.



First, let's begin with a discussion of a general Fourier Transform (FT) and then we will address the Fast Fourier Transform (FFT). The Fourier transform is a mathematical function that decomposes a waveform, which is a function of time, into the frequencies that make it up. The result produced by the Fourier transform is a complex valued function of frequency. The absolute value of the Fourier transform represents the frequency value present in the original function and its complex argument represents the phase offset of the basic sinusoidal in that frequency.

The Fourier transform is also called a generalization of the Fourier series. This term can also be applied to both the frequency domain representation and the mathematical function used. The Fourier transform helps in extending the Fourier series to non-periodic functions, which allows viewing any function as a sum of simple sinusoids. The definition of which is provided below. Note that we use the discrete time definition of the FT, or a Discrete Fourier Transform (DFT), and not the continuous time definition, the main difference between the two being a summation vs an integral. We make this choice because inputs into computers are discrete data points and not continuous so we can not use an integral, and by extension, we can't use the continuous time definition of the FT.

The decomposition of functions into their respective frequencies is a very powerful and useful tool, however, an FT requires massive amounts of computational work. For those of you in computer science, an FT would take O(n^2) time. So although we want to use this tool it is computationally expensive. Or at least this was the case until J.W Cooley and John Tukey came on the scene. They came up with the aptly named Cooley–Tukey FFT algorithm which reduced the time cost on a DFT from O(n^2) to O(nlogn). It recursively breaks down the DFT into smaller DFTs and turns the summation into a dynamic programming problem. So we save on time but we increase our space complexity dramatically, however, due to the continued improvement in transistor technology, modern day computing, for all practical purposes, doesn't care about space complexity until it has to. An example of a field that has to would be computational biology, due to the vast number of different genes in existence that all need to be tested. But that's beside the point, let's continue our discussion of an FFT.

The actual algorithm begins by splitting the matrix into two parts, one with all the even indexed elements, the other part with all the odd indexed elements. We continue spliting the matrices down in the same manner until we can perform a DFT on a manageable matrix. Another way to implement the split, rather than by the even-odd index convention, would be through a reversing the bit value of the array entry's index. You make the choice only on the language you program in; so choose the option that takes up less time to implement for the language.

Coding it will look like:

function cooley_tukey(x)

   N = length(x)
   if (N > 2)
       x_odd = cooley_tukey(x[1:2:N])
       x_even = cooley_tukey(x[2:2:N])
       x_odd = x[1]
       x_even = x[2]
   n = 0:N-1
   half = div(N,2)
   factor = exp.(-2im*pi*n/N)
   return vcat(x_odd .+ x_even .* factor[1:half], x_odd .- x_even .* factor[1:half])


FFT on Arduino

Here you can see some classic implementations in other languages, but as you can see the actual implementation is incredibly difficult. You can also find an example of a custom FFT at the same place. Typically one would not code an FFT from scratch unless you had a very specific purpose for it. Many libraries that deal with computational problems regarding any sort of waveform will have an FFT coded in the API that has been vetted by multiple software developers.

To implement a general FFT in an Arduino here are the steps:

1. Get your computer, Arduino, USB-B cable ready

2. Download the Arduino coding terminal on your computer (Web Editor Here)

3. Run the C++ code given below

To really see it in action:

1. Find a digital waveform (an audio signal, voltage signal, anything digital that can be modeled with sinusoids really)

2. Pass the waveform in as the parameter of the FFT

3. Print the output

To download and use Arduino's pre implemented arduino library see instructions here.

Code Implementations

Arduino C

'#'include "arduinoFFT.h" //import the library

'#'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(); // create FFT object

unsigned int sampling_period_us; unsigned long microseconds;

double vReal[SAMPLES]; //Real part of FFT array

double vImag[SAMPLES]; //Imaginary part of FFT aray

void setup() {


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


void loop() {

   for(int i=0; i<SAMPLES; i++)
       microseconds = micros();    //Overflows after around 70 minutes!
       vReal[i] = analogRead(0);
       vImag[i] = 0;
       while(micros() < (microseconds + sampling_period_us)){

   /*FFT*/                                                                // builtin FFT library and methods
   FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD);
   FFT.ComplexToMagnitude(vReal, vImag, SAMPLES);
   double peak = FFT.MajorPeak(vReal, SAMPLES, SAMPLING_FREQUENCY);

   //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[i], 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


  • Note other implementations exist and a few can be found here

More Arduino Examples

More examples on Arduino here.


  • Jordan Gewirtz
  • Nish Chakraburtty
  • Chanel Lynn

Group Link

eBox project page here

eBox log page here

External References (introduction to a fourier transform) (cooley tukey better explained) (introduction to fast fourier transform) (proof of cooley tukey) (cooley tukey intro) (Arduino FFT code)