3 from gnuradio import gr, gru
4 from gnuradio import usrp
5 from gnuradio.eng_option import eng_option
6 from gnuradio import eng_notation
7 from optparse import OptionParser
10 class my_graph(gr.top_block):
11 def __init__ (self, type, ampl, wfreq, offset, subdev_spec, interp, rf_freq):
12 gr.top_block.__init__(self, "usrp_siggen")
16 self.waveform_type = type
17 self.waveform_ampl = ampl
18 self.waveform_freq = wfreq
19 self.waveform_offset = offset
21 self.u = usrp.sink_c (0, self.interp)
23 # determine the daughterboard subdevice we're using
24 if subdev_spec is None:
25 ubdev_spec = usrp.pick_tx_subdevice(self.u)
26 m = usrp.determine_tx_mux_value(self.u, subdev_spec)
28 self.subdev = usrp.selected_subdev(self.u, subdev_spec)
29 self.subdev.set_gain(self.subdev.gain_range()[1]) # set max Tx gain
30 self.subdev.set_enable(True) # enable transmitter
31 print "Using TX d'board %s" % (self.subdev.side_and_name(),)
33 if not self.set_freq(rf_freq):
34 sys.stderr.write('Failed to set RF frequency\n')
37 if type == gr.GR_SIN_WAVE or type == gr.GR_CONST_WAVE:
38 self.src = gr.sig_source_c (self.usb_freq (),
44 elif type == gr.GR_UNIFORM or type == gr.GR_GAUSSIAN:
45 self.src = gr.noise_source_c (gr.GR_UNIFORM,
49 raise ValueError, type
51 self.connect (self.src, self.u)
55 return self.u.dac_freq() / self.interp
57 def usb_throughput (self):
58 return self.usb_freq () * 4
60 def set_freq(self, target_freq):
62 Set the center frequency we're interested in.
64 @param target_freq: frequency in Hz
67 Tuning is a two step process. First we ask the front-end to
68 tune as close to the desired frequency as it can. Then we use
69 the result of that operation and our target_frequency to
70 determine the value for the digital up converter.
72 r = self.u.tune(self.subdev._which, self.subdev, target_freq)
74 #print "r.baseband_freq =", eng_notation.num_to_str(r.baseband_freq)
75 #print "r.dxc_freq =", eng_notation.num_to_str(r.dxc_freq)
76 #print "r.residual_freq =", eng_notation.num_to_str(r.residual_freq)
77 #print "r.inverted =", r.inverted
85 parser = OptionParser (option_class=eng_option)
86 parser.add_option ("-T", "--tx-subdev-spec", type="subdev", default=(0, 0),
87 help="select USRP Tx side A or B")
88 parser.add_option ("-f", "--rf-freq", type="eng_float", default=None,
89 help="set RF center frequency to FREQ")
90 parser.add_option ("-i", "--interp", type="int", default=64,
91 help="set fgpa interpolation rate to INTERP [default=%default]")
93 parser.add_option ("--sine", dest="type", action="store_const", const=gr.GR_SIN_WAVE,
94 help="generate a complex sinusoid [default]", default=gr.GR_SIN_WAVE)
95 parser.add_option ("--const", dest="type", action="store_const", const=gr.GR_CONST_WAVE,
96 help="generate a constant output")
97 parser.add_option ("--gaussian", dest="type", action="store_const", const=gr.GR_GAUSSIAN,
98 help="generate Gaussian random output")
99 parser.add_option ("--uniform", dest="type", action="store_const", const=gr.GR_UNIFORM,
100 help="generate Uniform random output")
102 parser.add_option ("-w", "--waveform-freq", type="eng_float", default=100e3,
103 help="set waveform frequency to FREQ [default=%default]")
104 parser.add_option ("-a", "--amplitude", type="eng_float", default=16e3,
105 help="set waveform amplitude to AMPLITUDE [default=%default]", metavar="AMPL")
106 parser.add_option ("-o", "--offset", type="eng_float", default=0,
107 help="set waveform offset to OFFSET [default=%default]")
108 (options, args) = parser.parse_args ()
114 if options.rf_freq is None:
115 sys.stderr.write("usrp_siggen: must specify RF center frequency with -f RF_FREQ\n")
119 top_block = my_graph(options.type, options.amplitude, options.waveform_freq, options.offset,
120 options.tx_subdev_spec, options.interp, options.rf_freq)
125 except KeyboardInterrupt:
129 if __name__ == '__main__':