-
Notifications
You must be signed in to change notification settings - Fork 0
/
csi_data_read_parse.py
211 lines (162 loc) · 7.37 KB
/
csi_data_read_parse.py
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
#!/usr/bin/env python3
# -*-coding:utf-8-*-
# Copyright 2021 Espressif Systems (Shanghai) PTE LTD
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# WARNING: we don't check for Python build-time dependencies until
# check_environment() function below. If possible, avoid importing
# any external libraries here - put in external script, or import in
# their specific function instead.
import sys
import csv
import json
import argparse
import pandas as pd
import numpy as np
import serial
from os import path
from io import StringIO
from PyQt5.Qt import *
from pyqtgraph import PlotWidget
from PyQt5 import QtCore
import pyqtgraph as pq
import threading
import time
# Reduce displayed waveforms to avoid display freezes
CSI_VAID_SUBCARRIER_INTERVAL = 3
# Remove invalid subcarriers
# secondary channel : below, HT, 40 MHz, non STBC, v, HT-LFT: 0~63, -64~-1, 384
csi_vaid_subcarrier_index = []
csi_vaid_subcarrier_color = []
color_step = 255 // (28 // CSI_VAID_SUBCARRIER_INTERVAL + 1)
# LLTF: 52
csi_vaid_subcarrier_index += [i for i in range(6, 32, CSI_VAID_SUBCARRIER_INTERVAL)] # 26 red
csi_vaid_subcarrier_color += [(i * color_step, 0, 0) for i in range(1, 26 // CSI_VAID_SUBCARRIER_INTERVAL + 2)]
csi_vaid_subcarrier_index += [i for i in range(33, 59, CSI_VAID_SUBCARRIER_INTERVAL)] # 26 green
csi_vaid_subcarrier_color += [(0, i * color_step, 0) for i in range(1, 26 // CSI_VAID_SUBCARRIER_INTERVAL + 2)]
CSI_DATA_LLFT_COLUMNS = len(csi_vaid_subcarrier_index)
# HT-LFT: 56 + 56
csi_vaid_subcarrier_index += [i for i in range(66, 94, CSI_VAID_SUBCARRIER_INTERVAL)] # 28 blue
csi_vaid_subcarrier_color += [(0, 0, i * color_step) for i in range(1, 28 // CSI_VAID_SUBCARRIER_INTERVAL + 2)]
csi_vaid_subcarrier_index += [i for i in range(95, 123, CSI_VAID_SUBCARRIER_INTERVAL)] # 28 White
csi_vaid_subcarrier_color += [(i * color_step, i * color_step, i * color_step) for i in range(1, 28 // CSI_VAID_SUBCARRIER_INTERVAL + 2)]
# csi_vaid_subcarrier_index += [i for i in range(124, 162)] # 28 White
# csi_vaid_subcarrier_index += [i for i in range(163, 191)] # 28 White
CSI_DATA_INDEX = 200 # buffer size
CSI_DATA_COLUMNS = len(csi_vaid_subcarrier_index)
DATA_COLUMNS_NAMES = ["type", "id", "mac", "rssi", "rate", "sig_mode", "mcs", "bandwidth", "smoothing", "not_sounding", "aggregation", "stbc", "fec_coding",
"sgi", "noise_floor", "ampdu_cnt", "channel", "secondary_channel", "local_timestamp", "ant", "sig_len", "rx_state", "len", "first_word", "data"]
csi_data_array = np.zeros(
[CSI_DATA_INDEX, CSI_DATA_COLUMNS], dtype=np.complex64)
class csi_data_graphical_window(QWidget):
def __init__(self):
super().__init__()
self.resize(1280, 720)
self.plotWidget_ted = PlotWidget(self)
self.plotWidget_ted.setGeometry(QtCore.QRect(0, 0, 1280, 720))
self.plotWidget_ted.setYRange(-20, 100)
self.plotWidget_ted.addLegend()
self.csi_phase_array = np.abs(csi_data_array)
self.curve_list = []
# print(f"csi_vaid_subcarrier_color, len: {len(csi_vaid_subcarrier_color)}, {csi_vaid_subcarrier_color}")
for i in range(CSI_DATA_COLUMNS):
curve = self.plotWidget_ted.plot(
self.csi_phase_array[:, i], name=str(i), pen=csi_vaid_subcarrier_color[i])
self.curve_list.append(curve)
self.timer = pq.QtCore.QTimer()
self.timer.timeout.connect(self.update_data)
self.timer.start(100)
def update_data(self):
self.csi_phase_array = np.abs(csi_data_array)
for i in range(CSI_DATA_COLUMNS):
self.curve_list[i].setData(self.csi_phase_array[:, i])
def csi_data_read_parse(port: str, csv_writer):
ser = serial.Serial(port=port, baudrate=921600,
bytesize=8, parity='N', stopbits=1)
if ser.isOpen():
print("open success")
else:
print("open failed")
return
while True:
strings = str(ser.readline())
if not strings:
break
strings = strings.lstrip('b\'').rstrip('\\r\\n\'')
index = strings.find('CSI_DATA')
if index == -1:
continue
csv_reader = csv.reader(StringIO(strings))
csi_data = next(csv_reader)
if len(csi_data) != len(DATA_COLUMNS_NAMES):
print("element number is not equal")
continue
try:
csi_raw_data = json.loads(csi_data[-1])
except json.JSONDecodeError:
print(f"data is incomplete")
continue
if len(csi_raw_data) != 128 and len(csi_raw_data) != 256 and len(csi_raw_data) != 384:
print(f"element number is not equal: {len(csi_raw_data)}")
continue
csv_writer.writerow(csi_data)
# Rotate data to the left
csi_data_array[:-1] = csi_data_array[1:]
if len(csi_raw_data) == 128:
csi_vaid_subcarrier_len = CSI_DATA_LLFT_COLUMNS
else:
csi_vaid_subcarrier_len = CSI_DATA_COLUMNS
for i in range(csi_vaid_subcarrier_len):
csi_data_array[-1][i] = complex(csi_raw_data[csi_vaid_subcarrier_index[i] * 2],
csi_raw_data[csi_vaid_subcarrier_index[i] * 2 - 1])
ser.close()
return
class SubThread (QThread):
def __init__(self, serial_port, save_file_name):
super().__init__()
self.serial_port = serial_port
save_file_fd = open(save_file_name, 'w')
self.csv_writer = csv.writer(save_file_fd)
self.csv_writer.writerow(DATA_COLUMNS_NAMES)
def run(self):
csi_data_read_parse(self.serial_port, self.csv_writer)
def __del__(self):
self.wait()
if __name__ == '__main__':
if sys.version_info < (3, 6):
print(" Python version should >= 3.6")
exit()
parser = argparse.ArgumentParser(
description="Read CSI data from serial port and display it graphically")
parser.add_argument('-p', '--port', dest='port', action='store', required=True,
help="Serial port number of csv_recv device")
parser.add_argument('-s', '--store', dest='store_file', action='store', default='F:\SRIBD\ESP32-Realtime-System\data\csi_fall.csv',
help="Save the data printed by the serial port to a file")
parser.add_argument('-d', '--delay', dest='delay', type=int, default=0,
help="Delay in seconds before the program starts")
parser.add_argument('-t', '--time', dest='time', type=int, default=0,
help="Recording time in seconds")
args = parser.parse_args()
# 延迟执行
if args.delay > 0:
print(f"Delaying start for {args.delay} seconds...")
time.sleep(args.delay)
serial_port = args.port
file_name = args.store_file
app = QApplication(sys.argv)
subthread = SubThread(serial_port, file_name)
subthread.start()
window = csi_data_graphical_window()
window.show()
sys.exit(app.exec())