QuickFFT - High Speed (Low Accuracy) FFT For Arduino - Arduino Project Hub
QuickFFT - High Speed (Low Accuracy) FFT For Arduino - Arduino Project Hub
current_site=arduino&setup=true&redirect_to=%2Fprojecthub%2Fabhilashpatel121%2Fquickfft-high-speed-low-accuracy-
fft-for-arduino-e97e75)
We use cookies Ė
QuickFFT: High Speed (low accuracy) FFT
Our websites use cookies (also from third parties) for functional and analytical purposes, and to show you personalised
(http://creativecommons.org/licenses/by-nc/4.0)
ONLY REQUIRED ACCEPT ALL
This project is performing a faster frequency transform (FFT).
fft (/projecthub/projects/tags/fft) frequency (/projecthub/projects/tags/frequency)
Arduino Nano R3
(/projecthub/products/buy/1
× 1
9172?
( (/proj (/projecthub/products/
s=BAhJIhMzNzIyODUsUHJva
/ ecthu buy/19172?
mVjdAY6BkVG%0A)
p b/pro s=BAhJIhMzNzIyODUsU
(/projecthub/pro
r ducts HJvamVjdAY6BkVG%0A)
ducts/buy/19172?
o /buy/
s=BAhJIhMzNzIy
j 19172
ODUsUHJvamVjd
AY6BkVG%0A) e ?
c s=BA
t hJIhM
h zNzIy
u ODUs
b UHJva
/ mVjd
p AY6B
r kVG%
o 0A)
d
u
c
t
s
/
b
u
y
/
/
1
9
1
7
2
?
s
=
B
A
h
JI
h
M
z
N
z
I
y
O
D
U
s
U
H
J
v
a
m
V
j
d
A
Y
6
B
k
V
G
G
%
0
A
)
Typical Arduino has limited RAM and processing power, and FFT is a computationally-
intensive process. For many real-time applications, the only requirement is to get
frequency with maximum amplitude or required to detect frequency peaks.
In one of my tutorial, I prepared a code for FFT that can be found over here: EasyFFT
(https://create.arduino.cc/projecthub/abhilashpatel121/projects)
This code was able to perform FFT of up to 128 samples on Arduino nano. A higher
sample number than this is not possible due to the limited memory of Arduino. I have
modified the function a little bit to improve speed and reduce memory consumption.
This modification allows Arduino to perform FFT five times faster and consumes almost
half memory. This tutorial does not cover the Working of FFT, references for it can be
found at EasyFFT (https://create.arduino.cc/projecthub/abhilashpatel121/projects).
QUICK_FFT.INO
(HTTPS://HACKSTERIO.S3.AMAZONAWS.COM/UPLOADS/ATTACHMENTS/12
23694/QUICK_FFT.INO)
Working
The typical FFT function is modified to improve the speed with lesser accuracy. As shown
in the image a test signal needs to be multiplied by sine or cosine waveforms. These
values can be between 0 to 1, so making floating multiplication is a must. in Arduino,
Floating multiplication is slow compared to integer operations.
EasyFFT
PREVIOUS • • • •
NEXT
In this function, the sine/cosine wave is replaced by a square wave. As we have to
multiply a test signal with a square wave which may have values 0, 1 or -1. Due to that,
we can replace floating multiplication to simply integer addition or subtraction. For
Arduino integer addition or subtraction is around 5 times faster. This makes solving
around 5 times faster.
Due to this modification now frequency bin values can be stored as an integer (which
was previously float) and we get another advantage of lower memory consumption. In
Arduino Nano, int consumes 2 bytes of memory while float consumes 4 bytes of
memory. Due to this advantage in the new code, we are able to perform FFT for almost
256 samples (previously 128 samples).
In Normal FFT we needed to store the sine value to make a solution faster. In new
function, as we no more required sine/cosine values we can eliminate it and save some
memory.
Implementation:
Implementing this function is straightforward. We can simply copy the function at the
ens of the code. This function can be executed using the below command:
data: this term is an array having signal values, the recommended sample size are 2, 4, 8,
32, 64, 128, 256, 512,... onwards. if the sample size does not belong to these values it will
be clipped to the nearest lower side of values. for example, if the sample size is 75 than
FFT will be carried out for 64 numbers of samples. Max number of sample size is limited
by available RAM on Arduino.
The second term specifies the number of samples in an array and the last term is
sampling frequency in Hz.
Step 2: Code
This section explains the modification made in EasyFFT
(https://create.arduino.cc/projecthub/abhilashpatel121/easyfft-fast-fourier-transform-
fft-for-arduino-9d2677?ref=user&ref_id=1593632&offset=6) code that needs to be kept
in mind while making modification in the code,
1. As explained before, here integers are used to do FFT. Int in Arduino is a 16-bit number
and can contain values from -32768 to 32768. whenever the value of this int exceeds
this range it causes the problem. to eliminate this problem after ever level calculation. if
any of the value exceeds 15000 complete arrays will be divided by 100. this will prevent
the int to overflow.
2. Amplitude calculation: To calculate amplitude, the real and imaginary part needs to be
squared and the square root of the sum is required. squaring and the square root of the
function is time taking. to make the process faster, this code will simply do some of the
magnitudes of real and imaginary parts. This is surely less accurate and may lead to the
wrong conclusion in some cases. you may choose to return to the Normal method for
magnitude calculation but it will take more time and you also need to do some
arrangement to store these numbers.
3. This code does not have a module for multiple peak detection. It will simply choose
the value with max amplitude (excluding the first number which is DC offset). If you
need multiple peaks you can refer EasyFFT (https://www.instructables.com/id/EasyFFT-
Frequency-Transform-for-Arduino/) code and do required modification over here. In that
case, some array/variable also needs to be declared as a global variable.
4. The function contains the following line:
declaring the above variables as a global variable (pasting it at the start of code) will
save somewhere 1 milliseconds time at every execution.
5. Unlike the EasyFFT function, where the top 5 peaks were stored in the predefined
array. This function will return a float value. this value represents the frequency with
maximum amplitude in Hz. So the representation of code will look something like this.
float f= Q_FFT(data,256,100);
6. Peak Detection: Once frequency with max amplitude is found this function uses an
amplitude of frequency just before and after it to calculate the accurate results.
Amplitude used in this calculation is also the sum of modulus (not the square root of the
sum of squares)
if Fn is the frequency with max amplitude then the frequency can be calculated from
below formula.
Actual F= (An-1 *Fn-1 + An-1 *Fn-1 + An-1 *Fn-1 ) / (An-1+An+An+1)
where An is amplitude of n the frequency and Fn-1 is frequency value.
QUICK_FFT.INO
(HTTPS://HACKSTERIO.S3.AMAZONAWS.COM/UPLOADS/ATTACHMENTS/12
23696/QUICK_FFT.INO)
Step 3: Results:
Solving time is shown in the above image comparison with EasyFFT
(https://www.instructables.com/id/EasyFFT-Frequency-Transform-for-Arduino/). Speed
of it shown with the comparison.
comparision
PREVIOUS • •
NEXT
For sample data having 3 sinusoidal waves with different frequencies is shown. The
result from QuickFFT is compared with Scilab output. As we can see in the image 3 peaks
with max amplitude is matching with Scilab output. However, the output consists of lots
of noise, which may be misleading for some applications. So it is advised to check code
properly before applying to your application.
I hope you found this code useful for your project. In case of any query or suggestion
please do comment.
CODE
SCHEMATICS
E DOWNLOAD (HTTPS://HACKSTERIO.S3.AMAZONAWS.COM/UPLOADS/ATTACHMENTS/1223710/CAPTURE_5SRAEQQ
x
a
m
p
l
e
c
o
n
n
e
c
t
i
o
n
s
d
i
a
g
r
a
m
COMMENTS
AUTHOR
(/projecthub/abhilashpatel121)
abhilash_patel (/projecthub/abhilashpatel121)
8 PROJECTS 9 FOLLOWERS
FOLLOW (/PROJECTHUB/USERS/SIGN_UP?ID=1593632&M=USER&REASON=FOLLOW&REDIRECT
PUBLISHED ON
WRITE A COMMENT
Share
(/projecthub/aula-jazmati) (/projecthub/pietrocicuta)
and 2 others
(/projecthub/muhammad-aqib/high-speed-arduino-rc-car-5c2a3d?
ref=similar&ref_id=372285&offset=0)
(/projecthub/voske65/high-speed-pwm-on-arduino-atsamd21-859b06?
ref=similar&ref_id=372285&offset=1)
High Speed PWM on Arduino ATSAMD21 (/projecthub/voske65/high-
speed-pwm-on-arduino-atsamd21-859b06?
ref=similar&ref_id=372285&offset=1)
by JayV (/projecthub/voske65)
3,700 VIEWS 0 COMMENTS 5 RESPECTS
(/projecthub/mircemk/diy-fft-audio-spectrum-analyzer-ca2926?
ref=similar&ref_id=372285&offset=2)
DIY FFT Audio Spectrum Analyzer (/projecthub/mircemk/diy-fft-
audio-spectrum-analyzer-ca2926?
ref=similar&ref_id=372285&offset=2)
Project tutorial by Mirko Pavleski (/projecthub/mircemk)
12,836 VIEWS 1 COMMENT 14 RESPECTS
(/projecthub/abhilashpatel121/easyfft-fast-fourier-transform-fft-for-arduino-9d2677?
ref=similar&ref_id=372285&offset=3)
(/projecthub/taifur/high-power-dc-motor-starter-with-overload-protection-f5662c?
ref=similar&ref_id=372285&offset=5)
High Power DC Motor Starter with Overload Protection
(/projecthub/taifur/high-power-dc-motor-starter-with-overload-
protection-f5662c?ref=similar&ref_id=372285&offset=5)
by Md. Khairul Alam (/projecthub/taifur)
2,229 VIEWS 0 COMMENTS 18 RESPECTS
(https://www.arduino.cc)
Powered by
(https://www.hackster.io)