Fixed base class name.
[debian/gnuradio] / gnuradio-examples / python / hier / dect / usrp_source.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2007 Free Software Foundation, Inc.
4
5 # This file is part of GNU Radio
6
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)
10 # any later version.
11
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.
16
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.
21
22
23 from gnuradio import gr, usrp, eng_notation
24 n2s = eng_notation.num_to_str
25
26 # Hierarchical block implementing a USRP source for complex floats,
27 # with convenience functions for gain, tune, decimation, etc.
28 #
29 class usrp_source_c(gr.hier_block2):
30     """
31     Create a USRP source object supplying complex floats.
32     """
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
40
41         self._verbose = verbose
42         self._u = usrp.source_c(which)
43         if self._verbose:
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)
47         self.set_gain(gain)
48         self.set_calibration(calibration)
49         self.tune(freq)
50         self.connect(self._u, self)
51
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))
57         if self._verbose:
58             print 'RX using', self._subdev.name(), 'daughterboard'
59             
60     def pick_subdevice(self):
61         """
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.
65         Otherwise, select A.
66         """
67         if self._u.db[0][0].dbid() >= 0:       # dbid is < 0 if there's no d'board or a problem
68             return (0, 0)
69         if self._u.db[1][0].dbid() >= 0:
70             return (1, 0)
71         return (0, 0)
72
73     def set_if_rate(self, if_rate):
74         # If no IF rate specified, set to maximum decimation
75         if if_rate is None:
76             self._decim = 256
77         else:
78             self._decim = int(self._u.adc_rate()/if_rate)
79
80         self._u.set_decim_rate(self._decim)
81         self._if_rate = self._u.adc_rate()/self._decim
82
83         if self._verbose:
84             print "USRP decimation rate is", self._decim
85             print "USRP IF rate is", n2s(self._if_rate), "sps"
86             
87     def set_gain(self, gain):
88         # If no gain specified, set to midrange
89         if gain is None:
90             g = self._subdev.gain_range()
91             gain = (g[0]+g[1])/2.0
92         self._gain = gain
93         self._subdev.set_gain(self._gain)
94
95     def set_calibration(self, calibration):
96         self._cal = calibration
97         if self._verbose:
98             print "Using frequency calibration offset of", n2s(calibration), "Hz"
99             
100     def tune(self, freq):
101         """
102         Set the center frequency we're interested in.
103
104         @param target_freq: frequency in Hz
105         @type: bool
106
107         Tuning is a two step process.  First we ask the front-end to
108         tune as close to the desired frequency as it can.  Then we use
109         the result of that operation and our target_frequency to
110         determine the value for the digital down converter.
111         """
112         self._tune_result = usrp.tune(self._u, 0, self._subdev, freq+self._cal)
113         if self._tune_result:
114             if self._verbose:
115                 print "Baseband frequency is", n2s(self._tune_result.baseband_freq), "Hz"
116                 print "DXC frequency is", n2s(self._tune_result.dxc_freq), "Hz"
117                 print "Center frequency is", n2s(freq), "Hz"
118                 print "Residual frequency is", n2s(self._tune_result.residual_freq), "Hz"
119             return True
120         
121         return False
122
123 if __name__ == '__main__':
124     src = usrp_source_c(verbose=True)