Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-examples / python / digital / benchmark_rx.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2005,2006,2007,2009 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 from gnuradio import gr, gru, modulation_utils
24 from gnuradio import usrp
25 from gnuradio import eng_notation
26 from gnuradio.eng_option import eng_option
27 from optparse import OptionParser
28
29 import random
30 import struct
31 import sys
32
33 # from current dir
34 import usrp_receive_path
35
36 #import os
37 #print os.getpid()
38 #raw_input('Attach and press enter: ')
39
40 class my_top_block(gr.top_block):
41     def __init__(self, demodulator, rx_callback, options):
42         gr.top_block.__init__(self)
43
44         # Set up receive path
45         self.rxpath = usrp_receive_path.usrp_receive_path(demodulator, rx_callback, options) 
46
47         self.connect(self.rxpath)
48
49 # /////////////////////////////////////////////////////////////////////////////
50 #                                   main
51 # /////////////////////////////////////////////////////////////////////////////
52
53 global n_rcvd, n_right
54
55 def main():
56     global n_rcvd, n_right
57
58     n_rcvd = 0
59     n_right = 0
60     
61     def rx_callback(ok, payload):
62         global n_rcvd, n_right
63         (pktno,) = struct.unpack('!H', payload[0:2])
64         n_rcvd += 1
65         if ok:
66             n_right += 1
67
68         print "ok = %5s  pktno = %4d  n_rcvd = %4d  n_right = %4d" % (
69             ok, pktno, n_rcvd, n_right)
70
71
72     demods = modulation_utils.type_1_demods()
73
74     # Create Options Parser:
75     parser = OptionParser (option_class=eng_option, conflict_handler="resolve")
76     expert_grp = parser.add_option_group("Expert")
77
78     parser.add_option("-m", "--modulation", type="choice", choices=demods.keys(), 
79                       default='gmsk',
80                       help="Select modulation from: %s [default=%%default]"
81                             % (', '.join(demods.keys()),))
82
83     usrp_receive_path.add_options(parser, expert_grp)
84
85     for mod in demods.values():
86         mod.add_options(expert_grp)
87
88     (options, args) = parser.parse_args ()
89
90     if len(args) != 0:
91         parser.print_help(sys.stderr)
92         sys.exit(1)
93
94     if options.rx_freq is None:
95         sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
96         parser.print_help(sys.stderr)
97         sys.exit(1)
98
99
100     # build the graph
101     tb = my_top_block(demods[options.modulation], rx_callback, options)
102
103     r = gr.enable_realtime_scheduling()
104     if r != gr.RT_OK:
105         print "Warning: Failed to enable realtime scheduling."
106
107     tb.start()        # start flow graph
108     tb.wait()         # wait for it to finish
109
110 if __name__ == '__main__':
111     try:
112         main()
113     except KeyboardInterrupt:
114         pass