#!/usr/bin/env python # # Copyright 2005,2007,2008,2009 Free Software Foundation, Inc. # # This file is part of GNU Radio # # GNU Radio 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. # # GNU Radio 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 GNU Radio; see the file COPYING. If not, write to # the Free Software Foundation, Inc., 51 Franklin Street, # Boston, MA 02110-1301, USA. # """ Note: I consider usrp_spectrum_sense.py as the best given working example in the gnuradio project. I think also that the understanding of this program is very important to every gnuradio user (because it includes a practical FFT implementation + FSM control in its cpp code) Introduction: ------------- 1) This program can be used as a basic code for implementing wideband spectrum analyzer. 2) As we know, the USRP cannot examine more than 8 MHz of RF spectrum due to USB bus limitations. 3) So, to scan across a wide RF spectrum band (bigger than 8 MHz) we have to tune USRP RF front end in suitable steps so that we can examine a lot of spectrum, although not all at the same instant. 4) The usrp_spectrum_sense shows the way how it can be done.It steps across the spectrum and make the RF measurements. This application can sense a large bandwidth, but not in real time, and it can do the frequency sweep over the required frequency range, Theory: ------- 1) To use N points complex FFT X(W) analysis, we have to get N time samples x(t) which are sampled at Fs. 2) These N time samples must be time windowed using a known window function to reduce spectral leakage. 3) Performing N points complex FFT analysis. 4) The output of the complex FFT will represent the frequency spectrum contents as follows: a) The first value of the FFT output (bin 0 == X[0]) is the passband center frequency. b) The first half of the FFT (X[1] to X[N/2-1] contains the positive baseband frequencies,which corresponds to the passband spectrum from the center frequency out to the maximum passband frequency (from center frequency to +Fs/2). c) The second half of the FFT (X[N/2] to X[N-1]) contains the negative baseband frequencies,which correspond to the lowest passband frequency up to the passband center frequency (from -Fs/2 to center frequency). Example ------- Let us assume that we have 1024 (I and Q) samples gathered using a tuner centered at 20MHz. And let us assume that the sampling frequency was 8MHz. Doing 1024 points complex FFT means: 1) FFT Frequency resolution is : 8MHz / 1024 = 7812.5 KHz 2) The output of the FFT X[0] represents the spectrum at 20MHz. 3) The output of the FFT X[1] to X[511] represents the frequencies from 20.0078125 MHz to 23.9921875 MHz (about 4MHz above center frequency). 4) The output of the FFT X[512] to X[1023] represents the frequencies from 16.0078125 MHz to 19.9921875 MHz (about 4MHz bellow center frequency). RF Frequency Sweeping --------------------- 1) Let us suppose that we want to scan RF spectrum band from 10MHz to 52 MHz. 2) Let us remember that USRP can analyze 8MHz of frequency at a time. 3) So theoretically we have to step our RF center frequency as follows: First step is 14MHz (it will cover frequency band from 10MHz to 18MHz), Second step is 22MHz (it will cover frequency band from 18MHz to 26MHz), Third step is 30MHz (it will cover frequency band from 26MHz to 34MHz), Fourth step is 38MHz (it will cover frequency band from 34MHz to 42MHz), Fifth step is 46MHz (it will cover frequency band from 42MHz to 50MHz), and finally the Sixth step is 54MHz (it will cover frequency band from 50MHz to 58MHz). Remember that we want the frequencies up to 52MHz only, so we have to discard some FFT points from the Sixth analysis. 4) Paralytically we have to use FFT overlapping to reduce the non linearity response of the Digital Down Converter (the DDC frequency response is not Flat from -Fs/2 to + Fs/2) and to fill the frequency holes that will be present at the FFT analysis edges (10MHz, 18MHz, 26MHz, 34MHz, 42MHz, 50 MHz). So if we choose to use an overlap of 25%, this means that our step size will be 6MHz (8MHz*(1-.25)), thus practically we have to step our RF center frequency as follows: First step is 13MHz (it will cover frequency band from 9MHz to 17MHz), Second step is 19MHz (it will cover frequency band from 15MHz to 23MHz), Third step is 25MHz (it will cover frequency band from 21MHz to 29MHz), Fourth step is 31MHz (it will cover frequency band from 27MHz to 35MHz), Fifth step is 37MHz (it will cover frequency band from 33MHz to 41MHz), Sixth step is 43MHz (it will cover frequency band from 39MHz to 47MHz), and finally the Seventh step is 49MHz (it will cover frequency band from 45MHz to 53MHz), Changing RF center Frequency ---------------------------- 1) To change USRP RF center frequency we have to send a tunning command to the USRP every time we complete the analysis of the current frequency chunk. 2) Before gnuradio revision [10165], all USRP RF daughterboards tunning were done using Python functions and classes. After that revision, tunning the USRP daughterboards from withen C++ code is possible. 3) In usrp_spectrum_sense.py, the DSP C++ written code is allowed to transparently invoke Python code USRP tune function. This tunning control is done in gr_bin_statistics_f sink function. Tunning Delay Problem: --------------------- When we command the usrp RF daughterboard to change its center frequency, we have to wait until (right) ADC samples arrive to our FFT engine and we have to insure that it belongs to the wanted center frequency. This represents a problem since there are many delays along the digitization path (RF synthesizer settling time, and pipeline propagation delay [FPGA FIFO filling time, USB transferring time...etc]). To overcome this problem we have to use enough tune delay time in order to be sure that the samples entering our FFT block are belong to the requested center frequency. This is done simply by dropping the incoming received samples over a specified tunning delay time. usrp_spectrum_sense Implementation ---------------------------------- 1) The engine of the usrp_spectrum_sense depends mainly on bin_statistics sink function. 2) bin_statistics function combines statistics gathering with a state machine for controlling the USRP RF tuning (frequency sweeping). It determines max values (keeps track of the maximum power in each FFT bin) of vectors (with length vlen) over a time period determined by dwell_delay (after converting it to a number of FFT vectors). This operation is performed after discarding tune_delay samples. 3) After processing N = dwell_delay samples, bin_statistics composes a message and inserts it in a message queue. 4) Each message from bin_statistics consists of a vector of max values, prefixed by the center frequency corresponding to the associated samples, i.e., it is the center frequency value of the delivered input samples to bin_statistics. Choosing Tune and Dwell delay times ---------------------------------- 1) We have to play with the --tune-delay and --dwell-delay command line options to determine appropriate timming values. The most important one is the tune delay time. 2) The choose of tune-delay should include time for the front end PLL to settle, plus time for the new samples to propagate through the pipeline. The default value is 1ms, which is probably in the ballpark on the RFX** boards. The TV RX board is much slower. The tuner data sheets says it could take 100ms to settle. 3) The tune delay timing parameter passed to bin_statistics is calculated in FFT frames which depends on USRP rate and FFT length as in : tune_delay_passed_to_bin_statistics = int(round(required_tune_delay_in_sec*usrp_rate/fft_size)) if this calculated value is less than "1", then we should make it at least "1" FFT frame. For example: If the : required_tune_delay_in_sec = 10e-3 and usrp_rate = 8000000 (decimation =8) and FFT size is 1024 Then : tune_delay_passed_to_bin_stats = 78 (FFT Frames) This means we have to skip 78 incoming vectors (FFT frames) before we actually use the acquired samples in our spectrum statistics. 4) Beside tunning time depends on the hardware (RF synthesizer speed),one should remember that the time needed to collect 1024 samples with decimation rate=8 (minimum USRP decimation) is 128 usec, while the time needed to collect 1024 samples with decimation rate=256 (maximum USRP decimation) is 4.096 msec. This means that the tune delay in the case of decimation rate =256 should be larger than that used for decimation = 8. 4) A working tune delay value (which gives accurate results) can be known by experiments (for given decimation rate and FFT length). Interrupting Output Spectrum ----------------------------- The actual mapping from the levels at the daughterboard antenna input port to the output analysis values depends on a lot of factors including the used daughterboard RF gain and decimation specific gain in the digital down converter. You'll need to calibrate the system if you need something that maps to dBm.Currently, the output of usrp_spectrum_sense is the magnitude squared of the FFT output. That is, for each FFT bin[i], the output is Y[i] = re[X[i]]*re[X[i]] + im[X[i]]*im[X[i]]. If you want power, take the square root of the output. """ from gnuradio import gr, eng_notation from gnuradio import blocks from gnuradio import audio from gnuradio import usrp from gnuradio import filter from gnuradio import fft from gnuradio import uhd from gnuradio.eng_option import eng_option from optparse import OptionParser import sys import math import struct import time import os class tune(gr.feval_dd): """ This class allows C++ code (bin_statistics_f.cc) to callback into python to change USRP RF center Frequency. """ 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 to parse the incomming messages sent by bin_statistics. 1) It extracts the center frequncy and the incomming data vector length 2) Convert received data string to a floating point format. 3) Store the output in the "data" array """ 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) class my_top_block(gr.top_block): def __init__(self): gr.top_block.__init__(self) usage = "usage: %prog [options] min_freq max_freq" parser = OptionParser(option_class=eng_option, usage=usage) parser.add_option("-R", "--rx-subdev-spec", type="subdev", default=(0,0), help="select USRP Rx side A or B (default=B)") parser.add_option("-g", "--gain", type="eng_float", default=None, help="set gain in dB (default is midpoint)") parser.add_option("", "--tune-delay", type="eng_float", default=1e-3, metavar="SECS", 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]") parser.add_option("-F", "--fft-size", type="int", default=256, help="specify number of FFT bins [default=%default]") parser.add_option("-d", "--decim", type="intx", default=16, help="set decimation to DECIM [default=%default]") parser.add_option("", "--real-time", action="store_true", default=False, help="Attempt to enable real-time scheduling") parser.add_option("-B", "--fusb-block-size", type="int", default=0, help="specify fast usb block size [default=%default]") parser.add_option("-N", "--fusb-nblocks", type="int", default=0, help="specify number of fast usb blocks [default=%default]") (options, args) = parser.parse_args() if len(args) != 2: parser.print_help() sys.exit(1) self.min_freq = eng_notation.str_to_num(args[0]) self.max_freq = eng_notation.str_to_num(args[1]) if self.min_freq > self.max_freq: self.min_freq, self.max_freq = self.max_freq, self.min_freq # swap them self.fft_size = options.fft_size 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.source_c(fusb_block_size=options.fusb_block_size, fusb_nblocks=options.fusb_nblocks) 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) mywindow = window.blackmanharris(self.fft_size) fft = gr.fft_vcc(self.fft_size, True, mywindow) #This loop is calculating the gain of the applied window power = 0 for tap in mywindow: power += tap*tap c2mag = gr.complex_to_mag_squared(self.fft_size) # use c2mag = gr.complex_to_mag(self.fft_size) if you want the power # 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.06 *1e9 self.min_center_freq = self.min_freq nsteps = math.ceil((self.max_freq - self.min_freq) / self.freq_step) self.max_center_freq = self.max_freq 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) # 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) 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 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 "" print "Failed to set frequency to", target_freq return target_freq def set_freq(self, target_freq): """ Set the center frequency we're interested in. @param target_freq: frequency in Hz @rypte: bool Tuning is a two step process. First we ask the front-end to tune as close to the desired frequency as it can. Then we use the result of that operation and our target_frequency to determine the value for the digital down converter. """ return self.u.tune(0, self.subdev, target_freq) def set_gain(self, gain): self.subdev.set_gain(gain) def main_loop(tb): global free_band,status j=0 while 1: j= j + 1 # Get the next message sent from the C++ code (bin_statistics) # It contains the center frequency and the mag squared of the fft m = parse_msg(tb.msgq.delete_head()) # This is a blocking call. # Print center freq so we know that something is happening... signalPower = 0 noise_floor = 20000000 for bin in m.data: signalPower += bin print signalPower if signalPower>noise_floor and j > 1: print"PU Present" print m.center_freq free_band = m.center_freq signalPower = 0 print"Checking next frequency band " status =1 break if signalPower 1: print"PU absent" print m.center_freq free_band = m.center_freq signalPower = 0 status =0 break # 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.) # m.raw_data is a string that contains the binary floats. # You could write this as binary to a file. if __name__ == '__main__': tb = my_top_block() try: global free_band tb.start() # start executing flow graph in another thread, and return the control to the python program main_loop(tb) m1 = parse_msg(tb.msgq.delete_head()) # This is a blocking call. tb.stop() tb._tune_callback.tb=None tb=None time.sleep(0.2) print free_band if status==0: os.system('sudo ./rts.py -f 5G -b %d'% (int(free_band))) os.system('sudo ./benchmark_rx.py -f 5G -b %d -v'%(int(free_band))) if status==1: print "Going to next freq band" if free_band==int(5.18*1e9): os.system('sudo ./usrp_spectrum_sense_tx.py 5.24G 5.24G') if free_band==int(5.24*1e9): os.system('sudo ./usrp_spectrum_sense_tx.py 5.18G 5.18G') except KeyboardInterrupt: pass