Updated license from GPL version 2 or later to GPL version 3 or later.
[debian/gnuradio] / gnuradio-examples / python / digital / limbo / benchmark_mpsk_rx.py
1 #!/usr/bin/env python
2 #
3 # Copyright 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 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 bpsk import bpsk_demod
34 from dbpsk import dbpsk_demod
35 from dqpsk import dqpsk_demod
36 from receive_path import receive_path
37 import fusb_options
38
39 if 1:
40     import os
41     print os.getpid()
42     raw_input('Attach and press enter')
43
44
45 class my_graph(gr.flow_graph):
46
47     def __init__(self, demod_class, rx_subdev_spec,
48                  bitrate, decim_rate, spb,
49                  rx_callback, options, demod_kwargs):
50         gr.flow_graph.__init__(self)
51         self.rxpath = receive_path(self, demod_class, rx_subdev_spec,
52                                    bitrate, decim_rate, spb,
53                                    rx_callback, options, demod_kwargs)
54
55 # /////////////////////////////////////////////////////////////////////////////
56 #                                   main
57 # /////////////////////////////////////////////////////////////////////////////
58
59 global n_rcvd, n_right
60
61 def main():
62     global n_rcvd, n_right
63
64     n_rcvd = 0
65     n_right = 0
66     
67     def rx_callback(ok, payload):
68         global n_rcvd, n_right
69         (pktno,) = struct.unpack('!H', payload[0:2])
70         n_rcvd += 1
71         if ok:
72             n_right += 1
73
74         print "ok = %r  pktno = %4d  n_rcvd = %4d  n_right = %4d" % (
75             ok, pktno, n_rcvd, n_right)
76
77     parser = OptionParser (option_class=eng_option)
78     parser.add_option("-R", "--rx-subdev-spec", type="subdev", default=None,
79                       help="select USRP Rx side A or B")
80     parser.add_option("-f", "--freq", type="eng_float", default=423.1e6,
81                       help="set Rx frequency to FREQ [default=%default]",
82                       metavar="FREQ")
83     parser.add_option("-r", "--bitrate", type="eng_float", default=None,
84                       help="specify bitrate.  spb and interp will be derived.")
85     parser.add_option("-S", "--spb", type="int", default=None,
86                       help="set samples/baud [default=%default]")
87     parser.add_option("-d", "--decim", type="intx", default=None,
88                       help="set fpga decim rate to DECIM [default=%default]")
89     parser.add_option("-m", "--modulation", type="string", default='dbpsk',
90                       help="modulation type (bpsk, dbpsk, dqpsk) [default=%default]")
91     parser.add_option("", "--excess-bw", type="float", default=0.3,
92                       help="set RRC excess bandwith factor [default=%default]")
93     parser.add_option("-g", "--gain", type="eng_float", default=27,
94                       help="set rx gain")
95     parser.add_option("","--log", action="store_true", default=False,
96                       help="enable diagnostic logging")
97     fusb_options.add_options(parser)
98     (options, args) = parser.parse_args ()
99
100     if len(args) != 0:
101         parser.print_help()
102         sys.exit(1)
103
104     if options.freq < 1e6:
105         options.freq *= 1e6
106
107     demod_kwargs = {
108             'excess_bw' : options.excess_bw,
109             }
110
111     #FIXME: Needs to be worked in to overall structure; this will be fixed
112     #       once static class definitions for modulations are defined
113     if(options.modulation=='bpsk'):
114         modulation=bpsk_demod
115     elif(options.modulation=='dbpsk'):
116         modulation=dbpsk_demod
117     else:
118         modulation=dqpsk_demod
119
120     # build the graph
121     fg = my_graph(modulation,
122                   options.rx_subdev_spec, options.bitrate,
123                   options.decim, options.spb,
124                   rx_callback, options, demod_kwargs)
125
126     print "bitrate: %sb/sec" % (eng_notation.num_to_str(fg.rxpath.bitrate()),)
127     print "spb:     %3d" % (fg.rxpath.spb(),)
128     print "decim:   %3d" % (fg.rxpath.decim(),)
129
130     ok = fg.rxpath.set_freq(options.freq)
131     if not ok:
132         print "Failed to set Rx frequency to %s" % (eng_notation.num_to_str(options.freq),)
133         raise SystemExit
134     
135     fg.rxpath.set_gain(options.gain)
136     print "Rx gain_range: ", fg.rxpath.subdev.gain_range(), " using", fg.rxpath.gain
137
138     r = gr.enable_realtime_scheduling()
139     if r != gr.RT_OK:
140         print "Warning: Failed to enable realtime scheduling."
141
142     fg.start()        # start flow graph
143     fg.wait()         # wait for it to finish
144
145 if __name__ == '__main__':
146     try:
147         main()
148     except KeyboardInterrupt:
149         pass