3 # Copyright 2004,2005,2007,2008 Free Software Foundation, Inc.
5 # This file is part of GNU Radio
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)
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.
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.
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
31 class my_top_block(gr.top_block):
32 def __init__ (self, nsamples):
33 gr.top_block.__init__(self)
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)
46 return self.u.dac_freq() / self.interp
48 def usb_throughput (self):
49 return self.usb_freq () * 4
51 def set_waveform_type (self, type):
53 valid waveform types are: gr.GR_SIN_WAVE, gr.GR_CONST_WAVE,
54 gr.GR_UNIFORM and gr.GR_GAUSSIAN
56 self._configure_graph (type)
57 self.waveform_type = type
59 def set_waveform_ampl (self, ampl):
60 self.waveform_ampl = ampl
61 self.siggen.set_amplitude (ampl)
62 self.noisegen.set_amplitude (ampl)
64 def set_waveform_freq (self, freq):
65 self.waveform_freq = freq
66 self.siggen.set_frequency (freq)
68 def set_waveform_offset (self, offset):
69 self.waveform_offset = offset
70 self.siggen.set_offset (offset)
72 def set_interpolator (self, interp):
74 self.siggen.set_sampling_freq (self.usb_freq ())
75 self.u.set_interp_rate (interp)
77 def _instantiate_blocks (self):
79 self.u = usrp.sink_c (0, self.interp)
81 self.siggen = gr.sig_source_c (self.usb_freq (),
87 self.noisegen = gr.noise_source_c (gr.GR_UNIFORM,
92 self.head = gr.head(gr.sizeof_gr_complex, int(self.nsamples))
94 # self.file_sink = gr.file_sink (gr.sizeof_gr_complex, "siggen.dat")
96 def _configure_graph (self, type):
99 self.disconnect_all ()
102 self.connect(self.head, self.u)
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
117 raise ValueError, type
121 def set_freq(self, target_freq):
123 Set the center frequency we're interested in.
125 @param target_freq: frequency in Hz
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.
133 r = self.u.tune(self.subdev.which(), self.subdev, target_freq)
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
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]")
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")
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 ()
179 if options.rf_freq is None:
180 sys.stderr.write("usrp_siggen: must specify RF center frequency with -f RF_FREQ\n")
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)
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)
195 m = usrp.determine_tx_mux_value(tb.u, options.tx_subdev_spec)
196 #print "mux = %#04x" % (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(),)
201 if options.gain is None:
202 tb.subdev.set_gain(tb.subdev.gain_range()[1]) # set max Tx gain
204 tb.subdev.set_gain(options.gain) # set max Tx gain
206 if not tb.set_freq(options.rf_freq):
207 sys.stderr.write('Failed to set RF frequency\n')
210 tb.subdev.set_enable(True) # enable transmitter
214 except KeyboardInterrupt:
218 if __name__ == '__main__':