-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgorithm_for_Arduino.ino
55 lines (40 loc) · 1.46 KB
/
Algorithm_for_Arduino.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "arduinoFFT.h"
#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();
unsigned int sampling_period_us;
unsigned long microseconds;
double vReal[SAMPLES];
double vImag[SAMPLES];
void setup()
{
Serial.begin(115200); //Serial communication baud rate
sampling_period_us = round(1000000 * (1.0 / SAMPLING_FREQUENCY));
}
void loop()
{
/*SAMPLING*/
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*/
FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD);
FFT.ComplexToMagnitude(vReal, vImag, SAMPLES);
double peak = FFT.MajorPeak(vReal, SAMPLES, SAMPLING_FREQUENCY);
/*PRINT RESULTS*/
//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
}