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 sink for complex floats,
27 # with convenience functions for tuning, interpolation, etc.
29 class usrp_sink_c(gr.hier_block2):
31 Create a USRP sink object accepting complex floats.
33 def __init__(self, which=0, subdev_spec=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_sink_c", # Block typename
38 gr.io_signature(1,1,gr.sizeof_gr_complex), # Input signature
39 gr.io_signature(0,0,0)) # Output signature
41 self._verbose = verbose
42 self._u = usrp.sink_c(which)
44 print 'DAC sample rate is', n2s(self._u.dac_rate()), "sps"
45 self.set_subdev(subdev_spec)
46 self.set_if_rate(if_rate)
47 self.set_calibration(calibration)
49 self.connect(self, self._u)
51 def set_subdev(self, subdev_spec):
52 if subdev_spec is None:
53 subdev_spec = self.pick_subdevice()
54 self._subdev = usrp.selected_subdev(self._u, subdev_spec)
55 self._u.set_mux(usrp.determine_tx_mux_value(self._u, subdev_spec))
57 print 'TX using', self._subdev.name(), 'daughterboard'
59 def pick_subdevice(self):
61 The user didn't specify a subdevice.
62 If there's a daughterboard on A, select A.
63 If there's a daughterboard on B, select B.
66 if self._u.db[0][0].dbid() >= 0: # dbid is < 0 if there's no d'board or a problem
68 if self._u.db[1][0].dbid() >= 0:
72 def set_if_rate(self, if_rate):
73 # If no IF rate specified, set to maximum interpolation
77 self._interp = 4*int(self._u.dac_rate()/(4.0*if_rate)+0.5)
80 self._if_rate = self._u.dac_rate()/self._interp
81 self._u.set_interp_rate(self._interp)
84 print "USRP interpolation rate is", self._interp
85 print "USRP IF rate is", n2s(self._if_rate), "sps"
87 def set_calibration(self, calibration):
88 self._cal = calibration
90 print "Using frequency calibration offset of", n2s(calibration), "Hz"
94 Set the center frequency we're interested in.
96 @param target_freq: frequency in Hz
99 Tuning is a two step process. First we ask the front-end to
100 tune as close to the desired frequency as it can. Then we use
101 the result of that operation and our target_frequency to
102 determine the value for the digital down converter.
104 self._tune_result = self._u.tune(self._subdev._which, self._subdev, freq+self._cal)
105 if self._tune_result:
107 print "Baseband frequency is", n2s(self._tune_result.baseband_freq), "Hz"
108 print "DXC frequency is", n2s(self._tune_result.dxc_freq), "Hz"
109 print "Center frequency is", n2s(freq), "Hz"
110 print "Residual frequency is", n2s(self._tune_result.residual_freq), "Hz"
115 if __name__ == '__main__':
116 sink = usrp_sink_c(verbose=True)