3 # Copyright 2007 Free Software Foundation, Inc.
5 # This file is part of GNU Radio
7 # GNU Radio is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3, or (at your option)
12 # GNU Radio is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with GNU Radio; see the file COPYING. If not, write to
19 # the Free Software Foundation, Inc., 51 Franklin Street,
20 # Boston, MA 02110-1301, USA.
23 from gnuradio import gr, usrp, eng_notation
24 n2s = eng_notation.num_to_str
26 # Hierarchical block implementing a USRP source for complex floats,
27 # with convenience functions for gain, tune, decimation, etc.
29 class usrp_source_c(gr.hier_block2):
31 Create a USRP source object supplying complex floats.
33 def __init__(self, which=0, subdev_spec=None, gain=None, if_rate=None,
34 freq=0.0, calibration=0.0, verbose=False):
35 # Call hierarchical block constructor
36 gr.hier_block2.__init__(self,
37 "usrp_source_c", # Block typename
38 gr.io_signature(0,0,0), # Input signature
39 gr.io_signature(1,1,gr.sizeof_gr_complex)) # Output signature
41 self._verbose = verbose
42 self._u = usrp.source_c(which)
44 print 'ADC sample rate is', n2s(self._u.adc_rate()), "sps"
45 self.set_subdev(subdev_spec)
46 self.set_if_rate(if_rate)
48 self.set_calibration(calibration)
50 self.connect(self._u, self)
52 def set_subdev(self, subdev_spec):
53 if subdev_spec is None:
54 subdev_spec = self.pick_subdevice()
55 self._subdev = usrp.selected_subdev(self._u, subdev_spec)
56 self._u.set_mux(usrp.determine_rx_mux_value(self._u, subdev_spec))
58 print 'RX using', self._subdev.name(), 'daughterboard'
60 def pick_subdevice(self):
62 The user didn't specify a subdevice.
63 If there's a daughterboard on A, select A.
64 If there's a daughterboard on B, select B.
67 if self._u.db[0][0].dbid() >= 0: # dbid is < 0 if there's no d'board or a problem
69 if self._u.db[1][0].dbid() >= 0:
73 def set_if_rate(self, if_rate):
74 # If no IF rate specified, set to maximum decimation
78 self._decim = int(self._u.adc_rate()/if_rate)
80 self._u.set_decim_rate(self._decim)
81 self._if_rate = self._u.adc_rate()/self._decim
84 print "USRP decimation rate is", self._decim
85 print "USRP IF rate is", n2s(self._if_rate), "sps"
87 def set_gain(self, gain):
88 # If no gain specified, set to midrange
90 g = self._subdev.gain_range()
91 gain = (g[0]+g[1])/2.0
93 self._subdev.set_gain(self._gain)
95 print "USRP gain set to", self._gain
97 def set_calibration(self, calibration):
98 self._cal = calibration
100 print "Using frequency calibration offset of", n2s(calibration), "Hz"
102 def tune(self, freq):
104 Set the center frequency we're interested in.
106 @param target_freq: frequency in Hz
109 Tuning is a two step process. First we ask the front-end to
110 tune as close to the desired frequency as it can. Then we use
111 the result of that operation and our target_frequency to
112 determine the value for the digital down converter.
114 self._tune_result = usrp.tune(self._u, 0, self._subdev, freq+self._cal)
115 if self._tune_result:
117 print "Baseband frequency is", n2s(self._tune_result.baseband_freq), "Hz"
118 print "DXC frequency is", n2s(self._tune_result.dxc_freq), "Hz"
119 print "Center frequency is", n2s(freq), "Hz"
120 print "Residual frequency is", n2s(self._tune_result.residual_freq), "Hz"
125 if __name__ == '__main__':
126 src = usrp_source_c(verbose=True)