merged trondeau/digital-wip2 r4193:4730 into trunk - improves digital receiver and...
[debian/gnuradio] / gnuradio-examples / python / hier / digital / benchmark_rx.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2005,2006 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, 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 from receive_path import receive_path
35 import fusb_options
36
37 #import os
38 #print os.getpid()
39 #raw_input('Attach and press enter: ')
40
41
42 class my_graph(gr.hier_block2):
43     def __init__(self, demod_class, rx_callback, options):
44         gr.hier_block2.__init__(self, "my_graph",
45                                 gr.io_signature(0,0,0), # Input signature
46                                 gr.io_signature(0,0,0)) # Output signature
47         self.rxpath = receive_path(demod_class, rx_callback, options)
48         self.define_component("rxpath", self.rxpath)
49
50
51 # /////////////////////////////////////////////////////////////////////////////
52 #                                   main
53 # /////////////////////////////////////////////////////////////////////////////
54
55 global n_rcvd, n_right
56
57 def main():
58     global n_rcvd, n_right
59
60     n_rcvd = 0
61     n_right = 0
62     
63     def rx_callback(ok, payload):
64         global n_rcvd, n_right
65         (pktno,) = struct.unpack('!H', payload[0:2])
66         n_rcvd += 1
67         if ok:
68             n_right += 1
69
70         print "ok = %5s  pktno = %4d  n_rcvd = %4d  n_right = %4d" % (
71             ok, pktno, n_rcvd, n_right)
72
73
74     demods = modulation_utils.type_1_demods()
75
76     # Create Options Parser:
77     parser = OptionParser (option_class=eng_option, conflict_handler="resolve")
78     expert_grp = parser.add_option_group("Expert")
79
80     parser.add_option("-m", "--modulation", type="choice", choices=demods.keys(), 
81                       default='gmsk',
82                       help="Select modulation from: %s [default=%%default]"
83                             % (', '.join(demods.keys()),))
84
85     receive_path.add_options(parser, expert_grp)
86
87     for mod in demods.values():
88         mod.add_options(expert_grp)
89
90     fusb_options.add_options(expert_grp)
91     (options, args) = parser.parse_args ()
92
93     if len(args) != 0:
94         parser.print_help(sys.stderr)
95         sys.exit(1)
96
97     if options.rx_freq is None:
98         sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
99         parser.print_help(sys.stderr)
100         sys.exit(1)
101
102     r = gr.enable_realtime_scheduling()
103     if r != gr.RT_OK:
104         print "Warning: Failed to enable realtime scheduling."
105
106     # Create an instance of a hierarchical block
107     top_block = my_graph(demods[options.modulation], rx_callback, options)
108     
109     # Create an instance of a runtime, passing it the top block
110     runtime = gr.runtime(top_block)
111     runtime.start()
112
113     runtime.wait()         # wait for it to finish
114
115 if __name__ == '__main__':
116     try:
117         main()
118     except KeyboardInterrupt:
119         pass