Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-examples / python / digital-bert / benchmark_tx.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2008 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, eng_notation, usrp
24 from gnuradio.eng_option import eng_option
25 from optparse import OptionParser
26 from transmit_path import transmit_path
27 import sys
28
29 _dac_rate = 128e6
30
31 n2s = eng_notation.num_to_str
32
33 class tx_bpsk_block(gr.top_block):
34     def __init__(self, options):    
35         gr.top_block.__init__(self, "tx_mpsk")
36
37         self._transmitter = transmit_path(options.sps,
38                                           options.excess_bw,
39                                           options.amplitude)
40
41         if_rate = options.rate*options.sps
42         interp = int(_dac_rate/if_rate)
43
44         print "Modulation:", n2s(options.rate), "bits/sec"
45         print "TX IF rate:", n2s(if_rate), "samples/sec"
46         print "USRP interpolation:", interp
47         print "DAC amplitude:", options.amplitude
48
49         self._setup_usrp(options.which,
50                          interp,
51                          options.tx_subdev_spec,
52                          options.freq)
53         
54         self.connect(self._transmitter, self._usrp)
55
56         
57     def _setup_usrp(self, which, interp, subdev_spec, freq):
58         self._usrp = usrp.sink_c(which=which, interp_rate=interp)
59         if subdev_spec is None:
60             subdev_spec = usrp.pick_tx_subdevice(self._usrp)
61         self._usrp.set_mux(usrp.determine_tx_mux_value(self._usrp, subdev_spec))
62         self._subdev = usrp.selected_subdev(self._usrp, subdev_spec)
63         tr = usrp.tune(self._usrp, self._subdev.which(), self._subdev, freq)
64         if not (tr):
65             print "Failed to tune to center frequency!"
66         else:
67             print "Center frequency:", n2s(freq)
68         gain = float(self._subdev.gain_range()[1]) # Max TX gain
69         self._subdev.set_gain(gain)
70         self._subdev.set_enable(True)
71         print "TX d'board:", self._subdev.side_and_name()
72         
73
74 def get_options():
75     parser = OptionParser(option_class=eng_option)
76     parser.add_option("-w", "--which", type="int", default=0,
77                       help="select which USRP (0, 1, ...) default is %default",
78                       metavar="NUM")
79     parser.add_option("-T", "--tx-subdev-spec", type="subdev", default=None,
80                       help="select USRP Tx side A or B (default=first one with a daughterboard)")
81     parser.add_option("-f", "--freq", type="eng_float", default=None,
82                       help="set frequency to FREQ", metavar="FREQ")
83     parser.add_option("-a", "--amplitude", type="eng_float", default=2000,
84                       help="set Tx amplitude (0-32767) (default=%default)")
85     parser.add_option("-r", "--rate", type="eng_float", default=250e3,
86                       help="Select modulation symbol rate (default=%default)")
87     parser.add_option("", "--sps", type="int", default=2,
88                       help="Select samples per symbol (default=%default)")
89     parser.add_option("", "--excess-bw", type="eng_float", default=0.35,
90                       help="Select RRC excess bandwidth (default=%default)")
91                       
92     (options, args) = parser.parse_args()
93     if len(args) != 0:
94         parser.print_help()
95         sys.exit(1)
96         
97     if options.freq == None:
98         print "Must supply frequency as -f or --freq"
99         sys.exit(1)
100
101     return (options, args)
102
103 if __name__ == "__main__":
104     (options, args) = get_options()
105     
106     tb = tx_bpsk_block(options)
107
108     try:
109         tb.run()
110     except KeyboardInterrupt:
111         pass