-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
FFT_visualize_realtime.ino
228 lines (202 loc) · 6.13 KB
/
FFT_visualize_realtime.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <driver/i2s.h>
#include <WiFi.h>
#include <WebServer.h>
#include <WebSocketsServer.h>
#include <ArduinoJson.h>
#include <arduinoFFT.h>
#define I2S_PORT I2S_NUM_1
#define I2S_WS 18
#define I2S_SD 32
#define I2S_SCK 14
const int BLOCK_SIZE = 512;
int16_t samples[BLOCK_SIZE];
arduinoFFT FFT = arduinoFFT();
double vReal[BLOCK_SIZE];
double vImag[BLOCK_SIZE];
String labels[] = {"125", "250", "500", "1K", "2K", "4K", "8K", "16K"};
int bands[8] = {0, 0, 0, 0, 0, 0, 0, 0};
const uint8_t amplitude = 150;
const char * ssid = "Hollow_ESP32";
const char * password = "hollowman";
char webpage[] PROGMEM = R"=====(
<html>
<!-- Adding a data chart using Chart.js -->
<head>
<script src='https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js'></script>
</head>
<body onload="javascript:init()">
<div>
<canvas id="chart" width="600" height="400"></canvas>
</div>
<!-- Adding a websocket to the client (webpage) -->
<script>
var webSocket, dataPlot;
var maxDataPoints = 20;
const maxValue = 128;
const maxLow = maxValue * 0.5;
const maxMedium = maxValue * 0.2;
const maxHigh = maxValue * 0.3;
function init() {
webSocket = new WebSocket('ws://' + window.location.hostname + ':81/');
dataPlot = new Chart(document.getElementById("chart"), {
type: 'bar',
data: {
labels: [],
datasets: [{
data: [],
label: "L",
backgroundColor: "#D6E9C6"
},
{
data: [],
label: "M",
backgroundColor: "#FAEBCC"
},
{
data: [],
label: "H",
backgroundColor: "#EBCCD1"
},
]
},
options: {
responsive: false,
animation: false,
scales: {
xAxes: [{ stacked: true }],
yAxes: [{
display: true,
stacked: true,
ticks: {
beginAtZero: true,
steps: 1000,
stepValue: 500,
max: maxValue
}
}]
}
}
});
webSocket.onmessage = function(event) {
var data = JSON.parse(event.data);
dataPlot.data.labels = [];
dataPlot.data.datasets[0].data = [];
dataPlot.data.datasets[1].data = [];
dataPlot.data.datasets[2].data = [];
data.forEach(function(element) {
dataPlot.data.labels.push(element.bin);
var lowValue = Math.min(maxLow, element.value);
dataPlot.data.datasets[0].data.push(lowValue);
var mediumValue = Math.min(Math.max(0, element.value - lowValue), maxMedium);
dataPlot.data.datasets[1].data.push(mediumValue);
var highValue = Math.max(0, element.value - lowValue - mediumValue);
dataPlot.data.datasets[2].data.push(highValue);
});
dataPlot.update();
}
}
</script>
</body>
</html>
)=====";
WebServer server(80);
WebSocketsServer webSocket = WebSocketsServer(81);
void setup() {
Serial.begin(115200);
i2s_init();
i2s_setpin();
WiFi.begin(ssid, password);
while(WiFi.status()!=WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
delay(1000);
server.on("/",[](){
server.send_P(200, "text/html", webpage);
});
server.begin();
webSocket.begin();
}
void loop() {
webSocket.loop();
server.handleClient();
int num_bytes_read = i2s_read_bytes(I2S_PORT,
(void *)samples,
BLOCK_SIZE*2,
portMAX_DELAY); // no timeout
FFT_Operation();
constrain128();
Send_Msg();
}
void constrain128(){
for(int j=0;j<8;j++){
bands[j]=int(bands[j]*(128.0/100000));
bands[j]=constrain(bands[j],0,128);
}
}
void FFT_Operation(){
for (uint16_t i = 0; i < BLOCK_SIZE; i++) {
vReal[i] = samples[i] << 8;
vImag[i] = 0.0;
}
FFT.Windowing(vReal, BLOCK_SIZE, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
FFT.Compute(vReal, vImag, BLOCK_SIZE, FFT_FORWARD);
FFT.ComplexToMagnitude(vReal, vImag, BLOCK_SIZE);
for (int i = 0; i < 8; i++) {
bands[i] = 0;
}
//0赫兹不存在,另外由于FFT结果的对称性,通常我们只使用前半部分的结果,即小于采样频率一半的结果。
for (int i = 1; i < (BLOCK_SIZE/2); i++){
if (vReal[i] > 2000) {
if (i<2 ) bands[0] = max(bands[0], (int)(vReal[i]/amplitude)); // 125Hz
if (i >=2 && i<=4 ) bands[1] = max(bands[1], (int)(vReal[i]/amplitude)); // 250Hz
if (i >4 && i<=8 ) bands[2] = max(bands[2], (int)(vReal[i]/amplitude)); // 500Hz
if (i >8 && i<=15 ) bands[3] = max(bands[3], (int)(vReal[i]/amplitude)); // 1000Hz
if (i >15 && i<=35 ) bands[4] = max(bands[4], (int)(vReal[i]/amplitude)); // 2000Hz
if (i >35 && i<=58 ) bands[5] = max(bands[5], (int)(vReal[i]/amplitude)); // 4000Hz
if (i >58 && i<=128 ) bands[6] = max(bands[6], (int)(vReal[i]/amplitude)); // 8000Hz
if (i >128 ) bands[7] = max(bands[7], (int)(vReal[i]/amplitude)); // 16000Hz
}
}
}
void Send_Msg() {
String json = "[";
for (int i = 0; i < 8; i++) {
if (i > 0) {
json +=", ";
}
json += "{\"bin\":";
json += "\"" + labels[i] + "\"";
json += ", \"value\":";
json += String(bands[i]);
json += "}";
}
json += "]";
webSocket.broadcastTXT(json.c_str(), json.length());
}
void i2s_init(){
const i2s_config_t i2s_config = {
.mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX),
.sample_rate = 44100,
.bits_per_sample = i2s_bits_per_sample_t(16),
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
.communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
.intr_alloc_flags = 0, // default interrupt priority
.dma_buf_count = 8,
.dma_buf_len = 64,
.use_apll = true
};
i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);
}
void i2s_setpin(){
const i2s_pin_config_t pin_config = {
.bck_io_num = I2S_SCK,
.ws_io_num = I2S_WS,
.data_out_num = -1,
.data_in_num = I2S_SD
};
i2s_set_pin(I2S_PORT, &pin_config);
}