From 9b47ed5a0efa26a25a40d438ea8028c51ea18312 Mon Sep 17 00:00:00 2001
From: Mike Jameson <mike@scanoo.com>
Date: Sun, 24 Mar 2013 17:12:45 +0000
Subject: uhd: updated usrp_spectrum_sense with frequency readout

---
 gr-uhd/examples/python/usrp_spectrum_sense.py | 45 +++++++++++++++++++++------
 1 file changed, 35 insertions(+), 10 deletions(-)

(limited to 'gr-uhd/examples/python/usrp_spectrum_sense.py')

diff --git a/gr-uhd/examples/python/usrp_spectrum_sense.py b/gr-uhd/examples/python/usrp_spectrum_sense.py
index 32980adbfa..71f1f8c266 100755
--- a/gr-uhd/examples/python/usrp_spectrum_sense.py
+++ b/gr-uhd/examples/python/usrp_spectrum_sense.py
@@ -104,7 +104,10 @@ class my_top_block(gr.top_block):
                           help="time to delay (in seconds) after changing frequency [default=%default]")
         parser.add_option("", "--dwell-delay", type="eng_float",
                           default=10e-3, metavar="SECS",
-                          help="time to dwell (in seconds) at a given frequncy [default=%default]")
+                          help="time to dwell (in seconds) at a given frequency [default=%default]")
+        parser.add_option("", "--channel-bandwidth", type="eng_float",
+                          default=12.5e3, metavar="Hz",
+                          help="channel bandwidth of fft bins in Hz [default=%default]")
         parser.add_option("-F", "--fft-size", type="int", default=256,
                           help="specify number of FFT bins [default=%default]")
         parser.add_option("", "--real-time", action="store_true", default=False,
@@ -122,8 +125,9 @@ class my_top_block(gr.top_block):
             # swap them
             self.min_freq, self.max_freq = self.max_freq, self.min_freq
 
-	self.fft_size = options.fft_size
-
+        self.fft_size = options.fft_size
+        self.channel_bandwidth = options.channel_bandwidth
+        
         if not options.real_time:
             realtime = False
         else:
@@ -147,14 +151,14 @@ class my_top_block(gr.top_block):
         if(options.antenna):
             self.u.set_antenna(options.antenna, 0)
 
-        usrp_rate = options.samp_rate
+        self.usrp_rate = usrp_rate = options.samp_rate
         self.u.set_samp_rate(usrp_rate)
         dev_rate = self.u.get_samp_rate()
 
-	s2v = gr.stream_to_vector(gr.sizeof_gr_complex, self.fft_size)
+        s2v = gr.stream_to_vector(gr.sizeof_gr_complex, self.fft_size)
 
         mywindow = window.blackmanharris(self.fft_size)
-        fft = gr.fft_vcc(self.fft_size, True, mywindow)
+        fft = gr.fft_vcc(self.fft_size, True, mywindow, True)
         power = 0
         for tap in mywindow:
             power += tap*tap
@@ -185,8 +189,8 @@ class my_top_block(gr.top_block):
                                     dwell_delay)
 
         # 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)
+        #self.connect(self.u, s2v, fft, c2mag, log, stats)
+        self.connect(self.u, s2v, fft, c2mag, stats)
 
         if options.gain is None:
             # if no gain was specified, use the mid-point in dB
@@ -194,7 +198,7 @@ class my_top_block(gr.top_block):
             options.gain = float(g.start()+g.stop())/2.0
 
         self.set_gain(options.gain)
-	print "gain =", options.gain
+        print "gain =", options.gain
 
     def set_next_freq(self):
         target_freq = self.next_freq
@@ -227,6 +231,20 @@ class my_top_block(gr.top_block):
 
 
 def main_loop(tb):
+    
+    def nearest_freq(freq, channel_bandwidth):
+        freq = round(freq / channel_bandwidth, 0) * channel_bandwidth
+        return freq
+
+    def bin_freq(i_bin, center_freq):
+        hz_per_bin = tb.usrp_rate / tb.fft_size
+        freq = center_freq - (tb.usrp_rate / 2) + (hz_per_bin * i_bin)
+        freq = nearest_freq(freq, tb.channel_bandwidth)
+        return freq
+    
+    bin_start = int(tb.fft_size * ((1 - 0.75) / 2))
+    bin_stop = int(tb.fft_size - bin_start)
+    
     while 1:
 
         # Get the next message sent from the C++ code (blocking call).
@@ -234,7 +252,7 @@ def main_loop(tb):
         m = parse_msg(tb.msgq.delete_head())
 
         # Print center freq so we know that something is happening...
-        print m.center_freq
+        #print "m.center_freq:", m.center_freq
 
         # FIXME do something useful with the data...
 
@@ -244,6 +262,13 @@ def main_loop(tb):
         # m.raw_data is a string that contains the binary floats.
         # You could write this as binary to a file.
 
+        for i_bin in range(bin_start, bin_stop):
+
+            # create signal object to find matching signals
+            freq = int(bin_freq(i_bin, m.center_freq))
+            power = float(m.data[i_bin])
+            
+            print freq, power
 
 if __name__ == '__main__':
     t = ThreadClass()
-- 
cgit v1.2.3