Fixed base class name.
[debian/gnuradio] / gnuradio-examples / python / hier / digital / benchmark_loopback.py
1 #!/usr/bin/env python
2 #!/usr/bin/env python
3 #
4 # Copyright 2005, 2006,2007 Free Software Foundation, Inc.
5
6 # This file is part of GNU Radio
7
8 # GNU Radio is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3, or (at your option)
11 # any later version.
12
13 # GNU Radio is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17
18 # You should have received a copy of the GNU General Public License
19 # along with GNU Radio; see the file COPYING.  If not, write to
20 # the Free Software Foundation, Inc., 51 Franklin Street,
21 # Boston, MA 02110-1301, USA.
22
23
24 from gnuradio import gr, gru, modulation_utils
25 from gnuradio import eng_notation
26 from gnuradio.eng_option import eng_option
27 from optparse import OptionParser
28
29 import random, time, struct, sys, math
30
31 # from current dir
32 from transmit_path_lb import transmit_path
33 from receive_path_lb import receive_path
34 import fusb_options
35
36 class awgn_channel(gr.hier_block2):
37     def __init__(self, sample_rate, noise_voltage, frequency_offset, seed=False):
38         gr.hier_block2.__init__(self, "awgn_channel",
39                                 gr.io_signature(1,1,gr.sizeof_gr_complex), # Input signature
40                                 gr.io_signature(1,1,gr.sizeof_gr_complex)) # Output signature
41
42         # Create the Gaussian noise source
43         if not seed:
44             self.noise = gr.noise_source_c(gr.GR_GAUSSIAN, noise_voltage)
45         else:
46             rseed = int(time.time())
47             self.noise = gr.noise_source_c(gr.GR_GAUSSIAN, noise_voltage, rseed)
48         self.adder = gr.add_cc()
49
50         # Create the frequency offset
51         self.offset = gr.sig_source_c((sample_rate*1.0), gr.GR_SIN_WAVE, frequency_offset, 1.0, 0.0)
52         self.mixer = gr.multiply_cc()
53
54         # Connect the components
55         self.connect(self,        (self.mixer, 0))
56         self.connect(self.offset, (self.mixer, 1))
57         self.connect(self.mixer,  (self.adder, 0))
58         self.connect(self.noise,  (self.adder, 1))
59         self.connect(self.adder,   self)
60
61
62 class my_graph(gr.top_block):
63     def __init__(self, mod_class, demod_class, rx_callback, options):
64         gr.top_block.__init__(self, "my_graph")
65
66         channelon = True;
67
68         SNR = 10.0**(options.snr/10.0)
69         frequency_offset = options.frequency_offset
70         
71         power_in_signal = abs(options.tx_amplitude)**2
72         noise_power = power_in_signal/SNR
73         noise_voltage = math.sqrt(noise_power)
74
75         self.txpath = transmit_path(mod_class, options)
76         self.throttle = gr.throttle(gr.sizeof_gr_complex, options.sample_rate)
77         self.rxpath = receive_path(demod_class, rx_callback, options)
78
79         if channelon:
80             self.channel = awgn_channel(options.sample_rate, noise_voltage, frequency_offset, options.seed)
81             self.connect(self.txpath, self.throttle, self.channel, self.rxpath)
82         else:
83             self.connect(self.txpath, self.throttle, self.rxpath)
84
85 # /////////////////////////////////////////////////////////////////////////////
86 #                                   main
87 # /////////////////////////////////////////////////////////////////////////////
88
89 def main():
90
91     global n_rcvd, n_right
92
93     n_rcvd = 0
94     n_right = 0
95     
96     def rx_callback(ok, payload):
97         global n_rcvd, n_right
98         (pktno,) = struct.unpack('!H', payload[0:2])
99         n_rcvd += 1
100         if ok:
101             n_right += 1
102
103         print "ok = %5s  pktno = %4d  n_rcvd = %4d  n_right = %4d" % (
104             ok, pktno, n_rcvd, n_right)
105
106     def send_pkt(payload='', eof=False):
107         return top_block.txpath.send_pkt(payload, eof)
108
109
110     mods = modulation_utils.type_1_mods()
111     demods = modulation_utils.type_1_demods()
112
113     parser = OptionParser(option_class=eng_option, conflict_handler="resolve")
114     expert_grp = parser.add_option_group("Expert")
115     channel_grp = parser.add_option_group("Channel")
116
117     parser.add_option("-m", "--modulation", type="choice", choices=mods.keys(),
118                       default='dbpsk',
119                       help="Select modulation from: %s [default=%%default]"
120                             % (', '.join(mods.keys()),))
121
122     parser.add_option("-s", "--size", type="eng_float", default=1500,
123                       help="set packet size [default=%default]")
124     parser.add_option("-M", "--megabytes", type="eng_float", default=1.0,
125                       help="set megabytes to transmit [default=%default]")
126     parser.add_option("","--discontinuous", action="store_true", default=False,
127                       help="enable discontinous transmission (bursts of 5 packets)")
128
129     channel_grp.add_option("", "--sample-rate", type="eng_float", default=1e5,
130                            help="set speed of channel/simulation rate to RATE [default=%default]") 
131     channel_grp.add_option("", "--snr", type="eng_float", default=30,
132                            help="set the SNR of the channel in dB [default=%default]")
133     channel_grp.add_option("", "--frequency-offset", type="eng_float", default=0,
134                            help="set frequency offset introduced by channel [default=%default]")
135     channel_grp.add_option("", "--seed", action="store_true", default=False,
136                            help="use a random seed for AWGN noise [default=%default]")
137
138     transmit_path.add_options(parser, expert_grp)
139     receive_path.add_options(parser, expert_grp)
140
141     for mod in mods.values():
142         mod.add_options(expert_grp)
143     for demod in demods.values():
144         demod.add_options(expert_grp)
145
146     (options, args) = parser.parse_args ()
147
148     if len(args) != 0:
149         parser.print_help()
150         sys.exit(1)
151  
152     r = gr.enable_realtime_scheduling()
153     if r != gr.RT_OK:
154         print "Warning: failed to enable realtime scheduling"
155         
156     # Create an instance of a hierarchical block
157     top_block = my_graph(mods[options.modulation], demods[options.modulation], rx_callback, options)
158     top_block.start()
159
160     # generate and send packets
161     nbytes = int(1e6 * options.megabytes)
162     n = 0
163     pktno = 0
164     pkt_size = int(options.size)
165
166     while n < nbytes:
167         send_pkt(struct.pack('!H', pktno) + (pkt_size - 2) * chr(pktno & 0xff))
168         n += pkt_size
169         if options.discontinuous and pktno % 5 == 4:
170             time.sleep(1)
171         pktno += 1
172         
173     send_pkt(eof=True)
174
175     top_block.wait()
176     
177 if __name__ == '__main__':
178     try:
179         main()
180     except KeyboardInterrupt:
181         pass