Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-examples / python / usrp / fm_tx4.py
1 #!/usr/bin/env python
2
3 """
4 Transmit N simultaneous narrow band FM signals.
5
6 They will be centered at the frequency specified on the command line,
7 and will spaced at 25kHz steps from there.
8
9 The program opens N files with names audio-N.dat where N is in [0,7].
10 These files should contain floating point audio samples in the range [-1,1]
11 sampled at 32kS/sec.  You can create files like this using
12 audio_to_file.py
13 """
14
15 from gnuradio import gr, eng_notation
16 from gnuradio import usrp
17 from gnuradio import audio
18 from gnuradio import blks
19 from gnuradio.eng_option import eng_option
20 from optparse import OptionParser
21 import usrp_dbid
22 import math
23 import sys
24
25 from gnuradio.wxgui import stdgui, fftsink
26 from gnuradio import tx_debug_gui
27 import wx
28
29
30 ########################################################
31 # instantiate one transmit chain for each call
32
33 class pipeline(gr.hier_block):
34     def __init__(self, fg, filename, lo_freq, audio_rate, if_rate):
35
36         src = gr.file_source (gr.sizeof_float, filename, True)
37         fmtx = blks.nbfm_tx (fg, audio_rate, if_rate,
38                              max_dev=5e3, tau=75e-6)
39         
40         # Local oscillator
41         lo = gr.sig_source_c (if_rate,        # sample rate
42                               gr.GR_SIN_WAVE, # waveform type
43                               lo_freq,        #frequency
44                               1.0,            # amplitude
45                               0)              # DC Offset
46         mixer = gr.multiply_cc ()
47     
48         fg.connect (src, fmtx, (mixer, 0))
49         fg.connect (lo, (mixer, 1))
50
51         gr.hier_block.__init__(self, fg, src, mixer)
52
53
54
55 class fm_tx_graph (stdgui.gui_flow_graph):
56     def __init__(self, frame, panel, vbox, argv):
57         MAX_CHANNELS = 7
58         stdgui.gui_flow_graph.__init__ (self, frame, panel, vbox, argv)
59
60         parser = OptionParser (option_class=eng_option)
61         parser.add_option("-T", "--tx-subdev-spec", type="subdev", default=None,
62                           help="select USRP Tx side A or B")
63         parser.add_option("-f", "--freq", type="eng_float", default=None,
64                            help="set Tx frequency to FREQ [required]", metavar="FREQ")
65         parser.add_option("-n", "--nchannels", type="int", default=4,
66                            help="number of Tx channels [1,4]")
67         parser.add_option("","--debug", action="store_true", default=False,
68                           help="Launch Tx debugger")
69         (options, args) = parser.parse_args ()
70
71         if len(args) != 0:
72             parser.print_help()
73             sys.exit(1)
74
75         if options.nchannels < 1 or options.nchannels > MAX_CHANNELS:
76             sys.stderr.write ("fm_tx4: nchannels out of range.  Must be in [1,%d]\n" % MAX_CHANNELS)
77             sys.exit(1)
78         
79         if options.freq is None:
80             sys.stderr.write("fm_tx4: must specify frequency with -f FREQ\n")
81             parser.print_help()
82             sys.exit(1)
83
84         # ----------------------------------------------------------------
85         # Set up constants and parameters
86
87         self.u = usrp.sink_c ()       # the USRP sink (consumes samples)
88
89         self.dac_rate = self.u.dac_rate()                    # 128 MS/s
90         self.usrp_interp = 400
91         self.u.set_interp_rate(self.usrp_interp)
92         self.usrp_rate = self.dac_rate / self.usrp_interp    # 320 kS/s
93         self.sw_interp = 10
94         self.audio_rate = self.usrp_rate / self.sw_interp    # 32 kS/s
95
96         # determine the daughterboard subdevice we're using
97         if options.tx_subdev_spec is None:
98             options.tx_subdev_spec = usrp.pick_tx_subdevice(self.u)
99
100         m = usrp.determine_tx_mux_value(self.u, options.tx_subdev_spec)
101         #print "mux = %#04x" % (m,)
102         self.u.set_mux(m)
103         self.subdev = usrp.selected_subdev(self.u, options.tx_subdev_spec)
104         print "Using TX d'board %s" % (self.subdev.side_and_name(),)
105
106         self.subdev.set_gain(self.subdev.gain_range()[1])    # set max Tx gain
107         self.set_freq(options.freq)
108         self.subdev.set_enable(True)                         # enable transmitter
109
110         sum = gr.add_cc ()
111
112         # Instantiate N NBFM channels
113         step = 25e3
114         offset = (0 * step, 1 * step, -1 * step, 2 * step, -2 * step, 3 * step, -3 * step)
115         for i in range (options.nchannels):
116             t = pipeline (self, "audio-%d.dat" % (i % 4), offset[i],
117                           self.audio_rate, self.usrp_rate)
118             self.connect (t, (sum, i))
119
120         gain = gr.multiply_const_cc (4000.0 / options.nchannels)
121
122         # connect it all
123         self.connect (sum, gain)
124         self.connect (gain, self.u)
125
126         # plot an FFT to verify we are sending what we want
127         if 1:
128             post_mod = fftsink.fft_sink_c(self, panel, title="Post Modulation",
129                                           fft_size=512, sample_rate=self.usrp_rate,
130                                           y_per_div=20, ref_level=40)
131             self.connect (sum, post_mod)
132             vbox.Add (post_mod.win, 1, wx.EXPAND)
133             
134
135         if options.debug:
136             self.debugger = tx_debug_gui.tx_debug_gui(self.subdev)
137             self.debugger.Show(True)
138
139
140     def set_freq(self, target_freq):
141         """
142         Set the center frequency we're interested in.
143
144         @param target_freq: frequency in Hz
145         @rypte: bool
146
147         Tuning is a two step process.  First we ask the front-end to
148         tune as close to the desired frequency as it can.  Then we use
149         the result of that operation and our target_frequency to
150         determine the value for the digital up converter.  Finally, we feed
151         any residual_freq to the s/w freq translater.
152         """
153
154         r = self.u.tune(self.subdev._which, self.subdev, target_freq)
155         if r:
156             print "r.baseband_freq =", eng_notation.num_to_str(r.baseband_freq)
157             print "r.dxc_freq      =", eng_notation.num_to_str(r.dxc_freq)
158             print "r.residual_freq =", eng_notation.num_to_str(r.residual_freq)
159             print "r.inverted      =", r.inverted
160             
161             # Could use residual_freq in s/w freq translator
162             return True
163
164         return False
165
166 def main ():
167     app = stdgui.stdapp (fm_tx_graph, "Multichannel FM Tx")
168     app.MainLoop ()
169
170 if __name__ == '__main__':
171     main ()