Changeset 368

Show
Ignore:
Timestamp:
07/25/09 17:54:59 (15 years ago)
Author:
sriram
Message:

Back to the good old receive path

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • vtcross/branches/sriram/receive_path.py

    r363 r368  
    2222 
    2323from gnuradio import gr, gru, blks2 
    24 ############## imports specific to spectrum sense ####################### 
    25 from gnuradio import optfir, window 
    26 from gnuradio import audio 
    27 from gnuradio.eng_option import eng_option 
    28 from optparse import OptionParser 
    29 from usrpm import usrp_dbid 
    30 import math 
    31 import struct 
    32 ######################################################################### 
    3324from gnuradio import usrp 
    3425from gnuradio import eng_notation 
    3526import copy 
    3627import sys 
    37 from numpy import random 
     28 
    3829# from current dir 
    3930from pick_bitrate import pick_rx_bitrate 
    4031import usrp_options 
    41 import random 
    4232 
    4333# ///////////////////////////////////////////////////////////////////////////// 
     
    125115        if self._verbose: 
    126116            self._print_verbage() 
    127  
    128         #################################spectrum sense specific code############################# 
    129  
    130  
    131         self.min_freq = 462.4825e6 # setting min and max frequency inside the init rather than taking it from the command line 
    132         self.max_freq = 462.6425e6 
    133  
    134         if self.min_freq > self.max_freq: 
    135             self.min_freq, self.max_freq = self.max_freq, self.min_freq   # swap them 
    136  
    137         self.fft_size = 512 
    138  
    139  
    140         #if not options.real_time: 
    141         #    realtime = False 
    142  
    143         #else: 
    144         #    # Attempt to enable realtime scheduling 
    145         #    r = gr.enable_realtime_scheduling() 
    146         #    if r == gr.RT_OK: 
    147         #        realtime = True 
    148         #    else: 
    149         #        realtime = False 
    150         #        print "Note: failed to enable realtime scheduling" 
    151  
    152         # If the user hasn't set the fusb_* parameters on the command line, 
    153         # pick some values that will reduce latency. 
    154  
    155         #if 1: 
    156         #    if options.fusb_block_size == 0 and options.fusb_nblocks == 0: 
    157         #        if realtime:                        # be more aggressive 
    158         #            options.fusb_block_size = gr.prefs().get_long('fusb', 'rt_block_size', 1024) 
    159         #            options.fusb_nblocks    = gr.prefs().get_long('fusb', 'rt_nblocks', 16) 
    160         #        else: 
    161         #            options.fusb_block_size = gr.prefs().get_long('fusb', 'block_size', 4096) 
    162         #            options.fusb_nblocks    = gr.prefs().get_long('fusb', 'nblocks', 16) 
    163      
    164         #print "fusb_block_size =", options.fusb_block_size 
    165         #print "fusb_nblocks    =", options.fusb_nblocks 
    166  
    167         # build graph 
    168          
    169         #self.u = usrp_options.create_usrp_source(options) 
    170  
    171  
    172         #adc_rate = self.u.adc_rate()                # 64 MS/s 
    173         #usrp_decim = options.decim 
    174         #self.u.set_decim_rate(usrp_decim) 
    175         usrp_rate = self.adc_rate / self._decim 
    176  
    177         #self.u.set_mux(usrp.determine_rx_mux_value(self.u, options.rx_subdev_spec)) 
    178         #self.subdev = usrp.selected_subdev(self.u, options.rx_subdev_spec) 
    179         #print "Using RX d'board %s" % (self.subdev.side_and_name(),) 
    180  
    181  
    182          
    183         s2v = gr.stream_to_vector(gr.sizeof_gr_complex, self.fft_size) 
    184  
    185         mywindow = window.blackmanharris(self.fft_size) 
    186         fft = gr.fft_vcc(self.fft_size, True, mywindow)  
    187         power = 0 
    188         for tap in mywindow: 
    189             power += tap*tap 
    190  
    191          
    192         c2mag = gr.complex_to_mag_squared(self.fft_size) 
    193         print "print c2mag ",c2mag,"\n" 
    194         # FIXME the log10 primitive is dog slow 
    195         log = gr.nlog10_ff(10, self.fft_size, 
    196                            -20*math.log10(self.fft_size)-10*math.log10(power/self.fft_size)) 
    197                  
    198         # Set the freq_step to 75% of the actual data throughput. 
    199         # This allows us to discard the bins on both ends of the spectrum. 
    200  
    201         self.freq_step = 0.75 * usrp_rate 
    202         self.min_center_freq = self.min_freq + self.freq_step/2 
    203         nsteps = math.ceil((self.max_freq - self.min_freq) / self.freq_step) 
    204         self.max_center_freq = self.min_center_freq + (nsteps * self.freq_step) 
    205  
    206         self.next_freq = self.min_center_freq 
    207         tune_delay = 1e-3 
    208         dwell_delay = 1e-2 
    209         tune_delay  = max(0, int(round(tune_delay * usrp_rate / self.fft_size)))  # in fft_frames 
    210         dwell_delay = max(1, int(round(dwell_delay * usrp_rate / self.fft_size))) # in fft_frames 
    211  
    212         self.msgq = gr.msg_queue(16) 
    213         self._tune_callback = tune(self)        # hang on to this to keep it from being GC'd 
    214                  
    215         stats = gr.bin_statistics_f(self.fft_size, self.msgq, 
    216                                     self._tune_callback, tune_delay, dwell_delay) 
    217  
    218  
    219  
    220         # FIXME leave out the log10 until we speed it up 
    221         #self.connect(self.u, s2v, fft, c2mag, log, stats) 
    222         #self.connect(self.u, s2v, fft, c2mag, stats)  
    223          
    224         #if options.gain is None: 
    225         #    # if no gain was specified, use the mid-point in dB 
    226         #    g = self.subdev.gain_range() 
    227         #    options.gain = float(g[0]+g[1])/2 
    228  
    229         #self.set_gain(options.gain) 
    230         #print "gain =", options.gain 
    231          
    232         ########################################################################################## 
    233117             
    234118        self.connect(self.u, self.chan_filt, self.packet_receiver) 
    235         self.connect(self.u, s2v, fft, c2mag, stats)  
    236119 
    237120    def _setup_usrp_source(self, options): 
    238121 
    239122        self.u = usrp_options.create_usrp_source(options) 
    240         self.adc_rate = self.u.adc_rate() 
     123        adc_rate = self.u.adc_rate() 
    241124 
    242125        # derive values of bitrate, samples_per_symbol, and decim from desired info 
    243126        (self._bitrate, self._samples_per_symbol, self._decim) = \ 
    244127            pick_rx_bitrate(self._bitrate, self._demod_class.bits_per_symbol(), \ 
    245                             self._samples_per_symbol, self._decim, self.adc_rate) 
     128                            self._samples_per_symbol, self._decim, adc_rate) 
    246129 
    247130        self.u.set_decim(self._decim) 
     
    318201                          help="Log receive signal power to file (CAUTION: lots of data)") 
    319202 
    320     def set_next_freq(self): 
    321         target_freq = self.next_freq 
    322         self.next_freq = self.next_freq + self.freq_step 
    323         if self.next_freq >= self.max_center_freq: 
    324             self.next_freq = self.min_center_freq 
    325  
    326         if not self.set_freq(target_freq): 
    327             print "Failed to set frequency to", target_freq 
    328  
    329         return target_freq 
    330  
    331203    def _print_verbage(self): 
    332204        """ 
     
    342214        print "Rx Frequency:    %s"    % (eng_notation.num_to_str(self._rx_freq)) 
    343215 
    344     #Getting spectrum information 
    345     #Arguments... threshold - Minimum power equivalent available in spectrum 
    346 #                 trials - Fft frames...should be 1  
    347 #                 exceed count - Number of times threshold is exceeded...useless 
    348  
    349     def get_spec_stats(self,threshold,trials): 
    350         power_sum = 0 #sum of powers(each power value is determined by adding the 'fft square' points..these fft square points are essentially points from the PSD curve...and adding them gives us the power contained in the spectrum) 
    351         counter = 0 
    352         #exceed = 0 
    353         while counter < trials: 
    354                 # Get the next message sent from the C++ code (blocking call). 
    355                 # It contains the center frequency and the mag squared of the fft 
    356                 m = parse_msg(self.msgq.delete_head()) 
    357                 #print "printing mag sq of fft ",sum(m.data),"\n" 
    358                 power_sum = power_sum + sum(m.data) 
    359                 #if sum(m.data) > threshold 
    360                 #       exceed+=1 
    361                 #print sum(m.data),"\n" 
    362                 # Print center freq so we know that something is happening... 
    363                 #print m.center_freq 
    364                 counter +=1 
    365         # FIXME do something useful with the data... 
    366          
    367          
    368         # m.data are the mag_squared of the fft output (they are in the 
    369         # standard order.  I.e., bin 0 == DC.) 
    370         # You'll probably want to do the equivalent of "fftshift" on them 
    371         # m.raw_data is a string that contains the binary floats. 
    372         # You could write this as binary to a file. 
    373         print "here" 
    374         avg_power = power_sum/trials 
    375         print "avg power ",avg_power,"\n" 
    376         #ch = int(random.choice('17')) 
    377         if avg_power >= threshold: 
    378                 return True 
    379                 #return 1,ch 
    380         else: 
    381                 return False 
    382                 #return 0,"no" 
    383         #print "printing average power ",avg_power,"\n" 
    384         #if exceed >= exceed_count: 
    385         #       return avg_power,True  
    386         #       return avg_power,True #True = primary user is present at this frequency  
    387         #else 
    388         #       return avg_power,False 
    389         #       return avg_power,False #False = primary user is NOT present at this frequency  
    390  
    391216def add_freq_option(parser): 
    392217    """ 
     
    402227                          help="set Tx and/or Rx frequency to FREQ [default=%default]", 
    403228                          metavar="FREQ") 
    404  
    405  
    406 ##################### Spectrum sense specific classes################################## 
    407  
    408 class tune(gr.feval_dd): 
    409     """ 
    410     This class allows C++ code to callback into python. 
    411     """ 
    412     def __init__(self, tb): 
    413         gr.feval_dd.__init__(self) 
    414         self.tb = tb 
    415  
    416     def eval(self, ignore): 
    417         """ 
    418         This method is called from gr.bin_statistics_f when it wants to change 
    419         the center frequency.  This method tunes the front end to the new center 
    420         frequency, and returns the new frequency as its result. 
    421         """ 
    422         try: 
    423             # We use this try block so that if something goes wrong from here  
    424             # down, at least we'll have a prayer of knowing what went wrong. 
    425             # Without this, you get a very mysterious: 
    426             # 
    427             #   terminate called after throwing an instance of 'Swig::DirectorMethodException' 
    428             #   Aborted 
    429             # 
    430             # message on stderr.  Not exactly helpful ;) 
    431  
    432             new_freq = self.tb.set_next_freq() 
    433             return new_freq 
    434  
    435         except Exception, e: 
    436             print "tune: Exception: ", e 
    437  
    438  
    439 class parse_msg(object): 
    440     def __init__(self, msg): 
    441         self.center_freq = msg.arg1() 
    442         self.vlen = int(msg.arg2()) 
    443         assert(msg.length() == self.vlen * gr.sizeof_float) 
    444  
    445         # FIXME consider using Numarray or NumPy vector 
    446         t = msg.to_string() 
    447         self.raw_data = t 
    448         self.data = struct.unpack('%df' % (self.vlen,), t) 
    449  
    450  
    451 ########################################################################################