6597170a47c2e0a78ef619e369732c6f892f4b8c
[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         elif options.type == "2tone":
64             self._src1 = gr.sig_source_c(eth_rate,
65                                          gr.GR_SIN_WAVE,
66                                          options.waveform_freq,
67                                          options.amplitude,
68                                          0)
69             if(options.waveform2_freq is None):
70                 w2freq = -options.waveform_freq
71             else:
72                 w2freq = options.waveform2_freq
73             self._src2 = gr.sig_source_c(eth_rate,
74                                          gr.GR_SIN_WAVE,
75                                          w2freq,
76                                          options.amplitude,
77                                          0)
78             self._src = gr.add_cc()
79             self.connect(self._src1,(self._src,0))
80             self.connect(self._src2,(self._src,1))
81         else:
82             sys.stderr.write('Unknown waveform type\n')
83             raise SystemExit, 1
84
85         if options.verbose:
86             print "Network interface:", options.interface
87             print "USRP2 address:", self._u.mac_addr()
88             #print "Using TX d'board %s" % (self._u.tx_name(),)
89             print "Tx gain:", options.gain
90             print "Tx baseband frequency:", n2s(tr.baseband_freq), "Hz"
91             print "Tx DDC frequency:", n2s(tr.dxc_freq), "Hz"
92             print "Tx residual frequency:", n2s(tr.residual_freq), "Hz"
93             print "Tx interpolation rate:", options.interp
94             print "Tx GbE sample rate:", n2s(eth_rate), "samples/sec"
95             if options.type == gr.GR_SIN_WAVE:
96                 print "Baseband waveform type: Sine wave"
97                 print "Baseband waveform frequency:", n2s(options.waveform_freq), "Hz"
98             elif options.type == gr.GR_CONST_WAVE:
99                 print "Baseband waveform type: Constant"
100             elif options.type == gr.GR_GAUSSIAN:
101                 print "Baseband waveform type: Gaussian noise"
102             elif options.type == gr.GR_UNIFORM:
103                 print "Baseband waveform type: Uniform noise"           
104                             
105         # Wire the flowgraph
106         self.connect(self._src, self._u)
107             
108 def get_options():
109     usage="%prog: [options]"
110
111     parser = OptionParser(option_class=eng_option, usage=usage)
112     parser.add_option("-e", "--interface", type="string", default="eth0",
113                       help="use specified Ethernet interface [default=%default]")
114     parser.add_option("-m", "--mac-addr", type="string", default="",
115                       help="use USRP2 at specified MAC address [default=None]")  
116     parser.add_option("-i", "--interp", type="int", default=16,
117                       help="set fgpa decimation rate to DECIM [default=%default]")
118     parser.add_option("-g", "--gain", type="eng_float", default=None,
119                       help="set output gain to GAIN [default=%default]")
120     parser.add_option("-f", "--tx-freq", type="eng_float", default=None,
121                       help="set frequency to FREQ", metavar="FREQ")
122     parser.add_option("-v", "--verbose", action="store_true", default=False,
123                       help="verbose output")
124     parser.add_option("-w", "--waveform-freq", type="eng_float", default=0,
125                       help="set waveform frequency to FREQ [default=%default]")
126     parser.add_option("-x", "--waveform2-freq", type="eng_float", default=None,
127                       help="set waveform frequency to FREQ [default=%default]")
128     parser.add_option("-a", "--amplitude", type="eng_float", default=0.5,
129                       help="set waveform amplitude to AMPLITUDE (0-1.0) [default=%default]", metavar="AMPL")
130     parser.add_option("--offset", type="eng_float", default=0,
131                       help="set waveform offset to OFFSET [default=%default]")
132     parser.add_option("--sine", dest="type", action="store_const", const=gr.GR_SIN_WAVE,
133                       help="generate a complex sinusoid [default]", default=gr.GR_SIN_WAVE)
134     parser.add_option("--const", dest="type", action="store_const", const=gr.GR_CONST_WAVE, 
135                       help="generate a constant output")
136     parser.add_option("--gaussian", dest="type", action="store_const", const=gr.GR_GAUSSIAN,
137                       help="generate Gaussian random output")
138     parser.add_option("--uniform", dest="type", action="store_const", const=gr.GR_UNIFORM,
139                       help="generate Uniform random output")
140     parser.add_option("--2tone", dest="type", action="store_const", const="2tone",
141                       help="generate Two Tone signal for IMD testing")
142                       
143     (options, args) = parser.parse_args ()
144     if len(args) != 0:
145         parser.print_help()
146         raise SystemExit, 1
147     
148     if options.tx_freq is None:
149         parser.print_help()
150         sys.stderr.write('You must specify the frequency with -f FREQ\n');
151         raise SystemExit, 1
152     
153     return options
154
155
156 if __name__ == '__main__':
157     options = get_options()
158     tb = siggen_top_block(options)
159     
160     try:
161         tb.run()
162     except KeyboardInterrupt:
163         pass