#!/usr/bin/env python3 # -*- coding: utf-8 -*- # # Copyright 2016 <+YOU OR YOUR COMPANY+>. # # This is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3, or (at your option) # any later version. # # This software is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this software; see the file COPYING. If not, write to # the Free Software Foundation, Inc., 51 Franklin Street, # Boston, MA 02110-1301, USA. # import numpy as np from gnuradio import gr import code,math,pmt,time,threading class station_search(gr.sync_block): """ docstring for block station_search """ def __init__(self, fft_len=1024,num_decoders=4,center_freq=0,samp_rate=0,round_to=100e3,debug=False): gr.sync_block.__init__(self, name="station_search", in_sig=[(np.float32,fft_len)], out_sig=None) self.fft_len=fft_len self.num_decoders=num_decoders self.center_freq=center_freq self.samp_rate=samp_rate 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.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 #(freq-startfreq)*self.fft_len/self.samp_rate=index index=(freq-startfreq)*self.fft_len/self.samp_rate return index def index_to_freq(self,index): startfreq=self.center_freq-self.samp_rate/2 freq=index*self.samp_rate/self.fft_len+startfreq return freq def handle_ctrl_msg(self,msg): #if pmt.is_dict(msg):#check doesnt work, since pairs pass is_dict 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_indexthreshold) station_indices=[] try: last_index=max_indices[0][0] except IndexError: last_index=0 count=1#counts number of consecutive maximums threshold_reached=False # 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 for i in max_indices: if abs(i-last_index) <= fuzzyness: count+=i-last_index else:#last streak ended if(threshold_reached): station_indices.append(last_index-int(count/2))#use center of max-streak threshold_reached=False count=1 else:#last streak didn't reach threshold -> no station count=1 if count>=min_consec_max_threshold: threshold_reached=True last_index=i same_station_threshold=int(100000*self.fft_len/self.samp_rate)#0.1mhz #group similar indices: lastindex=0 group=None station_indices_grouped=[] for idx in station_indices: if group==None: #start new group group=[idx] elif (idx-lastindex)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) print("last_index:%s"%self.last_station_indices) print("tune_index:%s"%station_indices_tune) self.last_station_indices=station_indices_tune#save current stations to compare againts next station_strength=[] station_freqs=[] #index to freq: for index in station_indices_tune: startfreq=self.center_freq-self.samp_rate/2 freq=self.samp_rate*index/self.fft_len+startfreq #freq+=30000#add 30k because detected max often too low num_decimals=int(round(math.log(self.snapto,10))) 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) 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: return len(input_items[0])