Index: /vtcross/branches/sriram/receive_path.py
===================================================================
--- /vtcross/branches/sriram/receive_path.py	(revision 329)
+++ /vtcross/branches/sriram/receive_path.py	(revision 340)
@@ -22,4 +22,13 @@
 
 from gnuradio import gr, gru, blks2
+############## imports specific to spectrum sense #######################
+from gnuradio import optfir, window
+from gnuradio import audio
+from gnuradio.eng_option import eng_option
+from optparse import OptionParser
+from usrpm import usrp_dbid
+import math
+import struct
+#########################################################################
 from gnuradio import usrp
 from gnuradio import eng_notation
@@ -120,5 +129,5 @@
 
 	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
+        self.max_freq = 462.6425e6
 
 	if self.min_freq > self.max_freq:
@@ -163,22 +172,22 @@
         #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
+        usrp_rate = self.adc_rate / self._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)
 
         mywindow = window.blackmanharris(self.fft_size)
-        self.fft = gr.fft_vcc(self.fft_size, True, mywindow) #making it an attribute of the class
+        fft = gr.fft_vcc(self.fft_size, True, mywindow) 
         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)
+        
+        c2mag = gr.complex_to_mag_squared(self.fft_size)
 	print "print c2mag ",c2mag,"\n"
         # FIXME the log10 primitive is dog slow
@@ -195,15 +204,14 @@
 
         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
+        tune_delay = 1e-3
+	dwell_delay = 1e-2
+        tune_delay  = max(0, int(round(tune_delay * usrp_rate / self.fft_size)))  # in fft_frames
+        dwell_delay = max(1, int(round(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
+        	
+	stats = gr.bin_statistics_f(self.fft_size, self.msgq,
+                                    self._tune_callback, tune_delay, dwell_delay)
 
 
@@ -211,28 +219,28 @@
         # 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
+	#self.connect(self.u, s2v, fft, c2mag, stats) 
 	
-        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
+        #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)
+	self.connect(self.u, s2v, fft, c2mag, stats) 
 
     def _setup_usrp_source(self, options):
 
         self.u = usrp_options.create_usrp_source(options)
-        adc_rate = self.u.adc_rate()
+        self.adc_rate = self.u.adc_rate()
 
         # derive values of bitrate, samples_per_symbol, and decim from desired info
         (self._bitrate, self._samples_per_symbol, self._decim) = \
             pick_rx_bitrate(self._bitrate, self._demod_class.bits_per_symbol(), \
-                            self._samples_per_symbol, self._decim, adc_rate)
+                            self._samples_per_symbol, self._decim, self.adc_rate)
 
         self.u.set_decim(self._decim)
@@ -309,4 +317,15 @@
                           help="Log receive signal power to file (CAUTION: lots of data)")
 
+    def set_next_freq(self):
+        target_freq = self.next_freq
+        self.next_freq = self.next_freq + self.freq_step
+        if self.next_freq >= self.max_center_freq:
+            self.next_freq = self.min_center_freq
+
+        if not self.set_freq(target_freq):
+            print "Failed to set frequency to", target_freq
+
+        return target_freq
+
     def _print_verbage(self):
         """
@@ -321,4 +340,30 @@
         print "decim:           %3d"   % (self._decim)
         print "Rx Frequency:    %s"    % (eng_notation.num_to_str(self._rx_freq))
+
+    def get_avg_power(self,trials):
+    	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)
+    	counter = 0
+	
+    	while counter < 10 :
+
+        	# Get the next message sent from the C++ code (blocking call).
+        	# It contains the center frequency and the mag squared of the fft
+        	m = parse_msg(self.msgq.delete_head())
+		#print "printing mag sq of fft ",sum(m.data),"\n"
+		#if sum(m.data) > 1e12:
+		power_sum = power_sum + sum(m.data)
+        	# Print center freq so we know that something is happening...
+        	#print m.center_freq
+		counter +=1
+        # FIXME do something useful with the data...
+        
+        # m.data are the mag_squared of the fft output (they are in the
+        # standard order.  I.e., bin 0 == DC.)
+        # You'll probably want to do the equivalent of "fftshift" on them
+        # m.raw_data is a string that contains the binary floats.
+        # You could write this as binary to a file.
+
+    	avg_power = power_sum/counter
+    	print "printing average power ",avg_power,"\n"
 
 def add_freq_option(parser):
