Updated usrp2_rx_cfile.py and usrp2_siggen.py to use updated gr-usrp2
[debian/gnuradio] / gr-utils / src / python / usrp2_siggen.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
24 from gnuradio import usrp2
25 from gnuradio.eng_option import eng_option
26 from optparse import OptionParser
27 import sys
28
29 n2s = eng_notation.num_to_str
30
31 class siggen_top_block(gr.top_block):
32     def __init__(self, options):
33         gr.top_block.__init__(self)
34
35         # Create a USRP2 sink with the requested interpolation rate
36         self._u = usrp2.sink_32fc(options.interface, options.mac_addr)
37         self._u.set_interp(options.interp)
38
39         # Set the Tx daughterboard gain as requested
40         if options.gain is None:
41             g = self._u.gain_range()
42             options.gain = float(g[0]+g[1])/2
43         self._u.set_gain(options.gain)
44
45         # Tune the USRP2 FPGA and daughterboard to the requested center frequency
46         tr = self._u.set_center_freq(options.tx_freq)
47         if tr == None:
48             sys.stderr.write('Failed to set center frequency\n')
49             raise SystemExit, 1
50
51         eth_rate = self._u.dac_rate()/self._u.interp()
52         
53         # Create a source for the requested waveform type
54         if options.type == gr.GR_SIN_WAVE or options.type == gr.GR_CONST_WAVE:
55             self._src = gr.sig_source_c(eth_rate,              # Sample rate
56                                         options.type,          # Waveform type
57                                         options.waveform_freq, # Waveform frequency
58                                         options.amplitude,     # Waveform amplitude
59                                         options.offset)        # Waveform offset
60             
61         elif options.type == gr.GR_GAUSSIAN or options.type == gr.GR_UNIFORM:
62             self._src = gr.noise_source_c(options.type, options.amplitude)
63         else:
64             sys.stderr.write('Unknown waveform type\n')
65             raise SystemExit, 1
66
67         if options.verbose:
68             print "Network interface:", options.interface
69             print "USRP2 address:", self._u.mac_addr()
70             #print "Using TX d'board %s" % (self._u.tx_name(),)
71             print "Tx gain:", options.gain
72             print "Tx baseband frequency:", n2s(tr.baseband_freq), "Hz"
73             print "Tx DDC frequency:", n2s(tr.dxc_freq), "Hz"
74             print "Tx residual frequency:", n2s(tr.residual_freq), "Hz"
75             print "Tx interpolation rate:", options.interp
76             print "Tx GbE sample rate:", n2s(eth_rate), "samples/sec"
77             if options.type == gr.GR_SIN_WAVE:
78                 print "Baseband waveform type: Sine wave"
79                 print "Baseband waveform frequency:", n2s(options.waveform_freq), "Hz"
80             elif options.type == gr.GR_CONST_WAVE:
81                 print "Baseband waveform type: Constant"
82             elif options.type == gr.GR_GAUSSIAN:
83                 print "Baseband waveform type: Gaussian noise"
84             elif options.type == gr.GR_UNIFORM:
85                 print "Baseband waveform type: Uniform noise"           
86                             
87         # Wire the flowgraph
88         self.connect(self._src, self._u)
89             
90 def get_options():
91     usage="%prog: [options]"
92
93     parser = OptionParser(option_class=eng_option, usage=usage)
94     parser.add_option("-e", "--interface", type="string", default="eth0",
95                       help="use specified Ethernet interface [default=%default]")
96     parser.add_option("-m", "--mac-addr", type="string", default="",
97                       help="use USRP2 at specified MAC address [default=None]")  
98     parser.add_option("-i", "--interp", type="int", default=16,
99                       help="set fgpa decimation rate to DECIM [default=%default]")
100     parser.add_option("-g", "--gain", type="eng_float", default=None,
101                       help="set output gain to GAIN [default=%default]")
102     parser.add_option("-f", "--tx-freq", type="eng_float", default=None,
103                       help="set frequency to FREQ", metavar="FREQ")
104     parser.add_option("-v", "--verbose", action="store_true", default=False,
105                       help="verbose output")
106     parser.add_option("-w", "--waveform-freq", type="eng_float", default=0,
107                       help="set waveform frequency to FREQ [default=%default]")
108     parser.add_option("-a", "--amplitude", type="eng_float", default=0.5,
109                       help="set waveform amplitude to AMPLITUDE (0-1.0) [default=%default]", metavar="AMPL")
110     parser.add_option("--offset", type="eng_float", default=0,
111                       help="set waveform offset to OFFSET [default=%default]")
112     parser.add_option("--sine", dest="type", action="store_const", const=gr.GR_SIN_WAVE,
113                       help="generate a complex sinusoid [default]", default=gr.GR_SIN_WAVE)
114     parser.add_option("--const", dest="type", action="store_const", const=gr.GR_CONST_WAVE, 
115                       help="generate a constant output")
116     parser.add_option("--gaussian", dest="type", action="store_const", const=gr.GR_GAUSSIAN,
117                       help="generate Gaussian random output")
118     parser.add_option("--uniform", dest="type", action="store_const", const=gr.GR_UNIFORM,
119                       help="generate Uniform random output")
120
121     (options, args) = parser.parse_args ()
122     if len(args) != 0:
123         parser.print_help()
124         raise SystemExit, 1
125     
126     if options.tx_freq is None:
127         parser.print_help()
128         sys.stderr.write('You must specify the frequency with -f FREQ\n');
129         raise SystemExit, 1
130     
131     return options
132
133
134 if __name__ == '__main__':
135     options = get_options()
136     tb = siggen_top_block(options)
137     
138     try:
139         tb.run()
140     except KeyboardInterrupt:
141         pass