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 2, 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, blks
24 from gnuradio import eng_notation
25 from gnuradio.eng_option import eng_option
26 from optparse import OptionParser
27 from gnuradio.blksimpl import ofdm_pkt
29 import random, time, struct, sys, math, os
32 from transmit_path import transmit_path
33 from receive_path import receive_path
36 class my_graph(gr.flow_graph):
37 def __init__(self, callback, options):
38 gr.flow_graph.__init__(self)
40 if options.channel_on:
41 SNR = 10.0**(options.snr/10.0)
43 noise_power_in_channel = power_in_signal/SNR
44 noise_voltage = math.sqrt(noise_power_in_channel/2.0)
45 print "Noise voltage: ", noise_voltage
47 frequency_offset = options.frequency_offset / options.fft_length
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(self, 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 = blks.channel_model(self, noise_voltage, frequency_offset, options.clockrate_ratio, taps)
75 self.rxpath = receive_path(self, callback, options)
77 self.connect(self.zeros, (self.mux,0))
78 self.connect(self.txpath, (self.mux,1))
79 self.connect(self.mux, self.throttle, self.channel, self.rxpath)
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"))
85 # /////////////////////////////////////////////////////////////////////////////
87 # /////////////////////////////////////////////////////////////////////////////
90 global n_rcvd, n_right
95 def send_pkt(payload='', eof=False):
96 return fg.txpath.send_pkt(payload, eof)
98 def rx_callback(ok, payload):
99 global n_rcvd, n_right
101 (pktno,) = struct.unpack('!H', payload[0:2])
104 print "ok: %r \t pktno: %d \t n_rcvd: %d \t n_right: %d" % (ok, pktno, n_rcvd, n_right)
107 for x in payload[2:]:
108 t = hex(ord(x)).replace('0x', '')
112 printable = ''.join(printlst)
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="set sample rate to RATE (%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")
138 transmit_path.add_options(parser, expert_grp)
139 receive_path.add_options(parser, expert_grp)
140 ofdm_pkt.mod_ofdm_pkts.add_options(parser, expert_grp)
141 ofdm_pkt.demod_ofdm_pkts.add_options(parser, expert_grp)
143 (options, args) = parser.parse_args ()
146 fg = my_graph(rx_callback, options)
148 r = gr.enable_realtime_scheduling()
150 # print "Warning: failed to enable realtime scheduling"
152 fg.start() # start flow graph
154 # generate and send packets
155 nbytes = int(1e6 * options.megabytes)
158 pkt_size = int(options.size)
161 #r = ''.join([chr(random.randint(0,255)) for i in range(pkt_size-2)])
162 #pkt_contents = struct.pack('!H', pktno) + r
164 pkt_contents = struct.pack('!H', pktno) + (pkt_size - 2) * chr(pktno & 0xff)
166 send_pkt(pkt_contents)
168 #sys.stderr.write('.')
169 #if options.discontinuous and pktno % 5 == 4:
174 fg.wait() # wait for it to finish
177 if __name__ == '__main__':
180 except KeyboardInterrupt: