3 # Copyright 2006, 2007 Free Software Foundation, Inc.
5 # This file is part of GNU Radio
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)
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.
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.
23 from gnuradio import gr, blks2
24 from gnuradio import eng_notation
25 from gnuradio.eng_option import eng_option
26 from optparse import OptionParser
28 import random, time, struct, sys, math, os
31 from transmit_path import transmit_path
32 from receive_path import receive_path
35 class my_top_block(gr.top_block):
36 def __init__(self, callback, options):
37 gr.top_block.__init__(self)
39 if not options.channel_off:
40 SNR = 10.0**(options.snr/10.0)
41 power_in_signal = abs(options.tx_amplitude)**2.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
46 frequency_offset = options.frequency_offset / options.fft_length
47 print "Frequency offset: ", frequency_offset
49 if options.multipath_on:
50 taps = [1.0, .2, 0.0, .1, .08, -.4, .12, -.2, 0, 0, 0, .3]
56 frequency_offset = 0.0
59 symbols_per_packet = math.ceil(((4+options.size+4) * 8) / options.occupied_tones)
60 samples_per_packet = (symbols_per_packet+2) * (options.fft_length+options.cp_length)
61 print "Symbols per Packet: ", symbols_per_packet
62 print "Samples per Packet: ", samples_per_packet
63 if options.discontinuous:
64 stream_size = [100000, int(options.discontinuous*samples_per_packet)]
66 stream_size = [0, 100000]
69 self.zeros = gr.vector_source_c(z, True)
70 self.txpath = transmit_path(options)
72 #self.mux = gr.stream_mux(gr.sizeof_gr_complex, stream_size)
73 self.throttle = gr.throttle(gr.sizeof_gr_complex, options.sample_rate)
74 self.channel = blks2.channel_model(noise_voltage, frequency_offset,
75 options.clockrate_ratio, taps)
76 self.rxpath = receive_path(callback, options)
78 #self.connect(self.zeros, (self.mux,0))
79 #self.connect(self.txpath, (self.mux,1))
80 #self.connect(self.mux, self.throttle, self.channel, self.rxpath)
81 #self.connect(self.mux, self.throttle, self.rxpath)
82 self.connect(self.txpath, self.throttle, self.channel, self.rxpath)
85 self.connect(self.txpath, gr.file_sink(gr.sizeof_gr_complex, "txpath.dat"))
86 #self.connect(self.mux, gr.file_sink(gr.sizeof_gr_complex, "mux.dat"))
87 #self.connect(self.channel, gr.file_sink(gr.sizeof_gr_complex, "channel.dat"))
89 # /////////////////////////////////////////////////////////////////////////////
91 # /////////////////////////////////////////////////////////////////////////////
94 global n_rcvd, n_right
99 def send_pkt(payload='', eof=False):
100 return tb.txpath.send_pkt(payload, eof)
102 def rx_callback(ok, payload):
103 global n_rcvd, n_right
105 (pktno,) = struct.unpack('!H', payload[0:2])
108 print "ok: %r \t pktno: %d \t n_rcvd: %d \t n_right: %d" % (ok, pktno, n_rcvd, n_right)
111 for x in payload[2:]:
112 t = hex(ord(x)).replace('0x', '')
116 printable = ''.join(printlst)
121 parser = OptionParser(option_class=eng_option, conflict_handler="resolve")
122 expert_grp = parser.add_option_group("Expert")
123 parser.add_option("-s", "--size", type="eng_float", default=400,
124 help="set packet size [default=%default]")
125 parser.add_option("-M", "--megabytes", type="eng_float", default=1.0,
126 help="set megabytes to transmit [default=%default]")
127 parser.add_option("-r", "--sample-rate", type="eng_float", default=1e5,
128 help="limit sample rate to RATE in throttle (%default)")
129 parser.add_option("", "--snr", type="eng_float", default=30,
130 help="set the SNR of the channel in dB [default=%default]")
131 parser.add_option("", "--frequency-offset", type="eng_float", default=0,
132 help="set frequency offset introduced by channel [default=%default]")
133 parser.add_option("", "--clockrate-ratio", type="eng_float", default=1.0,
134 help="set clock rate ratio (sample rate difference) between two systems [default=%default]")
135 parser.add_option("","--discontinuous", type="int", default=0,
136 help="enable discontinous transmission, burst of N packets [Default is continuous]")
137 parser.add_option("","--channel-off", action="store_true", default=False,
138 help="Turns AWGN, freq offset channel off")
139 parser.add_option("","--multipath-on", action="store_true", default=False,
140 help="enable multipath")
142 transmit_path.add_options(parser, expert_grp)
143 receive_path.add_options(parser, expert_grp)
144 blks2.ofdm_mod.add_options(parser, expert_grp)
145 blks2.ofdm_demod.add_options(parser, expert_grp)
147 (options, args) = parser.parse_args ()
150 tb = my_top_block(rx_callback, options)
152 r = gr.enable_realtime_scheduling()
154 # print "Warning: failed to enable realtime scheduling"
156 tb.start() # start flow graph
158 # generate and send packets
159 nbytes = int(1e6 * options.megabytes)
162 pkt_size = int(options.size)
165 #r = ''.join([chr(random.randint(0,255)) for i in range(pkt_size-2)])
166 #pkt_contents = struct.pack('!H', pktno) + r
168 pkt_contents = struct.pack('!H', pktno) + (pkt_size - 2) * chr(pktno & 0xff)
170 send_pkt(pkt_contents)
172 #sys.stderr.write('.')
173 #if options.discontinuous and pktno % 5 == 4:
178 tb.wait() # wait for it to finish
181 if __name__ == '__main__':
184 except KeyboardInterrupt: