-
Notifications
You must be signed in to change notification settings - Fork 404
/
spectrum.py
125 lines (96 loc) · 3.92 KB
/
spectrum.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
#!/usr/bin/env python
#
# Copyright 2015 Bastian Bloessl <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import array
import struct
import sys
import time
from subprocess import call
import numpy as np
import pandas as pd
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
phy = "phy0"
call("echo chanscan > /sys/kernel/debug/ieee80211/" + phy + "/ath9k_htc/spectral_scan_ctl", shell=True)
plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111)
scatter, = ax.plot([2400, 2480], [-160, 50], 'r*')
scatter_min, = ax.plot([], [], 'bx')
scatter_max, = ax.plot([], [], 'go')
plt.show()
print "time,freq,signal"
while True:
### do measurement
call("iw dev wlan0 scan &>/dev/null", shell=True)
call("cat /sys/kernel/debug/ieee80211/" + phy + "/ath9k_htc/spectral_scan0 > data", shell=True)
with open("data", "rb") as file:
data = file.read(76)
x = []
y = []
now = time.time()
while data != "":
t, length = struct.unpack(">BH", data[0:3])
if t != 1 or length != 73:
print "only 20MHz supported atm"
sys.exit(1)
### metadata
max_exp, freq, rssi, noise, max_magnitude, max_index, bitmap_weight, tsf = struct.unpack('>BHbbHBBQ', data[3:20])
#print "max_exp: " + str(max_exp)
#print "freq: " + str(freq)
#print "rssi: " + str(rssi)
#print "noise: " + str(noise)
#print "max_magnitude: " + str(max_magnitude)
#print "max_index: " + str(max_index)
#print "bitmap_weight: " + str(bitmap_weight)
#print "tsf: " + str(tsf)
### measurements
measurements = array.array("B")
measurements.fromstring(data[20:])
squaresum = sum([(m << max_exp)**2 for m in measurements])
if squaresum == 0:
data = file.read(76)
continue
for i, m in enumerate(measurements):
if m == 0 and max_exp == 0:
m = 1
v = 10.0**((noise + rssi + 20.0 * np.log10(m << max_exp) - 10.0 * np.log10(squaresum))/10.0)
if i < 28:
f = freq - (20.0 / 64) * (28 - i)
else:
f = freq + (20.0 / 64) * (i - 27)
x.append(f)
y.append(v)
print str(now) + "," + str(f) + "," + str(v)
data = file.read(76)
df = pd.DataFrame(np.matrix([x, y]).T, columns = ["freq", "rssi"])
group = df.groupby('freq')
spectrum = group.mean()
spectrum_min = group.min()
spectrum_max = group.max()
### print output
#sys.stdout.write(str(time.time()))
#for freq, row in spectrum.iterrows():
# sys.stdout.write("," + str(freq) + ":" + str(row['rssi']))
#sys.stdout.write("\n")
scatter.set_xdata(spectrum.index)
scatter.set_ydata([10.0 * np.log10(val) for val in spectrum['rssi']])
scatter_min.set_xdata(spectrum_min.index)
scatter_min.set_ydata([10.0 * np.log10(val) for val in spectrum_min['rssi']])
scatter_max.set_xdata(spectrum_max.index)
scatter_max.set_ydata([10.0 * np.log10(val) for val in spectrum_max['rssi']])
fig.canvas.draw()