merged trondeau/digital-wip2 r4193:4730 into trunk - improves digital receiver and...
[debian/gnuradio] / gnuradio-examples / python / digital / benchmark_loopback.py
1 #!/usr/bin/env python
2 #!/usr/bin/env python
3 #
4 # Copyright 2005, 2006 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 2, 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_block):
37     def __init__(self, fg, sample_rate, noise_voltage, frequency_offset, seed=False):
38         self.input = gr.add_const_cc(0) # dummy input device
39         
40         # Create the Gaussian noise source
41         if not seed:
42             self.noise = gr.noise_source_c(gr.GR_GAUSSIAN, noise_voltage)
43         else:
44             rseed = int(time.time())
45             self.noise = gr.noise_source_c(gr.GR_GAUSSIAN, noise_voltage, rseed)
46
47         self.adder =  gr.add_cc()
48
49         # Create the frequency offset
50         self.offset = gr.sig_source_c(1, gr.GR_SIN_WAVE,
51                                       frequency_offset, 1.0, 0.0)
52         self.mixer = gr.multiply_cc()
53
54         # Connect the components
55         fg.connect(self.input, (self.mixer, 0))
56         fg.connect(self.offset, (self.mixer, 1))
57         fg.connect(self.mixer, (self.adder, 0))
58         fg.connect(self.noise, (self.adder, 1))
59
60         gr.hier_block.__init__(self, fg, self.input, self.adder)
61
62 class my_graph(gr.flow_graph):
63     def __init__(self, mod_class, demod_class, rx_callback, options):
64         gr.flow_graph.__init__(self)
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(self, mod_class, options)
76         self.throttle = gr.throttle(gr.sizeof_gr_complex, options.sample_rate)
77         self.rxpath = receive_path(self, demod_class, rx_callback, options)
78
79         if channelon:
80             self.channel = awgn_channel(self, options.sample_rate, noise_voltage, frequency_offset, options.seed)
81
82             # Connect components
83             self.connect(self.txpath, self.throttle, self.channel, self.rxpath)
84         else:
85             # Connect components
86             self.connect(self.txpath, self.throttle, self.rxpath)
87
88
89 # /////////////////////////////////////////////////////////////////////////////
90 #                                   main
91 # /////////////////////////////////////////////////////////////////////////////
92
93 def main():
94
95     global n_rcvd, n_right
96
97     n_rcvd = 0
98     n_right = 0
99     
100     def rx_callback(ok, payload):
101         global n_rcvd, n_right
102         (pktno,) = struct.unpack('!H', payload[0:2])
103         n_rcvd += 1
104         if ok:
105             n_right += 1
106
107         print "ok = %5s  pktno = %4d  n_rcvd = %4d  n_right = %4d" % (
108             ok, pktno, n_rcvd, n_right)
109
110     def send_pkt(payload='', eof=False):
111         return fg.txpath.send_pkt(payload, eof)
112
113
114     mods = modulation_utils.type_1_mods()
115     demods = modulation_utils.type_1_demods()
116
117     parser = OptionParser(option_class=eng_option, conflict_handler="resolve")
118     expert_grp = parser.add_option_group("Expert")
119     channel_grp = parser.add_option_group("Channel")
120
121     parser.add_option("-m", "--modulation", type="choice", choices=mods.keys(),
122                       default='dbpsk',
123                       help="Select modulation from: %s [default=%%default]"
124                             % (', '.join(mods.keys()),))
125
126     parser.add_option("-s", "--size", type="eng_float", default=1500,
127                       help="set packet size [default=%default]")
128     parser.add_option("-M", "--megabytes", type="eng_float", default=1.0,
129                       help="set megabytes to transmit [default=%default]")
130     parser.add_option("","--discontinuous", action="store_true", default=False,
131                       help="enable discontinous transmission (bursts of 5 packets)")
132
133     channel_grp.add_option("", "--sample-rate", type="eng_float", default=1e5,
134                            help="set speed of channel/simulation rate to RATE [default=%default]") 
135     channel_grp.add_option("", "--snr", type="eng_float", default=30,
136                            help="set the SNR of the channel in dB [default=%default]")
137     channel_grp.add_option("", "--frequency-offset", type="eng_float", default=0,
138                            help="set frequency offset introduced by channel [default=%default]")
139     channel_grp.add_option("", "--seed", action="store_true", default=False,
140                            help="use a random seed for AWGN noise [default=%default]")
141
142     transmit_path.add_options(parser, expert_grp)
143     receive_path.add_options(parser, expert_grp)
144
145     for mod in mods.values():
146         mod.add_options(expert_grp)
147     for demod in demods.values():
148         demod.add_options(expert_grp)
149
150     (options, args) = parser.parse_args ()
151
152     if len(args) != 0:
153         parser.print_help()
154         sys.exit(1)
155  
156     r = gr.enable_realtime_scheduling()
157     if r != gr.RT_OK:
158         print "Warning: failed to enable realtime scheduling"
159         
160     # Create an instance of a hierarchical block
161     fg = my_graph(mods[options.modulation], demods[options.modulation], rx_callback, options)
162     fg.start()
163
164     # generate and send packets
165     nbytes = int(1e6 * options.megabytes)
166     n = 0
167     pktno = 0
168     pkt_size = int(options.size)
169
170     while n < nbytes:
171         send_pkt(struct.pack('!H', pktno) + (pkt_size - 2) * chr(pktno & 0xff))
172         n += pkt_size
173         if options.discontinuous and pktno % 5 == 4:
174             time.sleep(1)
175         pktno += 1
176         
177     send_pkt(eof=True)
178
179     fg.wait()
180     
181 if __name__ == '__main__':
182     try:
183         main()
184     except KeyboardInterrupt:
185         pass