|
|
|
|
@ -21,7 +21,7 @@
|
|
|
|
|
|
|
|
|
|
import numpy as np |
|
|
|
|
from gnuradio import gr |
|
|
|
|
import code,math,pmt,time |
|
|
|
|
import code,math,pmt,time,threading |
|
|
|
|
|
|
|
|
|
class max_freq(gr.sync_block): |
|
|
|
|
""" |
|
|
|
|
@ -39,12 +39,13 @@ class max_freq(gr.sync_block):
|
|
|
|
|
self.snapto=round_to |
|
|
|
|
self.debug=debug |
|
|
|
|
self.last_station_indices=[0]*self.num_decoders |
|
|
|
|
self.message_port_register_out(pmt.intern('out')) |
|
|
|
|
self.timer=time.time() |
|
|
|
|
self.message_port_register_in(pmt.intern('ctrl')) |
|
|
|
|
self.set_msg_handler(pmt.intern('ctrl'), self.handle_ctrl_msg) |
|
|
|
|
self.searchMode=True |
|
|
|
|
self.index_fixed=[False]*self.num_decoders |
|
|
|
|
self.message_port_register_out(pmt.intern('out')) |
|
|
|
|
self.timer=time.time() |
|
|
|
|
self.message_port_register_in(pmt.intern('ctrl')) |
|
|
|
|
self.set_msg_handler(pmt.intern('ctrl'), self.handle_ctrl_msg) |
|
|
|
|
self.searchMode=True |
|
|
|
|
self.index_fixed=[False]*self.num_decoders |
|
|
|
|
self.search_thread=None |
|
|
|
|
def freq_to_index(self,freq): |
|
|
|
|
startfreq=self.center_freq-self.samp_rate/2 |
|
|
|
|
#freq=self.samp_rate*index/self.fft_len+startfreq |
|
|
|
|
@ -61,26 +62,26 @@ class max_freq(gr.sync_block):
|
|
|
|
|
try: |
|
|
|
|
m = pmt.pmt_to_python.pmt_to_dict(msg) |
|
|
|
|
if m.has_key("cmd") and m["cmd"]=="set_audio_freq": |
|
|
|
|
#print(m) |
|
|
|
|
#print(self.last_station_indices) |
|
|
|
|
freq_index=self.freq_to_index(m["freq"]) |
|
|
|
|
if m["chan"]=="left" and freq_index<self.fft_len-5: |
|
|
|
|
if self.last_station_indices[0]==freq_index: |
|
|
|
|
self.index_fixed[0]=False |
|
|
|
|
print("decoder 0 free") |
|
|
|
|
else: |
|
|
|
|
self.last_station_indices[0]=freq_index |
|
|
|
|
self.index_fixed[0]=True |
|
|
|
|
print("decoder 0 fixed to %i"%m["freq"]) |
|
|
|
|
if m["chan"]=="right" and freq_index<self.fft_len-5: |
|
|
|
|
if self.last_station_indices[1]==freq_index: |
|
|
|
|
self.index_fixed[1]=False |
|
|
|
|
print("decoder 1 free") |
|
|
|
|
else: |
|
|
|
|
self.last_station_indices[1]=freq_index |
|
|
|
|
self.index_fixed[1]=True |
|
|
|
|
print("decoder 1 fixed to %i"%m["freq"]) |
|
|
|
|
#print(self.last_station_indices) |
|
|
|
|
#print(m) |
|
|
|
|
#print(self.last_station_indices) |
|
|
|
|
freq_index=self.freq_to_index(m["freq"]) |
|
|
|
|
if m["chan"]=="left" and freq_index<self.fft_len-5: |
|
|
|
|
if self.last_station_indices[0]==freq_index: |
|
|
|
|
self.index_fixed[0]=False |
|
|
|
|
print("decoder 0 free") |
|
|
|
|
else: |
|
|
|
|
self.last_station_indices[0]=freq_index |
|
|
|
|
self.index_fixed[0]=True |
|
|
|
|
print("decoder 0 fixed to %i"%m["freq"]) |
|
|
|
|
if m["chan"]=="right" and freq_index<self.fft_len-5: |
|
|
|
|
if self.last_station_indices[1]==freq_index: |
|
|
|
|
self.index_fixed[1]=False |
|
|
|
|
print("decoder 1 free") |
|
|
|
|
else: |
|
|
|
|
self.last_station_indices[1]=freq_index |
|
|
|
|
self.index_fixed[1]=True |
|
|
|
|
print("decoder 1 fixed to %i"%m["freq"]) |
|
|
|
|
#print(self.last_station_indices) |
|
|
|
|
if m.has_key("cmd") and m["cmd"]=="switch mode": |
|
|
|
|
self.searchMode=not self.searchMode |
|
|
|
|
print("searchMode: %s"%self.searchMode) |
|
|
|
|
@ -89,19 +90,14 @@ class max_freq(gr.sync_block):
|
|
|
|
|
def set_center_freq(self, freq=None): |
|
|
|
|
self.index_fixed=[False]*self.num_decoders#free all decoders (freq wouldn't match anyways) |
|
|
|
|
if freq is not None: |
|
|
|
|
if isinstance(freq, float) or isinstance(freq, int): |
|
|
|
|
self.center_freq=freq |
|
|
|
|
else: |
|
|
|
|
self.center_freq = int(freq) |
|
|
|
|
def work(self, input_items, output_items): |
|
|
|
|
if time.time()-self.timer<2:#every 2 seconds |
|
|
|
|
return len(input_items[0]) |
|
|
|
|
elif self.searchMode: |
|
|
|
|
#in0 = input_items[0] |
|
|
|
|
#ii=input_items |
|
|
|
|
if isinstance(freq, float) or isinstance(freq, int): |
|
|
|
|
self.center_freq=freq |
|
|
|
|
else: |
|
|
|
|
self.center_freq = int(freq) |
|
|
|
|
def search(self,inputvector): |
|
|
|
|
carrier_width=2 |
|
|
|
|
carrier=self.fft_len/2 |
|
|
|
|
numbers=np.delete(input_items[0][0],range(carrier-carrier_width,carrier+carrier_width+1))#read input and disregard center (hackrf LO) |
|
|
|
|
numbers=np.delete(inputvector[0],range(carrier-carrier_width,carrier+carrier_width+1))#read input and disregard center (hackrf LO) |
|
|
|
|
#threshold=40# uni |
|
|
|
|
#threshold=60#home |
|
|
|
|
#threshold=np.mean(numbers)#2017-03-21 fft-multi-decoder |
|
|
|
|
@ -129,10 +125,10 @@ class max_freq(gr.sync_block):
|
|
|
|
|
# max_indices[0].append(0)#to detect last station |
|
|
|
|
max_indices=np.append(max_indices,0)#to detect last station |
|
|
|
|
#try: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#max_indices.remove(self.fft_len/2)#suppress local oscillator of hackrf |
|
|
|
|
#except ValueError: |
|
|
|
|
#pass |
|
|
|
|
#pass |
|
|
|
|
for i in max_indices: |
|
|
|
|
if abs(i-last_index) <= fuzzyness: |
|
|
|
|
count+=i-last_index |
|
|
|
|
@ -178,26 +174,26 @@ class max_freq(gr.sync_block):
|
|
|
|
|
new_stations=[] |
|
|
|
|
#add fixed stations (currently unused): |
|
|
|
|
for i,old_freq in enumerate(self.last_station_indices): |
|
|
|
|
if self.index_fixed[i]: |
|
|
|
|
station_indices_tune[i]=old_freq |
|
|
|
|
if self.index_fixed[i]: |
|
|
|
|
station_indices_tune[i]=old_freq |
|
|
|
|
#change existing/add new: |
|
|
|
|
for new_freq in station_indices_trunc: |
|
|
|
|
added=False |
|
|
|
|
for i,old_freq in enumerate(self.last_station_indices): |
|
|
|
|
if abs(old_freq-new_freq)<same_station_threshold: |
|
|
|
|
station_indices_tune[i]=new_freq |
|
|
|
|
added=True |
|
|
|
|
if not added: |
|
|
|
|
new_stations.append(new_freq) |
|
|
|
|
#print("tune1:%s"%station_indices_tune) |
|
|
|
|
if self.debug: |
|
|
|
|
added=False |
|
|
|
|
for i,old_freq in enumerate(self.last_station_indices): |
|
|
|
|
if abs(old_freq-new_freq)<same_station_threshold: |
|
|
|
|
station_indices_tune[i]=new_freq |
|
|
|
|
added=True |
|
|
|
|
if not added: |
|
|
|
|
new_stations.append(new_freq) |
|
|
|
|
#print("tune1:%s"%station_indices_tune) |
|
|
|
|
if self.debug: |
|
|
|
|
print("tunc:%s"%station_indices_trunc) |
|
|
|
|
print("new_1:%s"%new_stations) |
|
|
|
|
|
|
|
|
|
for i,tune_freq in enumerate(station_indices_tune): |
|
|
|
|
if tune_freq == 0 and len(new_stations)>0:#add new stations to empty decoders |
|
|
|
|
station_indices_tune[i]=new_stations.pop() |
|
|
|
|
#print("tune2:%s"%station_indices_tune) |
|
|
|
|
print("new_1:%s"%new_stations) |
|
|
|
|
|
|
|
|
|
for i,tune_freq in enumerate(station_indices_tune): |
|
|
|
|
if tune_freq == 0 and len(new_stations)>0:#add new stations to empty decoders |
|
|
|
|
station_indices_tune[i]=new_stations.pop() |
|
|
|
|
#print("tune2:%s"%station_indices_tune) |
|
|
|
|
|
|
|
|
|
if self.debug: |
|
|
|
|
print("new_2:%s"%new_stations) |
|
|
|
|
@ -216,19 +212,35 @@ class max_freq(gr.sync_block):
|
|
|
|
|
station_freqs.append(round(freq,-num_decimals)) |
|
|
|
|
station_strength.append(round(numbers[index],-2)) |
|
|
|
|
for i in range(0,min(self.num_decoders,len(station_freqs))): |
|
|
|
|
msg_string=str(i+1)+" "+str(station_freqs[i]) |
|
|
|
|
send_pmt = pmt.string_to_symbol(msg_string) |
|
|
|
|
self.message_port_pub(pmt.intern('out'), send_pmt) |
|
|
|
|
if self.debug: |
|
|
|
|
#print(max_indices) |
|
|
|
|
#print(np.mean(numbers)) |
|
|
|
|
#print(len(max_indices)) |
|
|
|
|
#print(station_indices) |
|
|
|
|
#print(station_indices_grouped) |
|
|
|
|
#print(station_indices_sorted) |
|
|
|
|
#print(station_indices_tune) |
|
|
|
|
#print(station_strength) |
|
|
|
|
print(station_freqs) |
|
|
|
|
msg_string=str(i+1)+" "+str(station_freqs[i]) |
|
|
|
|
send_pmt = pmt.string_to_symbol(msg_string) |
|
|
|
|
self.message_port_pub(pmt.intern('out'), send_pmt) |
|
|
|
|
if self.debug: |
|
|
|
|
#print(max_indices) |
|
|
|
|
#print(np.mean(numbers)) |
|
|
|
|
#print(len(max_indices)) |
|
|
|
|
#print(station_indices) |
|
|
|
|
#print(station_indices_grouped) |
|
|
|
|
#print(station_indices_sorted) |
|
|
|
|
#print(station_indices_tune) |
|
|
|
|
#print(station_strength) |
|
|
|
|
print(station_freqs) |
|
|
|
|
def work(self, input_items, output_items): |
|
|
|
|
if time.time()-self.timer<2:#every 2 seconds |
|
|
|
|
return len(input_items[0]) |
|
|
|
|
elif self.searchMode: |
|
|
|
|
inputvector=input_items[0] |
|
|
|
|
if self.search_thread == None or self.search_thread.is_alive()==False: |
|
|
|
|
if self.debug: |
|
|
|
|
print("starting thread") |
|
|
|
|
self.search_thread = threading.Thread(target=self.search, args=[inputvector], kwargs={}) |
|
|
|
|
self.search_thread.start() |
|
|
|
|
elif self.debug: |
|
|
|
|
print("thread exists") |
|
|
|
|
|
|
|
|
|
#in0 = input_items[0] |
|
|
|
|
#ii=input_items |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return len(input_items[0]) |
|
|
|
|
else: |
|
|
|
|
|