-
Notifications
You must be signed in to change notification settings - Fork 0
/
Create 328p_Ffv5RandSP.ino
100 lines (86 loc) · 2.63 KB
/
Create 328p_Ffv5RandSP.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "arduinoFFT.h"
#include "Entropy.h" // Include the Entropy library
#define SAMPLES 64 // Must be a power of 2
#define SAMPLING_FREQUENCY 1000 // Max for Arduino Uno is around 9kHz due to hardware limitations
unsigned int sampling_period_us;
unsigned long microseconds;
double vReal[SAMPLES];
double vImag[SAMPLES];
arduinoFFT FFT = arduinoFFT();
enum WINDOW_TYPE {
HAMMING,
HANNING,
BLACKMAN,
FLATTOP
};
WINDOW_TYPE windowType;
void setup() {
Serial.begin(9600);
sampling_period_us = round(1000000 * (1.0 / SAMPLING_FREQUENCY));
// Initialize the entropy library
Entropy.initialize();
}
double generateRandom() {
double randomValue = 0;
for(int i = 0; i < 3; i++) {
randomValue += Entropy.random(SAMPLES);
}
return randomValue/3; // Average to get number between 0 and SAMPLES-1
}
void loop() {
for (int i = 0; i < 4; i++) {
windowType = static_cast<WINDOW_TYPE>(i);
/* Generating Test Data */
microseconds = micros();
for(int i=0; i<SAMPLES; i++) {
// Generate a random value between 0 and SAMPLES-1
vReal[i] = generateRandom();
Serial.println(vReal[i]); // Plot the input data
vImag[i] = 0;
while(micros() - microseconds < sampling_period_us){
// wait to sample at correct time interval
}
microseconds += sampling_period_us;
}
/* Performing FFT */
unsigned long start_time = micros();
switch (windowType) {
case HAMMING:
FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
break;
case HANNING:
FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD); // Use Hamming for Hanning
break;
case BLACKMAN:
FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_BLACKMAN, FFT_FORWARD);
break;
case FLATTOP:
FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_FLT_TOP, FFT_FORWARD); // Use FFT_WIN_TYP_FLT_TOP for Flattop
break;
}
FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD);
FFT.ComplexToMagnitude(vReal, vImag, SAMPLES);
unsigned long end_time = micros();
for (int i = 0; i < SAMPLES; i++) {
Serial.println(vReal[i]); // Plot the output data
}
Serial.print("Execution Time for ");
switch (windowType) {
case HAMMING:
Serial.print("HAMMING: ");
break;
case HANNING:
Serial.print("HANNING: ");
break;
case BLACKMAN:
Serial.print("BLACKMAN: ");
break;
case FLATTOP:
Serial.print("FLATTOP: ");
break;
}
Serial.println(end_time - start_time, DEC);
Serial.println(" microseconds");
}
delay(1000); // Perform test once per second
}