switch source package format to 3.0 quilt
[debian/gnuradio] / gnuradio-examples / python / digital / benchmark_loopback.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 eng_notation
25 from gnuradio.eng_option import eng_option
26 from optparse import OptionParser
27
28 import random, time, struct, sys, math
29
30 # from current dir
31 from transmit_path import transmit_path
32 from receive_path import receive_path
33
34 class my_top_block(gr.top_block):
35     def __init__(self, mod_class, demod_class, rx_callback, options):
36         gr.top_block.__init__(self)
37
38         channelon = True;
39
40         SNR = 10.0**(options.snr/10.0)
41         frequency_offset = options.frequency_offset
42         
43         power_in_signal = abs(options.tx_amplitude)**2
44         noise_power = power_in_signal/SNR
45         noise_voltage = math.sqrt(noise_power)
46
47         # With new interface, sps does not get set by default, but
48         # in the loopback, we don't recalculate it; so just force it here
49         if(options.samples_per_symbol == None):
50             options.samples_per_symbol = 2
51
52         self.txpath = transmit_path(mod_class, options)
53         self.throttle = gr.throttle(gr.sizeof_gr_complex, options.sample_rate)
54         self.rxpath = receive_path(demod_class, rx_callback, options)
55
56         if channelon:
57             self.channel = gr.channel_model(noise_voltage, frequency_offset, 1.01)
58
59             if options.discontinuous:
60                 z = 20000*[0,]
61                 self.zeros = gr.vector_source_c(z, True)
62                 packet_size = 5*((4+8+4+1500+4) * 8)
63                 self.mux = gr.stream_mux(gr.sizeof_gr_complex, [packet_size-0, int(9e5)])
64
65                 # Connect components
66                 self.connect(self.txpath, (self.mux,0))
67                 self.connect(self.zeros, (self.mux,1))
68                 self.connect(self.mux, self.channel, self.rxpath)
69
70             else:
71                 self.connect(self.txpath, self.channel, self.rxpath)
72
73         else:
74             # Connect components
75             self.connect(self.txpath, self.throttle, self.rxpath)
76
77
78 # /////////////////////////////////////////////////////////////////////////////
79 #                                   main
80 # /////////////////////////////////////////////////////////////////////////////
81
82 def main():
83
84     global n_rcvd, n_right
85
86     n_rcvd = 0
87     n_right = 0
88     
89     def rx_callback(ok, payload):
90         global n_rcvd, n_right
91         (pktno,) = struct.unpack('!H', payload[0:2])
92         n_rcvd += 1
93         if ok:
94             n_right += 1
95
96         print "ok = %5s  pktno = %4d  n_rcvd = %4d  n_right = %4d" % (
97             ok, pktno, n_rcvd, n_right)
98         # print payload[2:len(payload)]
99
100     def send_pkt(payload='', eof=False):
101         return tb.txpath.send_pkt(payload, eof)
102
103
104     mods = modulation_utils.type_1_mods()
105     demods = modulation_utils.type_1_demods()
106
107     parser = OptionParser(option_class=eng_option, conflict_handler="resolve")
108     expert_grp = parser.add_option_group("Expert")
109     channel_grp = parser.add_option_group("Channel")
110
111     parser.add_option("-m", "--modulation", type="choice", choices=mods.keys(),
112                       default='dbpsk',
113                       help="Select modulation from: %s [default=%%default]"
114                             % (', '.join(mods.keys()),))
115
116     parser.add_option("-s", "--size", type="eng_float", default=1500,
117                       help="set packet size [default=%default]")
118     parser.add_option("-M", "--megabytes", type="eng_float", default=1.0,
119                       help="set megabytes to transmit [default=%default]")
120     parser.add_option("","--discontinuous", action="store_true", default=False,
121                       help="enable discontinous transmission (bursts of 5 packets)")
122
123     channel_grp.add_option("", "--sample-rate", type="eng_float", default=1e5,
124                            help="set speed of channel/simulation rate to RATE [default=%default]") 
125     channel_grp.add_option("", "--snr", type="eng_float", default=30,
126                            help="set the SNR of the channel in dB [default=%default]")
127     channel_grp.add_option("", "--frequency-offset", type="eng_float", default=0,
128                            help="set frequency offset introduced by channel [default=%default]")
129     channel_grp.add_option("", "--seed", action="store_true", default=False,
130                            help="use a random seed for AWGN noise [default=%default]")
131
132     transmit_path.add_options(parser, expert_grp)
133     receive_path.add_options(parser, expert_grp)
134
135     for mod in mods.values():
136         mod.add_options(expert_grp)
137     for demod in demods.values():
138         demod.add_options(expert_grp)
139
140     (options, args) = parser.parse_args ()
141
142     if len(args) != 0:
143         parser.print_help()
144         sys.exit(1)
145  
146     r = gr.enable_realtime_scheduling()
147     if r != gr.RT_OK:
148         print "Warning: failed to enable realtime scheduling"
149         
150     # Create an instance of a hierarchical block
151     tb = my_top_block(mods[options.modulation], demods[options.modulation], rx_callback, options)
152     tb.start()
153
154     # generate and send packets
155     nbytes = int(1e6 * options.megabytes)
156     n = 0
157     pktno = 0
158     pkt_size = int(options.size)
159
160     while n < nbytes:
161         send_pkt(struct.pack('!H', pktno & 0xffff) + (pkt_size - 2) * chr(pktno & 0xff))
162         n += pkt_size
163         pktno += 1
164         
165     send_pkt(eof=True)
166
167     tb.wait()
168     
169 if __name__ == '__main__':
170     try:
171         main()
172     except KeyboardInterrupt:
173         pass