Merged r5556:5561 from jcorgan/snd into trunk. Updates gr-sounder with receive proces...
[debian/gnuradio] / gr-sounder / src / python / usrp_sounder.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2007 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 2, 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
24 from gnuradio.sounder import sounder
25 from gnuradio import eng_notation
26 from gnuradio.eng_option import eng_option
27 from optparse import OptionParser
28 import sys
29
30 n2s = eng_notation.num_to_str
31
32 def main():
33     parser = OptionParser(option_class=eng_option)
34     parser.add_option("-R", "--rx-subdev-spec", type="subdev", default=(0, 0),
35                       help="select USRP Rx side A or B")
36     parser.add_option("-f", "--frequency", type="eng_float", default=0.0,
37                       help="set frequency to FREQ in Hz, default is %default", metavar="FREQ")
38     parser.add_option("-d", "--degree", type="int", default=12,
39                       help="set sounding sequence degree (2-12), default is %default,")
40     parser.add_option("-t", "--transmit", action="store_true", default=False,
41                       help="enable sounding transmitter")
42     parser.add_option("-r", "--receive", action="store_true", default=False,
43                       help="enable sounding receiver")
44     parser.add_option("-l", "--loopback", action="store_true", default=False,
45                       help="enable digital loopback, default is disabled")
46     parser.add_option("-v", "--verbose", action="store_true", default=False,
47                       help="enable verbose output, default is disabled")
48     parser.add_option("-D", "--debug", action="store_true", default=False,
49                       help="enable debugging output, default is disabled")
50     parser.add_option("-F", "--filename", default=None,
51                       help="log received impulse responses to file")
52                       
53     (options, args) = parser.parse_args()
54
55     if len(args) != 0 or not (options.transmit | options.receive):
56         parser.print_help()
57         sys.exit(1)
58
59     if options.receive and (options.filename == None):
60         print "Must supply filename when receiving."
61         sys.exit(1)
62
63     if options.degree > 12 or options.degree < 2:
64         print "PN code degree must be between 2 and 12"
65         sys.exit(1)
66
67     length = int(2**options.degree-1)
68     if options.verbose:
69         print "Using PN code degree of", options.degree, "length", length
70         if options.loopback == False:
71             print "Sounding frequency range is", n2s(options.frequency-16e6), "to", n2s(options.frequency+16e6)
72         if options.filename != None:
73             print "Logging impulse records to file: ", options.filename
74             
75     msgq = gr.msg_queue()
76     s = sounder(transmit=options.transmit,receive=options.receive,loopback=options.loopback,
77                 rx_subdev_spec=options.rx_subdev_spec,frequency=options.frequency,degree=options.degree,
78                 length=length,msgq=msgq,verbose=options.verbose,debug=options.debug)
79     s.start()
80
81     if options.receive:
82         f = open(options.filename, "wb")
83         print "Enter CTRL-C to stop."
84         try:
85             while (1):
86                 msg = msgq.delete_head()
87                 if msg.type() == 1:
88                     break
89                 rec = msg.to_string()[:length*gr.sizeof_gr_complex]
90                 if options.debug:
91                     print "Received impulse vector of length", len(rec)
92                 f.write(rec)
93                 
94         except KeyboardInterrupt:
95             pass
96     else:
97         if options.transmit:
98             raw_input("Press return to exit.")
99         
100 if __name__ == "__main__":
101     main()