-
Notifications
You must be signed in to change notification settings - Fork 1
/
non_wifi_interference_analysis.py
302 lines (273 loc) · 8.83 KB
/
non_wifi_interference_analysis.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# Date : 10 September ,2013
#Purpose : To calculate the entropy of the physical layer error counters
# and plot the entropy of the counters
# Also calculates the FFT and Autocorrelation of an input series
# Runs on local machine so that $DISPLAY is accessible
import matplotlib.font_manager
import numpy as np
from matplotlib.figure import Figure
from matplotlib.backends.backend_agg import FigureCanvasAgg
import datetime as dt
import sys,os
from collections import defaultdict
from pylab import plot, show, title, xlabel, ylabel, subplot,savefig
from scipy import fft, arange
import pickle
fig_width = 12
fig_length = 12.25
# Can be used to adjust the border and spacing of the figure
fig_left = 0.12
fig_right = 0.94
fig_bottom = 0.25
fig_top = 0.94
fig_hspace = 0.5
def pickle_reader(input_folder):
data_fs=os.listdir(input_folder)
Physical_errors_table=defaultdict(list)
for f_name in data_fs :
_f_content= pickle.load(open(input_folder+f_name,'rb'))
router_id= _f_content[0]
Physical_errors_table[router_id]=_f_content[1]
return Physical_errors_table
def auto_correlation(y):
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.plot(y)
#ax1.xcorr(y, y, usevlines=True, maxlags=50, normed=True, lw=2)
ax1.grid(True)
ax1.axhline(0, color='black', lw=2)
ax2 = fig.add_subplot(212, sharex=ax1)
ax2.acorr(y, usevlines=True, normed=True, maxlags=30, lw=2)
ax2.grid(True)
ax2.axhline(0, color='black', lw=2)
ax2.set_title('AutoCorrelation')
plt.show()
def mylog(val,base):
if val:
a= np.log(val)
b=np.log(base)
return (a*1.0 /b)
else:
return 0
def new_entropy(a):
num_bins=10
counts, bin_edges = np.histogram(a,bins=num_bins,normed=True)
cdf=np.cumsum(counts)
scale = 1.0/cdf[-1]
cdf=cdf*scale
subplot(2,1,1)
sorted=np.sort(counts)
plot(counts,np.arange(len(sorted)*1.0)/len(sorted) )
loc='hello'
plot(bin_edges[1:],cdf,label=loc,color='r', linewidth=5.0)
show()
def _e(labels):
num_bins=10
counts, bin_edges = np.histogram(labels,bins=num_bins,normed=True)
cdf=np.cumsum(counts)
scale = 1.0/cdf[-1]
counts=counts*scale
ent=0.
for i in counts:
if i > 0:
ent -= i * mylog(i, base=10)
return ent
def entropy(labels):
""" Computes entropy of label distribution. """
n_labels = len(labels)
if n_labels <= 1:
return 0
counts = np.bincount(labels)
probs = counts / (n_labels*1.0)
n_classes = np.count_nonzero(probs)
if n_classes <= 1:
return 0
ent = 0.0
# Compute standard entropy.
for i in probs:
ent -= i * mylog(i, base=n_classes)
return ent
def save_plotSpectrum(y,Fs,image_name):
"""
Plots a Single-Sided Amplitude Spectrum of y(t)
"""
fig = Figure(linewidth=0.0)
fig.set_size_inches(fig_width,fig_length, forward=True)
Figure.subplots_adjust(fig, left = fig_left, right = fig_right, bottom = fig_bottom, top = fig_top, hspace = fig_hspace)
n = len(y) # length of the signal
_subplot = fig.add_subplot(2,1,1)
print "Fi"
_subplot.plot(arange(0,n),y)
xlabel('Time')
ylabel('Amplitude')
_subploti_2=fig.add_subplot(2,1,2)
k = arange(n)
T = n/Fs
frq = k/T # two sides frequency range
frq = frq[range(n/2)] # one side frequency range
Y = fft(y)/n # fft computing and normalization
Y = Y[range(n/2)]
_subplot_2.plot(frq,abs(Y),'r') # plotting the spectrum
xlabel('Freq (Hz)')
ylabel('|Y(freq)|')
print "here"
canvas = FigureCanvasAgg(fig)
if '.eps' in outfile_name:
canvas.print_eps(outfile_name, dpi = 110)
if '.png' in outfile_name:
canvas.print_figure(outfile_name, dpi = 110)
def plotSpectrum(y,Fs,image_name):
"""
Plots a Single-Sided Amplitude Spectrum of y(t)
"""
n = len(y) # length of the signal
subplot(2,1,1)
plot(arange(0,n),y)
xlabel('Time')
ylabel('Amplitude')
subplot(2,1,2)
k = arange(n)
T = n/Fs
frq = k/T # two sides frequency range
frq = frq[range(n/2)] # one side frequency range
Y = fft(y)/n # fft computing and normalization
Y = Y[range(n/2)]
plot(frq,abs(Y),'r') # plotting the spectrum
xlabel('Freq (Hz)')
ylabel('|Y(freq)|')
print "here"
#show()
savefig(image_name,dpi=110)
def print_image(x,y,x2,y2,outfile_name):
fig = Figure(linewidth=0.0)
fig.set_size_inches(fig_width,fig_length, forward=True)
Figure.subplots_adjust(fig, left = fig_left, right = fig_right, bottom = fig_bottom, top = fig_top, hspace = fig_hspace)
_subplot = fig.add_subplot(2,1,1)
_subplot.set_title('Detection of Source generating non-wifi Interference')
_subplot.plot(x,y,color='b')
_subplot.set_xlabel('Time')
_subplot.set_ylabel('Error Counts')
# _subplot.set_ylim([0,1])
_subplot2=fig.add_subplot(2,1,2)
_subplot2.plot(x2,y2,color='r') # plotting the spectrum
_subplot2.set_ylabel('Entropy')
_subplot2.set_xlabel('Time')
_subplot2.set_ylim([0,1])
canvas = FigureCanvasAgg(fig)
if '.eps' in outfile_name:
canvas.print_eps(outfile_name, dpi = 110)
if '.png' in outfile_name:
canvas.print_figure(outfile_name, dpi = 110)
if 0:# __name__ =='__main__':
'''
Does processing of the physical layer errors
in the each frame pickled by phy_err_stats.py
'''
if len(sys.argv)!=2:
print "Usage: python file.py <folder with data>"
sys.exit(1)
input_f=sys.argv[1]
Physical_errors_table=pickle_reader(input_f)
global_data=[]
for k,v in Physical_errors_table.iteritems() : # key is the router id
a=sorted(v,key=v.get)
for i in a :
d=v[i]
phy_=d[0][0]
phy_cck=d[0][1]
phy_ofdm=d[0][2]
global_data.append(phy_ofdm)
y=[]
for i in global_data:
for t in i :
if not(t[0]==0):
y.append(t)
time1= global_data[0][0][0]
err_samples=[]
temp_accumulator=0
orig_ofdm_counts=[]
orig_ofdm_time=[]
sa=0
er=0
for i in range(1,len(y)) :
sa=sa+1
if y[i-1][0]>y[i][0] :
er=er+1
else:
orig_ofdm_counts.append(y[i][1])
orig_ofdm_time.append(y[i][0])
if y[i][0]-time1 <8334:
temp_accumulator=temp_accumulator+y[i][1]
else :
err_samples.append(temp_accumulator)
temp_accumulator=0
time1=y[i][0]
print er*100.0/(er+sa)
e=[]
t=[]
print "before spectrum plotting "
plotSpectrum(err_samples,1000000/8334.0,'fft.png')
print "done with printing spectrum "
for i in range(0,len(err_samples),250):
#auto_correlation(err_samples[i:i+250])
e.append(entropy(err_samples[i:i+250]))
t.append(i)
print_image(orig_ofdm_time,orig_ofdm_counts,t,e,'non_scaled_entropy_without_ofdm.png')
# print "ofdm",entropy(phy_ofdm[0:2000])
# print "phy", entropy(phy_[0:2000])
# print "cck",entropy(phy_cck[0:2000])
if __name__ =='__main__':
'''
Does processing of the timestamps of bad fcs frames
collected by data
'''
if len(sys.argv)!=2:
print "Usage: python file.py <folder with data>"
sys.exit(1)
input_f=sys.argv[1]
Physical_errors_table=pickle_reader(input_f)
global_data=[]
for k,v in Physical_errors_table.iteritems() : # key is the router id
file_timestamps=sorted(v,key=v.get)
for tstamp in file_timestamps :
d=v[tstamp]
phy_=d[0]
global_data.append(phy_)
y=[]
st=0
for i in global_data:
for t in i :
y.append(t)
err_samples=[]
temp_accumulator=0
orig_ofdm_counts=[]
orig_ofdm_time=[]
sa=0
er=0
time1= y[0][0]
for i in range(1,len(y)) :
sa=sa+1
if y[i-1][0]>y[i][0] :
er=er+1
else:
orig_ofdm_counts.append(y[i][1])
orig_ofdm_time.append(y[i][0])
if y[i][0]-time1 <8334:
temp_accumulator=temp_accumulator+y[i][1]
else :
err_samples.append(temp_accumulator)
temp_accumulator=0
time1=y[i][0]
print "% error=",er*100.0/(er+sa)
e=[]
t=[]
print "before spectrum plotting "
save_plotSpectrum(err_samples,1000000/8334.0,'fft.png')
print "done with printing spectrum "
for i in range(0,len(err_samples),250):
#auto_correlation(err_samples[i:i+250])
e.append(entropy(err_samples[i:i+250]))
t.append(i)
print_image(orig_ofdm_time,orig_ofdm_counts,t,e,'non_scaled_entropy_without_ofdm.png')