Merging OFDM features branch r5661:5759 into trunk. OFDM works over the air with...
[debian/gnuradio] / gnuradio-examples / python / ofdm / benchmark_ofdm.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2006, 2007 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 2, 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, blks
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, os
29
30 # from current dir
31 from transmit_path import transmit_path
32 from receive_path import receive_path
33
34
35 class my_graph(gr.flow_graph):
36     def __init__(self, callback, options):
37         gr.flow_graph.__init__(self)
38
39         if options.channel_on:
40             SNR = 10.0**(options.snr/10.0)
41             power_in_signal = 1.0
42             noise_power_in_channel = power_in_signal/SNR
43             noise_voltage = math.sqrt(noise_power_in_channel/2.0)
44             print "Noise voltage: ", noise_voltage
45
46             frequency_offset = options.frequency_offset / options.fft_length
47
48             if options.multipath_on:
49                 taps = [1.0, .2, 0.0, .1, .08, -.4, .12, -.2, 0, 0, 0, .3]
50             else:
51                 taps = [1.0, 0.0]
52
53         else:
54             noise_voltage = 0.0
55             frequency_offset = 0.0
56             taps = [1.0, 0.0]
57
58         symbols_per_packet = math.ceil(((4+options.size+4) * 8) / options.occupied_tones)
59         samples_per_packet = (symbols_per_packet+2) * (options.fft_length+options.cp_length)
60         print "Symbols per Packet: ", symbols_per_packet
61         print "Samples per Packet: ", samples_per_packet
62         if options.discontinuous:
63             stream_size = [100000, int(options.discontinuous*samples_per_packet)]
64         else:
65             stream_size = [0, 100000]
66
67         z = [0,]
68         self.zeros = gr.vector_source_c(z, True)
69         self.txpath = transmit_path(self, options)
70
71         self.mux = gr.stream_mux(gr.sizeof_gr_complex, stream_size)
72         self.throttle = gr.throttle(gr.sizeof_gr_complex, options.sample_rate)
73         self.channel = blks.channel_model(self, noise_voltage, frequency_offset, options.clockrate_ratio, taps)
74         self.rxpath = receive_path(self, callback, options)
75                 
76         self.connect(self.zeros, (self.mux,0))
77         self.connect(self.txpath, (self.mux,1))
78         self.connect(self.mux, self.throttle, self.channel, self.rxpath)
79
80         if options.log:
81             self.connect(self.txpath, gr.file_sink(gr.sizeof_gr_complex, "txpath.dat"))
82             self.connect(self.mux, gr.file_sink(gr.sizeof_gr_complex, "mux.dat"))
83             self.connect(self.channel, gr.file_sink(gr.sizeof_gr_complex, "channel.dat"))
84             
85 # /////////////////////////////////////////////////////////////////////////////
86 #                                   main
87 # /////////////////////////////////////////////////////////////////////////////
88
89 def main():
90     global n_rcvd, n_right
91         
92     n_rcvd = 0
93     n_right = 0
94         
95     def send_pkt(payload='', eof=False):
96         return fg.txpath.send_pkt(payload, eof)
97         
98     def rx_callback(ok, payload):
99         global n_rcvd, n_right
100         n_rcvd += 1
101         (pktno,) = struct.unpack('!H', payload[0:2])
102         if ok:
103             n_right += 1
104         print "ok: %r \t pktno: %d \t n_rcvd: %d \t n_right: %d" % (ok, pktno, n_rcvd, n_right)
105
106         printlst = list()
107         for x in payload[2:]:
108             t = hex(ord(x)).replace('0x', '')
109             if(len(t) == 1):
110                 t = '0' + t
111             printlst.append(t)
112         printable = ''.join(printlst)
113
114         print printable
115         print "\n"
116                 
117     parser = OptionParser(option_class=eng_option, conflict_handler="resolve")
118     expert_grp = parser.add_option_group("Expert")
119     parser.add_option("-s", "--size", type="eng_float", default=1450,
120                       help="set packet size [default=%default]")
121     parser.add_option("-M", "--megabytes", type="eng_float", default=1.0,
122                       help="set megabytes to transmit [default=%default]")
123     parser.add_option("-r", "--sample-rate", type="eng_float", default=1e5,
124                       help="limit sample rate to RATE in throttle (%default)") 
125     parser.add_option("", "--snr", type="eng_float", default=30,
126                       help="set the SNR of the channel in dB [default=%default]")
127     parser.add_option("", "--frequency-offset", type="eng_float", default=0,
128                       help="set frequency offset introduced by channel [default=%default]")
129     parser.add_option("", "--clockrate-ratio", type="eng_float", default=1.0,
130                       help="set clock rate ratio (sample rate difference) between two systems [default=%default]")
131     parser.add_option("","--discontinuous", type="int", default=0,
132                       help="enable discontinous transmission, burst of N packets [Default is continuous]")
133     parser.add_option("","--channel-on", action="store_true", default=True,
134                       help="Enables AWGN, freq offset")
135     parser.add_option("","--multipath-on", action="store_true", default=False,
136                       help="enable multipath")
137
138     transmit_path.add_options(parser, expert_grp)
139     receive_path.add_options(parser, expert_grp)
140     blks.ofdm_mod.add_options(parser, expert_grp)
141     blks.ofdm_demod.add_options(parser, expert_grp)
142     
143     (options, args) = parser.parse_args ()
144        
145     # build the graph
146     fg = my_graph(rx_callback, options)
147     
148     r = gr.enable_realtime_scheduling()
149     #    if r != gr.RT_OK:
150     #        print "Warning: failed to enable realtime scheduling"
151         
152     fg.start()                       # start flow graph
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         #r = ''.join([chr(random.randint(0,255)) for i in range(pkt_size-2)])
162         #pkt_contents = struct.pack('!H', pktno) + r
163
164         pkt_contents = struct.pack('!H', pktno) + (pkt_size - 2) * chr(pktno & 0xff)
165
166         send_pkt(pkt_contents)
167         n += pkt_size
168         #sys.stderr.write('.')
169         #if options.discontinuous and pktno % 5 == 4:
170         #    time.sleep(1)
171         pktno += 1
172         
173     send_pkt(eof=True)
174     fg.wait()                       # wait for it to finish
175
176
177 if __name__ == '__main__':
178     try:
179         main()
180     except KeyboardInterrupt:
181         pass
182
183