Imported Upstream version 3.2.2
[debian/gnuradio] / gr-utils / src / python / usrp_siggen.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2004,2005,2007,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, gru
24 from gnuradio import usrp
25 from gnuradio.eng_option import eng_option
26 from gnuradio import eng_notation
27 from optparse import OptionParser
28 import sys
29
30
31 class my_top_block(gr.top_block):
32     def __init__ (self, nsamples):
33         gr.top_block.__init__(self)
34         
35         # controllable values
36         self.interp = 64
37         self.waveform_type = gr.GR_SIN_WAVE
38         self.waveform_ampl = 16000
39         self.waveform_freq = 100.12345e3
40         self.waveform_offset = 0
41         self.nsamples = nsamples
42         self._instantiate_blocks ()
43         self.set_waveform_type (self.waveform_type)
44
45     def usb_freq (self):
46         return self.u.dac_freq() / self.interp
47
48     def usb_throughput (self):
49         return self.usb_freq () * 4
50         
51     def set_waveform_type (self, type):
52         '''
53         valid waveform types are: gr.GR_SIN_WAVE, gr.GR_CONST_WAVE,
54         gr.GR_UNIFORM and gr.GR_GAUSSIAN
55         '''
56         self._configure_graph (type)
57         self.waveform_type = type
58
59     def set_waveform_ampl (self, ampl):
60         self.waveform_ampl = ampl
61         self.siggen.set_amplitude (ampl)
62         self.noisegen.set_amplitude (ampl)
63
64     def set_waveform_freq (self, freq):
65         self.waveform_freq = freq
66         self.siggen.set_frequency (freq)
67         
68     def set_waveform_offset (self, offset):
69         self.waveform_offset = offset
70         self.siggen.set_offset (offset)
71
72     def set_interpolator (self, interp):
73         self.interp = interp
74         self.siggen.set_sampling_freq (self.usb_freq ())
75         self.u.set_interp_rate (interp)
76
77     def _instantiate_blocks (self):
78         self.src = None
79         self.u = usrp.sink_c (0, self.interp)
80         
81         self.siggen = gr.sig_source_c (self.usb_freq (),
82                                        gr.GR_SIN_WAVE,
83                                        self.waveform_freq,
84                                        self.waveform_ampl,
85                                        self.waveform_offset)
86
87         self.noisegen = gr.noise_source_c (gr.GR_UNIFORM,
88                                            self.waveform_ampl)
89
90         self.head = None
91         if self.nsamples > 0:
92             self.head = gr.head(gr.sizeof_gr_complex, int(self.nsamples))
93
94         # self.file_sink = gr.file_sink (gr.sizeof_gr_complex, "siggen.dat")
95
96     def _configure_graph (self, type):
97         try:
98             self.lock()
99             self.disconnect_all ()
100
101             if self.head:
102                 self.connect(self.head, self.u)
103                 tail = self.head
104             else:
105                 tail = self.u
106                 
107             if type == gr.GR_SIN_WAVE or type == gr.GR_CONST_WAVE:
108                 self.connect (self.siggen, tail)
109                 # self.connect (self.siggen, self.file_sink)
110                 self.siggen.set_waveform (type)
111                 self.src = self.siggen
112             elif type == gr.GR_UNIFORM or type == gr.GR_GAUSSIAN:
113                 self.connect (self.noisegen, tail)
114                 self.noisegen.set_type (type)
115                 self.src = self.noisegen
116             else:
117                 raise ValueError, type
118         finally:
119             self.unlock()
120
121     def set_freq(self, target_freq):
122         """
123         Set the center frequency we're interested in.
124
125         @param target_freq: frequency in Hz
126         @rypte: bool
127
128         Tuning is a two step process.  First we ask the front-end to
129         tune as close to the desired frequency as it can.  Then we use
130         the result of that operation and our target_frequency to
131         determine the value for the digital up converter.
132         """
133         r = self.u.tune(self.subdev.which(), self.subdev, target_freq)
134         if r:
135             #print "r.baseband_freq =", eng_notation.num_to_str(r.baseband_freq)
136             #print "r.dxc_freq      =", eng_notation.num_to_str(r.dxc_freq)
137             #print "r.residual_freq =", eng_notation.num_to_str(r.residual_freq)
138             #print "r.inverted      =", r.inverted
139             return True
140
141         return False
142
143
144
145 def main ():
146     parser = OptionParser (option_class=eng_option)
147     parser.add_option ("-T", "--tx-subdev-spec", type="subdev", default=(0, 0),
148                        help="select USRP Tx side A or B")
149     parser.add_option ("-f", "--rf-freq", type="eng_float", default=None,
150                        help="set RF center frequency to FREQ")
151     parser.add_option ("-i", "--interp", type="int", default=64,
152                        help="set fgpa interpolation rate to INTERP [default=%default]")
153
154     parser.add_option ("--sine", dest="type", action="store_const", const=gr.GR_SIN_WAVE,
155                        help="generate a complex sinusoid [default]", default=gr.GR_SIN_WAVE)
156     parser.add_option ("--const", dest="type", action="store_const", const=gr.GR_CONST_WAVE, 
157                        help="generate a constant output")
158     parser.add_option ("--gaussian", dest="type", action="store_const", const=gr.GR_GAUSSIAN,
159                        help="generate Gaussian random output")
160     parser.add_option ("--uniform", dest="type", action="store_const", const=gr.GR_UNIFORM,
161                        help="generate Uniform random output")
162
163     parser.add_option ("-w", "--waveform-freq", type="eng_float", default=0,
164                        help="set waveform frequency to FREQ [default=%default]")
165     parser.add_option ("-a", "--amplitude", type="eng_float", default=16e3,
166                        help="set waveform amplitude to AMPLITUDE [default=%default]", metavar="AMPL")
167     parser.add_option ("-g", "--gain", type="eng_float", default=None,
168                        help="set output gain to GAIN [default=%default]")
169     parser.add_option ("-o", "--offset", type="eng_float", default=0,
170                        help="set waveform offset to OFFSET [default=%default]")
171     parser.add_option ("-N", "--nsamples", type="eng_float", default=0,
172                        help="set number of samples to transmit [default=+inf]")
173     (options, args) = parser.parse_args ()
174
175     if len(args) != 0:
176         parser.print_help()
177         raise SystemExit
178
179     if options.rf_freq is None:
180         sys.stderr.write("usrp_siggen: must specify RF center frequency with -f RF_FREQ\n")
181         parser.print_help()
182         raise SystemExit
183
184     tb = my_top_block(options.nsamples)
185     tb.set_interpolator (options.interp)
186     tb.set_waveform_type (options.type)
187     tb.set_waveform_freq (options.waveform_freq)
188     tb.set_waveform_ampl (options.amplitude)
189     tb.set_waveform_offset (options.offset)
190
191     # determine the daughterboard subdevice we're using
192     if options.tx_subdev_spec is None:
193         options.tx_subdev_spec = usrp.pick_tx_subdevice(tb.u)
194
195     m = usrp.determine_tx_mux_value(tb.u, options.tx_subdev_spec)
196     #print "mux = %#04x" % (m,)
197     tb.u.set_mux(m)
198     tb.subdev = usrp.selected_subdev(tb.u, options.tx_subdev_spec)
199     print "Using TX d'board %s" % (tb.subdev.side_and_name(),)
200     
201     if options.gain is None:
202         tb.subdev.set_gain(tb.subdev.gain_range()[1])    # set max Tx gain
203     else:
204         tb.subdev.set_gain(options.gain)    # set max Tx gain
205
206     if not tb.set_freq(options.rf_freq):
207         sys.stderr.write('Failed to set RF frequency\n')
208         raise SystemExit
209     
210     tb.subdev.set_enable(True)                       # enable transmitter
211
212     try:
213         tb.run()
214     except KeyboardInterrupt:
215         pass
216
217
218 if __name__ == '__main__':
219     main ()