Updated usrp2_rx_cfile.py and usrp2_siggen.py to use updated gr-usrp2
[debian/gnuradio] / gr-utils / src / python / usrp2_rx_cfile.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 """
24 Read samples from the USRP2 and write to file formatted as binary
25 outputs single precision complex float values or complex short values 
26 (interleaved 16 bit signed short integers).
27 """
28
29 from gnuradio import gr, eng_notation
30 from gnuradio import usrp2
31 from gnuradio.eng_option import eng_option
32 from optparse import OptionParser
33 import sys
34
35 n2s = eng_notation.num_to_str
36
37 class rx_cfile_block(gr.top_block):
38
39     def __init__(self, options, filename):
40         gr.top_block.__init__(self)
41
42         # Create a USRP2 source
43         if options.output_shorts:
44             self._u = usrp2.source_16sc(options.interface, options.mac_addr)
45             self._sink = gr.file_sink(gr.sizeof_short*2, filename)
46         else:
47             self._u = usrp2.source_32fc(options.interface, options.mac_addr)
48             self._sink = gr.file_sink(gr.sizeof_gr_complex, filename)
49
50         # Set receiver decimation rate
51         self._u.set_decim(options.decim)
52
53         # Set receive daughterboard gain
54         if options.gain is None:
55             g = self._u.gain_range()
56             options.gain = float(g[0]+g[1])/2
57             print "Using mid-point gain of", options.gain, "(", g[0], "-", g[1], ")"
58         self._u.set_gain(options.gain)
59
60         # Set receive frequency
61         tr = self._u.set_center_freq(options.freq)
62         if tr == None:
63             sys.stderr.write('Failed to set center frequency\n')
64             raise SystemExit, 1
65
66         # Create head block if needed and wire it up
67         if options.nsamples is None:
68             self.connect(self._u, self._sink)
69         else:
70             if options.output_shorts:
71                 self._head = gr.head(gr.sizeof_short*2, int(options.nsamples))
72             else:
73                 self._head = gr.head(gr.sizeof_gr_complex, int(options.nsamples))
74
75             self.connect(self._u, self._head, self._sink)
76
77         input_rate = self._u.adc_rate()/self._u.decim()
78         
79         if options.verbose:
80             print "Network interface:", options.interface
81             print "USRP2 address:", self._u.mac_addr()
82             print "Using RX d'board id 0x%04X" % (self._u.daughterboard_id(),)
83             print "Rx gain:", options.gain
84             print "Rx baseband frequency:", n2s(tr.baseband_freq)
85             print "Rx DDC frequency:", n2s(tr.dxc_freq)
86             print "Rx residual frequency:", n2s(tr.residual_freq)
87             print "Rx decimation rate:", options.decim
88             print "Rx sample rate:", n2s(input_rate)
89             if options.nsamples is None:
90                 print "Receiving samples until Ctrl-C"
91             else:
92                 print "Receving", n2s(options.nsamples), "samples"
93             if options.output_shorts:
94                 print "Writing 16-bit complex shorts"
95             else:
96                 print "Writing 32-bit complex floats"
97             print "Output filename:", filename
98         
99 def get_options():
100     usage="%prog: [options] output_filename"
101     parser = OptionParser(option_class=eng_option, usage=usage)
102     parser.add_option("-e", "--interface", type="string", default="eth0",
103                       help="use specified Ethernet interface [default=%default]")
104     parser.add_option("-m", "--mac-addr", type="string", default="",
105                       help="use USRP2 at specified MAC address [default=None]")  
106     parser.add_option("-d", "--decim", type="int", default=16,
107                       help="set fgpa decimation rate to DECIM [default=%default]")
108     parser.add_option("-f", "--freq", type="eng_float", default=None,
109                       help="set frequency to FREQ", metavar="FREQ")
110     parser.add_option("-g", "--gain", type="eng_float", default=None,
111                       help="set gain in dB (default is midpoint)")
112     parser.add_option( "-s","--output-shorts", action="store_true", default=False,
113                       help="output interleaved shorts instead of complex floats")
114     parser.add_option("-N", "--nsamples", type="eng_float", default=None,
115                       help="number of samples to collect [default=+inf]")
116     parser.add_option("-v", "--verbose", action="store_true", default=False,
117                       help="verbose output")
118     (options, args) = parser.parse_args ()
119     if len(args) != 1:
120         parser.print_help()
121         raise SystemExit, 1
122     
123     if options.freq is None:
124         parser.print_help()
125         sys.stderr.write('You must specify the frequency with -f FREQ\n');
126         raise SystemExit, 1
127     
128     return (options, args[0])
129
130
131 if __name__ == '__main__':
132     (options, filename) = get_options()
133     tb = rx_cfile_block(options, filename)
134     
135     try:
136         tb.run()
137     except KeyboardInterrupt:
138         pass