def preprocess_test_data(self, iq_data, sample_rate,rbw=None): ## getting spectrogram if rbw: _, time, fft_d = iq2fft(iq_data, sample_rate, rbw) else: _, time, fft_d = iq2fft(iq_data, sample_rate, self.rbw) ## scaling spectrogram if use_scaling: fft_d = scale_test_vectors(fft_d, self.scaler) return (time, fft_d)
def save_alot_spec(data_dir): plots_path = os.path.join(data_dir, 'samples') 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) freqs, _, fft_d = iq2fft(glob_data_iq, sample_rate, rbw) glob_means = np.mean(fft_d, axis=0) glob_stds = np.std(fft_d, axis=0) del fft_d gc.collect() basic_len = get_basic_block_len(sample_rate) starts = np.random.randint(0, glob_data_iq.shape[0] - basic_len, (5, )) print('random starts as indices:') print(starts) for ind, j in enumerate(starts): data_iq = glob_data_iq[j:j + basic_len, :] _, time, fft_d = iq2fft(data_iq, sample_rate, rbw) means = np.mean(fft_d, axis=0) stds = np.std(fft_d, axis=0) f, axes = plt.subplots(nrows=2, ncols=1, sharex=True) plt.sca(axes[0]) plt.plot(freqs, glob_means, color='black', linewidth=3) plt.plot(freqs, glob_means - glob_stds, color='red', linewidth=3) plt.plot(freqs, glob_means + glob_stds, color='red', linewidth=3) plt.plot(freqs, means, color='green', linewidth=1) plt.plot(freqs, means - stds, color='green', linewidth=1) plt.plot(freqs, means + stds, color='green', linewidth=1) plt.sca(axes[1]) plt.imshow(fft_d, aspect='auto', origin='lower', extent=[freqs[0], freqs[-1], time[0], time[-1]]) f.suptitle(data_name) f.suptitle('file: ' + data_name + \ '\nstart index = ' + str(j)) f.set_size_inches(8, 6.5, forward=True) # plt.show() # input('continue? [y]') fig_path = os.path.join(plots_path, data_name + '_sample_' + str(ind)) save_fig(f, fig_path) plt.close(f) print('workin on file ' + data_name + ' - {}/{}'.format(i, 5 * num_records))
def preprocess_train_data(self, iq_data,sample_rate,rbw=None): scaler_path = os.path.join(self.model_path, "train_scaler.pkl") ## getting spectrogram if rbw: self.freqs, time, fft_d = iq2fft(iq_data, sample_rate, rbw) else: self.freqs, time, fft_d = iq2fft(iq_data, sample_rate, self.rbw) ## scaling spectrogram if use_scaling: (fft_d, scaler) = scale_train_vectors(fft_d, scaler_path, rng=feature_range) self.scaler = scaler return (time, fft_d)
def preprocess_test_data(self, iq_data, sample_rate,rbw=None): ## getting spectrogram if rbw: self.rbw = rbw self.freqs, time, fft_list = iq2fft(iq_data, sample_rate, self.rbw, mode=['real', 'imag']) fft_d = np.stack(fft_list, axis=-1) return (time, fft_d)
def preprocess_test_data(self, iq_data, sample_rate): self.scaler_path = os.path.join(self.model_path, "train_scaler.pkl") self.freqs, time, fft_test = iq2fft(iq_data, sample_rate, self.rbw) ## scaling spectrogram if use_scaling: fft_test = scale_test_vectors(fft_test, self.scaler_path) ## getting spectrogram return (time, fft_test)
def preprocess_test_data(self, iq_data, sample_rate, rbw=None): self.whiten_path = os.path.join(self.model_path, "zca_scaler.pkl") ## whitening if use_whitening: iq_data = whiten_test_data(iq_data, self.whiten_path) ## getting spectrogram freqs, time, fft_d = iq2fft(iq_data, sample_rate, self.rbw) ## scaling spectrogram if use_scaling: fft_d = scale_test_vectors(fft_d, scaler=self.scaler) return ((freqs, time), fft_d)
def preprocess_train(self, iq_data, sample_rate): params_path = os.path.join(self.model_path, "model_params.pkl") ## getting spectrogram self.freqs, time, fft_list = iq2fft(iq_data, sample_rate, self.rbw, mode=['real', 'imag']) fft_iq = np.stack(fft_list, axis=-1) self.means = np.mean(fft_iq, axis=0) self.stds = np.std(fft_iq, axis=0) self.gen_gaussians() params_dic = {'freqs': self.freqs, 'means': self.means, 'stds': self.stds, 'gaussians': self.gaussians} persist_object(params_dic, params_path) self.loaded = True
def plot_prediction(self, iq_data_basic_block, sample_rate): ## get only basic_block_len basic_len = get_basic_block_len(sample_rate, basic_time) if basic_len != iq_data_basic_block.shape[0]: raise("iq_data not 1 [msec]...") _, pred_matrix = self.predict_basic_block(iq_data_basic_block, sample_rate) pred_matrix[0,0] = 0 pred_matrix[-1,-1] = 7 _, time, fft_d = iq2fft(iq_data_basic_block, sample_rate, self.rbw) imshow_limits = [self.freqs[0], self.freqs[-1], time[0], time[-1]] fig, axes = plt.subplots(nrows=1, ncols=2, sharex=True, sharey=True) plt.sca(axes[0]) pred_im = plt.imshow(pred_matrix, aspect='auto', origin='lower', extent=imshow_limits) plt.colorbar(pred_im) plt.sca(axes[1]) fft_im = plt.imshow(fft_d, aspect='auto', origin='lower', extent=imshow_limits) plt.colorbar(fft_im)
def plot_prediction(self, iq_data_basic_block, sample_rate): ## get only basic_block_len basic_len = get_basic_block_len(sample_rate, basic_time) if basic_len != iq_data_basic_block.shape[0]: raise ("iq_data too long...") pred_freqs, pred_time, pred_matrix = self.predict_basic_block( iq_data_basic_block, sample_rate) pred_matrix[-1, -1] = sigma_ae freqs, time, fft_d = iq2fft(iq_data_basic_block, sample_rate, self.rbw) fig, axes = plt.subplots(nrows=1, ncols=2, sharex=True, sharey=True) plt.sca(axes[0]) plt.imshow(pred_matrix, aspect='auto', origin='lower', extent=[ pred_freqs[0], pred_freqs[-1], pred_time[0], pred_time[-1] ]) plt.sca(axes[1]) plt.imshow(fft_d, aspect='auto', origin='lower', extent=[freqs[0], freqs[-1], time[0], time[-1]])
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))