def waterfall_next(self, waterfall_last, action): waterfall_n = waterfall_last.copy() sub_pow_ls = [] jamm_ls = copy.copy(self.jamm_pos) self.set_user_freq(action) w_f = usrp_spetrum_sense.main(self.tb, 1) #waterfall_lib_mean = np.mean(self.waterfall_lib[action], 0).reshape(1, 1, 1601, 1) #sub_waterfall = w_f - waterfall_lib_mean jamm_ls.append( int(self.jammer_obj.get_center_freq() - 100500000) / 500000) """ for i in range(self.n_actions): pow_ = np.sum(sub_waterfall[0,0,(i + 1) * 80 - 2:(i + 1) * 80 + 3, 0]) sub_pow_ls.append(pow_) if pow_ > 40: jamm_ls.append(i) """ for i in range(self.line_num): if i < self.line_num - 1: waterfall_n[0, i] = waterfall_n[0, i + 1] else: waterfall_n[0, i] = w_f #print(sub_pow_ls) return waterfall_n, jamm_ls
def waterfall_lib(self): self.main_thread() waterfall_lib = np.zeros(shape=(self.n_actions, 50, 1601, 1), dtype=float) for i in range(self.n_actions): self.set_user_freq(self.action_space[i]) waterfall = usrp_spetrum_sense.main(self.tb) waterfall_lib[i] = waterfall self.user_obj.stop() return waterfall_lib
def waterfall_lib(self): waterfall_lib = np.zeros(shape=(self.n_actions, 50, 1601, 1), dtype=float) for i in range(self.n_actions): self.set_user_freq(self.action_space[i]) waterfall = usrp_spetrum_sense.main(self.tb, 50) waterfall_lib[i] = waterfall t = threading.Thread(target=self.main_jammer_tx) t.start() return waterfall_lib
def waterfall_next(self, waterfall_last, action): waterfall_n = waterfall_last.copy() self.set_user_freq(self.action_space[action]) w_f = usrp_spetrum_sense.main(self.tb) for i in range(self.line_num): if i < self.line_num - 1: waterfall_n[0, i, :, 0] = waterfall_n[0, i + 1, :, 0] else: waterfall_n[0, i, :, 0] = w_f[0, 0, :, 0] return waterfall_n
def spect_sense(self): root = tk.Tk() root.title("matplotlib in TK") f = Figure(figsize=(7, 7), dpi=200) canvas = FigureCanvasTkAgg(f, master=root) canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1) x = 0 while True: x = x + 1 if x < 18 else 0 self.set_user_freq(self.action_space[x]) w_f = usrp_spetrum_sense.main(self.tb) f.clf() waterfall_figure = f.add_subplot(111) waterfall_figure.plot(w_f[0, -1]) canvas.draw()
def __init__(self): super(freq_env, self).__init__() self.action_space = [ i for i in np.arange(100000000.0, 110000001.0, 1000000) ] self.n_actions = len(self.action_space) self.n_features = 1601 self.jammer_obj = jammer() self.user_obj = user() self.action_ls = [100000000.0] self.start_freq = 129000000.0 self.end_freq = 130000000.0 self.tb = usrp_spetrum_sense.my_top_block() self.tb.start() self.wf, self.min_index, self.user_f, self.sum_ls = usrp_spetrum_sense.main( self.tb)
def spect_sense(self): while True: self.set_user_freq(self.user_f) w_f, min_i, userfreq, sumls = usrp_spetrum_sense.main(self.tb) x = 3 if self.min_index == 0 or self.min_index == 1 or self.min_index == 2 or self.min_index == 3 or self.min_index == 4: if sumls[self. min_index] > x * self.sum_ls[self.min_index] or sumls[ self.min_index + 2] > x * self.sum_ls[self.min_index + 2] or sumls[ self.min_index + 4] > x * self.sum_ls[self.min_index + 4]: self.user_f = userfreq self.wf = w_f self.min_index = min_i self.sum_ls = sumls self.set_user_freq(self.user_f) elif self.min_index == 39 or self.min_index == 38 or self.min_index == 37 or self.min_index == 36 or self.min_index == 35: if sumls[self. min_index] > x * self.sum_ls[self.min_index] or sumls[ self.min_index - 2] > x * self.sum_ls[self.min_index - 2] or sumls[ self.min_index - 4] > x * self.sum_ls[self.min_index - 4]: self.user_f = userfreq self.wf = w_f self.min_index = min_i self.sum_ls = sumls self.set_user_freq(self.user_f) else: if sumls[self. min_index] > x * self.sum_ls[self.min_index] or sumls[ self.min_index - 2] > x * self.sum_ls[self.min_index - 2] or sumls[ self.min_index - 4] > x * self.sum_ls[ self.min_index - 4] or sumls[ self.min_index + 2] > x * self.sum_ls[ self.min_index + 2] or sumls[ self.min_index + 4] > x * self.sum_ls[ self.min_index + 4]: self.user_f = userfreq self.wf = w_f self.min_index = min_i self.sum_ls = sumls self.set_user_freq(self.user_f) time.sleep(2)
def waterfall_background(self): self.jamm_pos = [] self.freq_pow = [] waterfall_bg = usrp_spetrum_sense.main(self.tb) for i in range(self.n_actions): sum_freq_1 = np.sum(waterfall_bg[0, -1, (2 * i + 1) * 40:(2 * i + 3) * 40, 0]) self.freq_pow.append(sum_freq_1) #sum_freq_2 = np.sum(waterfall_bg[0, 0, (2 * i + 1) * 40:(2 * i + 3) * 40, 0]) #sum_freq_3 = np.sum(waterfall_bg[0, 24, (2 * i + 1) * 40:(2 * i + 3) * 40, 0]) if sum_freq_1 > 450: self.jamm_pos.append(i) print(self.freq_pow) print(self.jamm_pos) return waterfall_bg
def step(self, action): # ---------------------------------define reward------------------------------ jammer_freq = self.get_jammer_freq() print("jammer_freq---------------------", jammer_freq) if self.action_space[action] - jammer_freq > 100000.0: if self.action_ls[-1] == self.action_space[action]: reward = 1 done = True else: reward = 0.8 done = True else: reward = -1 done = False self.action_ls.append(self.action_space[action]) #---------------------------------define s_ ---------------------------------- self.set_user_freq(self.action_space[action]) s_ = usrp_spetrum_sense.main() return s_, reward, done
def waterfall_background(self): self.jamm_pos = [] self.freq_pow = [] waterfall_bg = usrp_spetrum_sense.main(self.tb, 100) self.waterfall_bg_mean = np.mean(waterfall_bg[0], 0).reshape(1, 1, 1601, 1) for i in range(self.n_actions): sum_freq_1 = np.sum( self.waterfall_bg_mean[0, 0, (i + 1) * 80 - 40:(i + 1) * 80 + 41, 0]) self.freq_pow.append(sum_freq_1) # sum_freq_2 = np.sum(waterfall_bg[0, 0, (2 * i + 1) * 40:(2 * i + 3) * 40, 0]) # sum_freq_3 = np.sum(waterfall_bg[0, 24, (2 * i + 1) * 40:(2 * i + 3) * 40, 0]) if sum_freq_1 > 280: self.jamm_pos.append(i) #print(self.freq_pow) #print(self.jamm_pos) return waterfall_bg
def waterfall_reset(self): self.current_line = -50 self.jamm_pos = [] self.freq_pow = [] rand_int = random.randint(0, self.n_actions - 1) self.action_last = rand_int waterfall_reset = np.zeros(shape=(1, 50, 1601, 1), dtype=float) self.line_num = list(waterfall_reset.shape)[1] self.set_user_freq(self.action_space[rand_int]) w_f_reset = usrp_spetrum_sense.main(self.tb) for i in range(self.line_num): self.current_line += 1 waterfall_reset[0, i, :, 0] = self.power_ls[(self.index_end[0] + 1) * i:(self.index_end[0] + 1) * i + self.index_end[0] + 1] for i in range(self.n_actions): sum_freq = np.sum(waterfall_reset[0, -1, (2 * i + 1) * 40:(2 * i + 3) * 40, 0]) self.freq_pow.append(sum_freq) if sum_freq > 400: self.jamm_pos.append(i) #print(self.freq_pow) #print(self.jamm_pos) #plt.subplot(211) #for i in range(19): #plt.axvline((i+1)*80,color='red') #plt.plot(waterfall_reset[0,-1]) waterfall_reset[0, -1, :, 0] = w_f_reset[0, 0, :, 0] #plt.subplot(212) #plt.plot(w_f[0, -1]) #plt.show() return waterfall_reset pass
def reset(self): self.action_ls = [100000000.0] self.set_user_freq(100000000.0) self.s, self.userfreq = usrp_spetrum_sense.main() return self.s