Index: /vtcross/branches/sriram/receive_path.py
===================================================================
--- /vtcross/branches/sriram/receive_path.py	(revision 319)
+++ /vtcross/branches/sriram/receive_path.py	(revision 329)
@@ -115,6 +115,114 @@
         if self._verbose:
             self._print_verbage()
+
+	#################################spectrum sense specific code#############################
+
+
+	self.min_freq = 462.4825e6 # setting min and max frequency inside the init rather than taking it from the command line
+        self.max_freq = 462.4825e6
+
+	if self.min_freq > self.max_freq:
+            self.min_freq, self.max_freq = self.max_freq, self.min_freq   # swap them
+
+	self.fft_size = 128
+
+
+        #if not options.real_time:
+        #    realtime = False
+
+        #else:
+        #    # Attempt to enable realtime scheduling
+        #    r = gr.enable_realtime_scheduling()
+        #    if r == gr.RT_OK:
+        #        realtime = True
+        #    else:
+        #        realtime = False
+        #        print "Note: failed to enable realtime scheduling"
+
+        # If the user hasn't set the fusb_* parameters on the command line,
+        # pick some values that will reduce latency.
+
+        #if 1:
+        #    if options.fusb_block_size == 0 and options.fusb_nblocks == 0:
+        #        if realtime:                        # be more aggressive
+        #            options.fusb_block_size = gr.prefs().get_long('fusb', 'rt_block_size', 1024)
+        #            options.fusb_nblocks    = gr.prefs().get_long('fusb', 'rt_nblocks', 16)
+        #        else:
+        #            options.fusb_block_size = gr.prefs().get_long('fusb', 'block_size', 4096)
+        #            options.fusb_nblocks    = gr.prefs().get_long('fusb', 'nblocks', 16)
+    
+        #print "fusb_block_size =", options.fusb_block_size
+	#print "fusb_nblocks    =", options.fusb_nblocks
+
+        # build graph
+        
+        #self.u = usrp_options.create_usrp_source(options)
+
+
+        #adc_rate = self.u.adc_rate()                # 64 MS/s
+        #usrp_decim = options.decim
+        #self.u.set_decim_rate(usrp_decim)
+        usrp_rate = adc_rate / usrp_decim
+
+        self.u.set_mux(usrp.determine_rx_mux_value(self.u, options.rx_subdev_spec))
+        self.subdev = usrp.selected_subdev(self.u, options.rx_subdev_spec)
+        print "Using RX d'board %s" % (self.subdev.side_and_name(),)
+
+
+	#s2v = gr.stream_to_vector(gr.sizeof_gr_complex, self.fft_size)
+	self.s2v = gr.stream_to_vector(gr.sizeof_gr_complex, self.fft_size) # making it an attribute of the class
+
+        mywindow = window.blackmanharris(self.fft_size)
+        self.fft = gr.fft_vcc(self.fft_size, True, mywindow) #making it an attribute of the class
+        power = 0
+        for tap in mywindow:
+            power += tap*tap
+
+        #c2mag = gr.complex_to_mag_squared(self.fft_size) # making it an attribute of the class
+        self.c2mag = gr.complex_to_mag_squared(self.fft_size)
+	print "print c2mag ",c2mag,"\n"
+        # FIXME the log10 primitive is dog slow
+        log = gr.nlog10_ff(10, self.fft_size,
+                           -20*math.log10(self.fft_size)-10*math.log10(power/self.fft_size))
+		
+        # Set the freq_step to 75% of the actual data throughput.
+        # This allows us to discard the bins on both ends of the spectrum.
+
+        self.freq_step = 0.75 * usrp_rate
+        self.min_center_freq = self.min_freq + self.freq_step/2
+        nsteps = math.ceil((self.max_freq - self.min_freq) / self.freq_step)
+        self.max_center_freq = self.min_center_freq + (nsteps * self.freq_step)
+
+        self.next_freq = self.min_center_freq
+        
+        tune_delay  = max(0, int(round(options.tune_delay * usrp_rate / self.fft_size)))  # in fft_frames
+        dwell_delay = max(1, int(round(options.dwell_delay * usrp_rate / self.fft_size))) # in fft_frames
+
+        self.msgq = gr.msg_queue(16)
+        self._tune_callback = tune(self)        # hang on to this to keep it from being GC'd
+        #stats = gr.bin_statistics_f(self.fft_size, self.msgq,
+        #                            self._tune_callback, tune_delay, dwell_delay)
+	
+	self.stats = gr.bin_statistics_f(self.fft_size, self.msgq,
+                                    self._tune_callback, tune_delay, dwell_delay) #making it an attribute of the class
+
+
+
+        # FIXME leave out the log10 until we speed it up
+	#self.connect(self.u, s2v, fft, c2mag, log, stats)
+	#self.connect(self.u, s2v, fft, c2mag, stats) # taking care of the connect in benchmark_txrx
+	
+        if options.gain is None:
+            # if no gain was specified, use the mid-point in dB
+            g = self.subdev.gain_range()
+            options.gain = float(g[0]+g[1])/2
+
+        self.set_gain(options.gain)
+	print "gain =", options.gain
+	
+	##########################################################################################
             
         self.connect(self.u, self.chan_filt, self.packet_receiver)
+	self.connect(self.u, s2v)
 
     def _setup_usrp_source(self, options):
@@ -227,2 +335,50 @@
                           help="set Tx and/or Rx frequency to FREQ [default=%default]",
                           metavar="FREQ")
+
+
+##################### Spectrum sense specific classes##################################
+
+class tune(gr.feval_dd):
+    """
+    This class allows C++ code to callback into python.
+    """
+    def __init__(self, tb):
+        gr.feval_dd.__init__(self)
+        self.tb = tb
+
+    def eval(self, ignore):
+        """
+        This method is called from gr.bin_statistics_f when it wants to change
+        the center frequency.  This method tunes the front end to the new center
+        frequency, and returns the new frequency as its result.
+        """
+        try:
+            # We use this try block so that if something goes wrong from here 
+            # down, at least we'll have a prayer of knowing what went wrong.
+            # Without this, you get a very mysterious:
+            #
+            #   terminate called after throwing an instance of 'Swig::DirectorMethodException'
+            #   Aborted
+            #
+            # message on stderr.  Not exactly helpful ;)
+
+            new_freq = self.tb.set_next_freq()
+            return new_freq
+
+        except Exception, e:
+            print "tune: Exception: ", e
+
+
+class parse_msg(object):
+    def __init__(self, msg):
+        self.center_freq = msg.arg1()
+        self.vlen = int(msg.arg2())
+        assert(msg.length() == self.vlen * gr.sizeof_float)
+
+        # FIXME consider using Numarray or NumPy vector
+        t = msg.to_string()
+        self.raw_data = t
+        self.data = struct.unpack('%df' % (self.vlen,), t)
+
+
+########################################################################################
