-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch_graph.py
More file actions
167 lines (115 loc) · 4.57 KB
/
batch_graph.py
File metadata and controls
167 lines (115 loc) · 4.57 KB
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
# 将此文件覆盖到路径CSIKit中的CSIKit\CSIKit\tools,便于使用trans.py批量生成图片数据
import matplotlib.pyplot as plt
import numpy as np
from CSIKit.util import matlab
from CSIKit.util.csitools import get_CSI
from CSIKit.util.filters import bandpass, hampel, running_mean
from CSIKit.reader import get_reader, IWLBeamformReader
DEFAULT_PATH = "./data/intel/misc/log.all_csi.6.7.6.dat"
# DEFAULT_PATH = "./data/pi/walk_1597159475.pcap"
class BatchGraph:
def __init__(self, path: str = DEFAULT_PATH, scaled: bool = False, filter_mac: str = None):
reader = get_reader(path)
self.csi_data = reader.read_file(path, scaled=scaled, filter_mac=filter_mac)
self.path = path
def prepostfilter(self):
csi_trace = self.csi_data.frames
finalEntry, no_frames, _ = get_CSI(self.csi_data)
finalEntry = finalEntry[15]
hampelData = hampel(finalEntry, 10)
smoothedData = running_mean(hampelData.copy(), 10)
y = finalEntry
y2 = hampelData
y3 = smoothedData
x = list([x.timestamp for x in csi_trace])
if sum(x) == 0:
x = np.arange(0, no_frames, 1)
plt.plot(x, y, label="Raw")
plt.plot(x, y2, label="Hampel")
plt.plot(x, y3, "r", label="Hampel + Running Mean")
plt.xlabel("Time (s)")
plt.ylabel("Amplitude (dBm)")
plt.legend(loc="upper right")
output_path = self.path.replace('.dat', '_prepostfilter.png')
plt.savefig(output_path)
plt.close()
def plotAllSubcarriers(self):
finalEntry, no_frames, _ = get_CSI(self.csi_data)
for x in finalEntry:
plt.plot(np.arange(no_frames) / 20, x)
plt.xlabel("Time (s)")
plt.ylabel("Amplitude (dBm)")
plt.legend(loc="upper right")
output_path = self.path.replace('.dat', '_all_subcarriers.png')
plt.savefig(output_path)
plt.close()
def heatmap(self):
finalEntry, no_frames, no_subcarriers = get_CSI(self.csi_data)
if len(finalEntry.shape) == 4:
finalEntry = finalEntry[:, :, 0, 0]
finalEntry = np.transpose(finalEntry)
x_label = "Time (s)"
try:
x = self.csi_data.timestamps
x = [timestamp - x[0] for timestamp in x]
except AttributeError:
x = [0]
if sum(x) == 0:
xlim = no_frames
x_label = "Frame No."
else:
xlim = max(x)
limits = [0, xlim, 1, no_subcarriers]
_, ax = plt.subplots()
im = ax.imshow(finalEntry, cmap="jet", extent=limits, aspect="auto")
cbar = ax.figure.colorbar(im, ax=ax)
cbar.ax.set_ylabel("Amplitude (dBm)")
plt.xlabel(x_label)
plt.ylabel("Subcarrier Index")
plt.title(self.csi_data.filename)
output_path = self.path.replace('.dat', '_heatmap.png')
plt.savefig(output_path)
plt.close()
@staticmethod
def plot_heatmap(csi_matrix, timestamps, output_path):
csi_matrix = np.transpose(csi_matrix)
x_label = "Time (s)"
try:
x = timestamps
x = [timestamp - x[0] for timestamp in x]
except AttributeError:
x = [0]
if sum(x) == 0:
xlim = csi_matrix.shape[1]
x_label = "Frame No."
else:
xlim = max(x)
limits = [0, xlim, 1, csi_matrix.shape[0]]
_, ax = plt.subplots()
im = ax.imshow(csi_matrix, cmap="jet", extent=limits, aspect="auto")
cbar = ax.figure.colorbar(im, ax=ax)
cbar.ax.set_ylabel("Amplitude (dBm)")
plt.xlabel(x_label)
plt.ylabel("Subcarrier Index")
plt.title("CSI Amplitude Heatmap Plot")
plt.savefig(output_path)
plt.close()
def sumsqrssi(self):
finalEntry, no_frames, no_subcarriers = get_CSI(self.csi_data, extract_as_dBm=False)
if len(finalEntry.shape) == 4:
finalEntry = finalEntry[:, :, 0, 0]
csi = finalEntry
rss = [x.rssi for x in self.csi_data.frames]
sumsq = np.sum(csi ** 2, axis=1)
norm_sumsq = np.sqrt(sumsq) / no_subcarriers
line = [matlab.db(sumsq_value) / rss_value for rss_value, sumsq_value in zip(rss, norm_sumsq)]
plt.plot(self.csi_data.timestamps, line)
plt.xlabel("Time (s)")
plt.ylabel("RSSI / sumsq")
plt.title(self.csi_data.filename)
output_path = self.path.replace('.dat', '_sumsqrssi.png')
plt.savefig(output_path)
plt.close()
if __name__ == "__main__":
bg = BatchGraph()
bg.heatmap()