-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_hists.py
91 lines (73 loc) · 3.03 KB
/
get_hists.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
import os
import sys
import gc
import numpy as np
import matplotlib.pyplot as plt
from utilities.preprocessing import get_xhdr_sample_rate, load_raw_data, get_basic_block_len, persist_object, trim_iq_basic_block, iq2fft, get_config
from utilities.plots import save_fig_pickle, load_fig_pickle, save_fig
def save_alot_spec(data_dir):
plots_path = data_dir
if not os.path.exists(plots_path):
os.mkdir(plots_path)
data_name = os.path.basename(data_dir)
sample_rate = get_xhdr_sample_rate(data_dir)
glob_data_iq = load_raw_data(data_dir)
basic_len = get_basic_block_len(sample_rate)
starts = np.random.randint(0, glob_data_iq.shape[0]-basic_len, (sample_per_file,))
print('random starts as indices:')
print(starts)
for j in starts:
gc.collect()
f, axes = plt.subplots(nrows=1, ncols=len(complex2scalar_mode)+1, sharey=True)
## ploting spectrogram
data_iq = glob_data_iq[j:j+basic_len,:]
freqs, time, fft_d = iq2fft(data_iq, sample_rate, rbw)
plt.sca(axes[0])
plt.imshow(fft_d.T, aspect='auto', origin='lower', extent=[time[0], time[-1], freqs[0], freqs[-1]])
plt.title('spectrogram')
## getting index of intrest freqs
intrest_freqs_index = []
for f_0 in intrest_freqs:
intrest_freqs_index.append(np.argmin(np.abs(freqs - f_0)))
## ploting histograms of power
_, _, ffts = iq2fft(glob_data_iq, sample_rate, rbw, mode=complex2scalar_mode)
for l,fft_d in enumerate(ffts):
plt.sca(axes[l+1])
for freq_ind in intrest_freqs_index:
freq_col = fft_d[:, freq_ind]
hist, bins_edge = np.histogram(freq_col, bins=200)
hist = (1e6/np.max(hist)) * hist
plt.plot(bins_edge[:-1], hist+freqs[freq_ind])
print('mode: '+complex2scalar_mode[l]+' --- hist for freq: '+str(freqs[freq_ind]))
plt.title('hist of: '+complex2scalar_mode[l])
f.suptitle(data_name)
f.set_size_inches(12, 6.5, forward=True)
# plt.show()
# input('continue? [y]')
#
fig_path = os.path.join(plots_path, data_name + '_hists')
save_fig(f, fig_path)
plt.close(f)
print('workin on file ' + data_name + ' - {}/{}'.format(i,sample_per_file*num_records))
conf=get_config()
rbw = conf['preprocessing']['ae']['rbw']
normal_path = 'iq_data\\CELL\\normal'
anomal_path = 'iq_data\\CELL\\anomal'
normal_records = os.listdir(normal_path)# discarding train record
anomal_records = os.listdir(anomal_path)
num_records = len(normal_records)+len(anomal_records)
sample_per_file = 1
records = []
preds = []
trues = []
i = 0
complex2scalar_mode = ['power', 'square_abs', 'real', 'angle']
intrest_freqs = [-30e6, -25e6, -17e6, -10e6, -3e6, 4e6, 11e6, 12e6, 13e6, 14e6, 19e6, 25e6, 30e6]
for r in normal_records:
data_dir = os.path.join(normal_path, r)
i+=1
save_alot_spec(data_dir)
for r in anomal_records:
data_dir = os.path.join(anomal_path, r)
i+=1
save_alot_spec(data_dir)