Introduction: Arduino: Frequency Transform (DFT)
Step 1: How It Works(concept):
Given program for frequency transform provides great control over output you required. this program evaluates frequency range given by user at given input for data-set.
- In figure a data set made of two frequencies named f2 and f5 given which need to be tested. f2 and f5 are random names for two frequency, higher number for relatively higher frequency. here smaller frequency f2 has higher amplitude and f5 has smaller amplitude.
- It can be shown mathematically that -summation of multiplication of two harmonic data-set having different frequency tends to zero(higher number of data can lead to batter result). In our case If these two multiplication frequency has same (or very close) frequency that sum of multiplication is nonzero number where amplitude depends on amplitude of data.
- to detect specific frequency given data-set can be multiplied by various test frequencies and result can give component of that frequency in data.
Step 2: How It Works(in Code):
for that given data (f2+f5) one by one f1 to f6 is multiplies and value of sum is noted down. that final sum represents content of that frequency. rest (non-matching) of frequency should be ideally zero but it is not possible in real case. to make sum zero it is required to have infinite size of data-sets.
- as can be shown in figure f1 to f6 trial frequency and its multiplication with data set at every point is shown.
- in second figure summation of that multiplication at each frequency is plotted. two peaks at 1 and 5 is identifiable.
so using same approach for a random data we can evaluate for so many frequency and analyze frequency content of data.
Step 3: Using Code for Frequency Analysing:
for an example lets use this code to find DFT of square wave.
- first paste attached code(dft function) after loop as shown image.
8 TERMS THAT NEEDS TO BE SPECIFY
- an array of which dft need to be taken
- size of an array
- time interval between 2 reading in array in milliSECONDS
- lower value of frequency range in Hz
- upper value of frequency range in Hz
- size of steps for frequency range
- repeatation of a signal(minimum 1) higher number batter accuracy but increased solution time
- window function:
0 for no window
1 for flat-top window
2 for hann window
3 for hamming window
(if you do not have any idea about selecting window keep default 3)
example: dft(a,8,0.5,0,30,0.5,10,3); here a is an array of size 8 element to be checked for 0 Hz to 30 Hz with 0.5 step (0,0.5,1,1.5, ... ,29,29.5,30) 10 repeatation and hamming window
here it is possible to use bigger sized array as much as arduino can handle.
Step 4: Output:
if you comment out
from code serial plotter will give nature of frequency spectrum an if not Serial monitor would give frequency with its amplitude.
Step 5: Checking Various Window and Sample Sizes:
in figure, frequency of sine wave is measured using different setting.
Step 6: Example:
in figure transform of data using SciLab and arduino is compared.
1 year ago
Hello, thanks for the information.
I want to ask a question that I am curious about. I am not professional. I'm trying to make the irregular sinus signals countable. I need to apply this correction not just to one frequency, but to multiple frequencies respectively, if not simultaneously. Because many signals are obtained from the analog sensor.
When I research, I think fft will work for me.
Do you think this valuable code you have prepared will be useful for me?
Reply 1 year ago
check my recent instructable named ApproxFFT and EasyFFT available in my profile.
5 years ago
I saw it well.
What I would like to do is to DFT the current measured when current is applied to the nonlinear device.
I would like to analyze the fundamental wave 3-wave 5-wave by DFT the current.
If you know what to do, give me advice.
I am Korean, and I am beginner to deal with Arduino.
Reply to email@example.com
Reply 5 years ago
this code is way more slower for real time application. i highly recommend you to use fft.
follow this article:
5 years ago
but how to remove the dc component?
6 years ago
How to use this code for applying dft on readings from adc
Reply 6 years ago
first you need to specify an array and than store your data from adc to array. and dft of an array can be taken as explained.