Updated license from GPL version 2 or later to GPL version 3 or later.
[debian/gnuradio] / gnuradio-examples / python / digital / limbo / benchmark_gmsk_rx.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2005 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, blks
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
32 # from current dir
33 from receive_path import receive_path
34 import fusb_options
35
36 #import os
37 #print os.getpid()
38 #raw_input('Attach and press enter')
39
40
41 class my_graph(gr.flow_graph):
42
43     def __init__(self, demod_class, rx_subdev_spec,
44                  bitrate, decim_rate, spb,
45                  rx_callback, options, demod_kwargs):
46         gr.flow_graph.__init__(self)
47         self.rxpath = receive_path(self, demod_class, rx_subdev_spec,
48                                    bitrate, decim_rate, spb,
49                                    rx_callback, options, demod_kwargs)
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 = %r  pktno = %4d  n_rcvd = %4d  n_right = %4d" % (
71             ok, pktno, n_rcvd, n_right)
72
73     parser = OptionParser (option_class=eng_option)
74     parser.add_option("-R", "--rx-subdev-spec", type="subdev", default=None,
75                       help="select USRP Rx side A or B")
76     parser.add_option("-f", "--freq", type="eng_float", default=423.1e6,
77                       help="set Rx frequency to FREQ [default=%default]",
78                       metavar="FREQ")
79     parser.add_option("-r", "--bitrate", type="eng_float", default=None,
80                       help="specify bitrate.  spb and interp will be derived.")
81     parser.add_option("-g", "--rx-gain", type="eng_float", default=27,
82                       help="set rx gain")
83     parser.add_option("-S", "--spb", type="int", default=None,
84                       help="set samples/baud [default=%default]")
85     parser.add_option("-d", "--decim", type="intx", default=None,
86                       help="set fpga decim rate to DECIM [default=%default]")
87     fusb_options.add_options(parser)
88     (options, args) = parser.parse_args ()
89
90     if len(args) != 0:
91         parser.print_help()
92         sys.exit(1)
93
94     if options.freq < 1e6:
95         options.freq *= 1e6
96
97     demod_kwargs = { } # placeholder    
98     
99     # build the graph
100     fg = my_graph(blks.gmsk_demod,
101                   options.rx_subdev_spec, options.bitrate,
102                   options.decim, options.spb, rx_callback,
103                   options, demod_kwargs)
104
105     print "bitrate: %sb/sec" % (eng_notation.num_to_str(fg.rxpath.bitrate()),)
106     print "spb:     %3d" % (fg.rxpath.spb(),)
107     print "decim:   %3d" % (fg.rxpath.decim(),)
108
109     ok = fg.rxpath.set_freq(options.freq)
110     if not ok:
111         print "Failed to set Rx frequency to %s" % (eng_notation.num_to_str(options.freq),)
112         raise SystemExit
113     
114     fg.rxpath.set_gain(options.rx_gain)
115     print "Rx gain_range: ", fg.rxpath.subdev.gain_range(), " using", fg.rxpath.gain
116
117     r = gr.enable_realtime_scheduling()
118     if r != gr.RT_OK:
119         print "Warning: Failed to enable realtime scheduling."
120
121     fg.start()        # start flow graph
122     fg.wait()         # wait for it to finish
123
124 if __name__ == '__main__':
125     try:
126         main()
127     except KeyboardInterrupt:
128         pass