0% found this document useful (0 votes)
160 views5 pages

EasyFFT Frequency Transform For Arduino PDF

This document provides instructions for implementing a Fast Fourier Transform (FFT) algorithm on Arduino. It explains that FFT allows decomposing signals into their frequency components, which can be useful for analyzing sensor data. The code precalculates sine and cosine values to improve speed. It performs FFT on sample sizes of powers of 2. Testing shows it can analyze a 1.25Hz triangle wave in 26ms for 64 samples. While accuracy is reduced, this FFT method enables real-time frequency analysis on Arduino.

Uploaded by

Tresor Benga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
160 views5 pages

EasyFFT Frequency Transform For Arduino PDF

This document provides instructions for implementing a Fast Fourier Transform (FFT) algorithm on Arduino. It explains that FFT allows decomposing signals into their frequency components, which can be useful for analyzing sensor data. The code precalculates sine and cosine values to improve speed. It performs FFT on sample sizes of powers of 2. Testing shows it can analyze a 1.25Hz triangle wave in 26ms for 64 samples. While accuracy is reduced, this FFT method enables real-time frequency analysis on Arduino.

Uploaded by

Tresor Benga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

instructables

EasyFFT: Fast Fourier Transform (FFT) for Arduino

by abhilash_patel

Measurement of frequency from the captured signal signal analysis. The motive of this project was to
can be a di cult task, especially on Arduino as it has prepare a code that is easy to implement on Arduino
lower computational power. There are methods without getting into the background of it.
available to capture zero-crossing where the
frequency is captured by checked how many times This project does not explain the Working of FFT but
the signal crosses zero lines within the given time. explains the application of the FFT function. The same
Such a method may not work when the signal is a process is also explained in the attached video.
combination of various frequencies.
If you are only interested in the application of code
This is somehow di cult to code if you are not from and not into an explanation of it. You may skip directly
such a background. But being a tinkerer this code may to step no 3.
be highly useful for various projects related to music,

https://youtu.be/kJ-g9MfY0Wg

Download
https://www.instructables.com/ORIG/FWO/ZWPR/KAFNFZA4/FWOZWPRKAFNFZA4.ino

Step 1: Introduction to Frequency Transform

EasyFFT: Fast Fourier Transform (FFT) for Arduino: Page 1


Any signal can be composed of a combination of of two frequencies f2 and f5. This signal is multiplied
various sinusoidal waves. So any time-based signal by test sine waves of values f1 to f5.
can be also shown as a combination of the various
sine of di erent amplitudes. It can be shown mathematically that -summation of
multiplication of two harmonic data-set having
I tried to explain the working of DFT(discrete Fourier di erent frequency tends to zero(higher number of
transform)in one of the previous instructable data can lead to batter result). In our case, If these
(https://www.instructables.com/id/Arduino- two multiplication frequency has the same (or very
Frequency...). These methods are extremely slow for close) frequency that sum of multiplication is the
any real-time application. which makes it almost nonzero number.
useless.
So if our signal is multiplied by f1 summation of
In the image, a signal is shown which is a combination multiplication will be zero (near to zero for real

application). similar is the case for f3, f4. However for intense calculation takes a higher time.
the value, f2 and f5 output won't be zero, but Mathematically it is shown that for N number of
signi cantly higher than the rest of the values. samples it takes N*N complex multiplication.

Here a signal is tested with 5 frequencies, so signal


needs to be multiplied by ve frequencies. Such

Step 2: Fast Fourier Transform

EasyFFT: Fast Fourier Transform (FFT) for Arduino: Page 2


To make the computation of DFT faster FFT algorithm 1. https:// ylib.com/books/en/2.729.1/derivation_of_...
was developed by James Cooley and John Tukey. This
algorithm is also considered as one of the most 2.
important algorithms of the 20th century. It divides a https://jakevdp.github.io/blog/2013/08/28/understa...
signal into an odd and even sequenced part which
makes a number of required calculations lower. By 3.
using it total required complex multiplication can be https://cnx.org/contents/[email protected]:zmcmahhR@
reduced to NlogN. which is a signi cant improvement. 7/D...

You may refer below references that I referred to 4. https://en.wikipedia.org/wiki/Fast_Fourier_transfo...


while writing the code for a detailed understanding of
the mathematics behind FFT:

Step 3: Explanation of Code

1. Fast sine and Cosine:

Calculation FFT takes the value of various sine and cosine multiple times. The inbuilt function of Arduino is not fast
enough and takes a good amount of time to provide the required value. Which makes code signi cantly slower
(doubles time for 64 samples). To counter this issue value of sine for 0 to 90 degrees is stored as multiple of 255.
Doing so will eliminate the need of using storing numbers as oat and we can store it as byte which takes 1/4th
space on Arduino. T he s ine _da t a [ ] ne e ds t o pa s t e a t t o p o f co de t o de cla re it a s a g lo ba l v a ria ble .

Apart from sine_data, an array called f _pe a ks [] de cla re d a s a g lo ba l v a ria ble . After every run of FFT
function this array updates. Where f_peaks[0] is the most dominant frequency and further values in descending
order.

byte sine_data [91]=<br> {


0,
4, 9, 13, 18, 22, 27, 31, 35, 40, 44,
49, 53, 57, 62, 66, 70, 75, 79, 83, 87,
91, 96, 100, 104, 108, 112, 116, 120, 124, 127,
131, 135, 139, 143, 146, 150, 153, 157, 160, 164,
167, 171, 174, 177, 180, 183, 186, 189, 192, 195,
198, 201, 204, 206, 209, 211, 214, 216, 219, 221,
223, 225, 227, 229, 231, 233, 235, 236, 238, 240,
241, 243, 244, 245, 246, 247, 248, 249, 250, 251,
252, 253, 253, 254, 254, 254, 255, 255, 255, 255
};
float f_peaks[5];

As we have stored value of sine for 0 to 90 degree any value of sine or cosine can be calculated. Below function the
rst round of the number to zero decimal point and return value from stored data. this method needs only one
oating division. This can be further reduced by directly storing sine values (not 255 multiple). but that eats up high
memory on Arduino.

Using the above procedure reduces accuracy but improves speed. For 64 points, it gives the advantage of 8ms and
for 128 points it gives an advantage of 20ms.

EasyFFT: Fast Fourier Transform (FFT) for Arduino: Page 3


https://www.instructables.com/ORIG/F2E/G56H/KAFNFZ9Q/F2EG56HKAFNFZ9Q.ino
… Download

Step 4: Explanation of Code: FFT Function

FFT can only be performed for the sample size of 2,4,8,16,32,64 and so on. if the value is not 2^n, than it will take
the lower side of value. For example, if we choose the sample size of 70 then it will only consider the rst 64
samples and omit rest.

It is always recommended to have a sample size of 2^n. which can be:

2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, ...

Two oats out_r and out_im will take a high amount of memory. for Arduino nano won't work for samples higher
than 128 (and in some cases 128) due to lack of available memory.

unsigned int data[13]={1,2,4,8,16,32,64,128,256,512,1024,2048};


int a,c1,f,o,x;
a=N;

for(int i=0;i<12;i++) //calculating the levels


{ if(data[i]<=a){o=i;} }

int in_ps[data[o]]={}; //input for sequencing


float out_r[data[o]]={}; //real part of transform
float out_im[data[o]]={}; //imaginory part of transform

Further ow is as follow:

1. Code generates a bit reversed the order for the given sample size (details on bit reversing on references: step 2)

2. Input data ordered as per generated order,

3. FFT performed

4. The amplitude of the complex number calculated,

5. Peaks are detected and ordered in descending order

6. results can be accessed from f_peaks[].

[to access other data (apart from peak frequency) code should be modi ed, so that local variable can be copied to
some prede ned global variable]

Step 5: Testing the Code

EasyFFT: Fast Fourier Transform (FFT) for Arduino: Page 4


A sample triangle wave is given as input. for this wave output value might be anywhere within speci ed bins.
sampling frequency is 10 Hz and the frequency of
wave itself is 1.25 Hz. Speed:

As can be shown from the raw output, value is for Arduino nano it takes:
matching with the FFT calculated by Scilab. however,
these values are not exactly the same as we low 16 Points : 4ms
accuracy but faster sine wave. 32 Points : 10ms
64 Points : 26ms
In output frequency array frequency are 1.25 and 3.75. 128 Points: 53ms
it is not necessary to get the exact value every time.
typically these numbers are called frequency bins. so

Step 6: Conclusion

This FFT code can be used in real-time applications. As it takes around 30 ms to complete the calculation. However,
its resolution limited by a number of samples. The number of the sample is limited by Arduino memory. By using
Arduino Mega or other higher performance board accuracy can be improved.

if you have any queries, suggestions, or corrections feel free to comment.

Great tut, but FFT stands for Fast Fourier Transform

Thanks for pointing it out. Title corrected.

EasyFFT: Fast Fourier Transform (FFT) for Arduino: Page 5

You might also like