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, blks
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_graph(gr.flow_graph):
36 def __init__(self, callback, options):
37 gr.flow_graph.__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(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,
75 options.clockrate_ratio, taps)
76 self.rxpath = receive_path(self, 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)
83 self.connect(self.txpath, gr.file_sink(gr.sizeof_gr_complex, "txpath.dat"))
84 self.connect(self.mux, gr.file_sink(gr.sizeof_gr_complex, "mux.dat"))
85 self.connect(self.channel, gr.file_sink(gr.sizeof_gr_complex, "channel.dat"))
87 # /////////////////////////////////////////////////////////////////////////////
89 # /////////////////////////////////////////////////////////////////////////////
92 global n_rcvd, n_right
97 def send_pkt(payload='', eof=False):
98 return fg.txpath.send_pkt(payload, eof)
100 def rx_callback(ok, payload):
101 global n_rcvd, n_right
103 (pktno,) = struct.unpack('!H', payload[0:2])
106 print "ok: %r \t pktno: %d \t n_rcvd: %d \t n_right: %d" % (ok, pktno, n_rcvd, n_right)
109 for x in payload[2:]:
110 t = hex(ord(x)).replace('0x', '')
114 printable = ''.join(printlst)
119 parser = OptionParser(option_class=eng_option, conflict_handler="resolve")
120 expert_grp = parser.add_option_group("Expert")
121 parser.add_option("-s", "--size", type="eng_float", default=400,
122 help="set packet size [default=%default]")
123 parser.add_option("-M", "--megabytes", type="eng_float", default=1.0,
124 help="set megabytes to transmit [default=%default]")
125 parser.add_option("-r", "--sample-rate", type="eng_float", default=1e5,
126 help="limit sample rate to RATE in throttle (%default)")
127 parser.add_option("", "--snr", type="eng_float", default=30,
128 help="set the SNR of the channel in dB [default=%default]")
129 parser.add_option("", "--frequency-offset", type="eng_float", default=0,
130 help="set frequency offset introduced by channel [default=%default]")
131 parser.add_option("", "--clockrate-ratio", type="eng_float", default=1.0,
132 help="set clock rate ratio (sample rate difference) between two systems [default=%default]")
133 parser.add_option("","--discontinuous", type="int", default=0,
134 help="enable discontinous transmission, burst of N packets [Default is continuous]")
135 parser.add_option("","--channel-off", action="store_true", default=False,
136 help="Turns AWGN, freq offset channel off")
137 parser.add_option("","--multipath-on", action="store_true", default=False,
138 help="enable multipath")
140 transmit_path.add_options(parser, expert_grp)
141 receive_path.add_options(parser, expert_grp)
142 blks.ofdm_mod.add_options(parser, expert_grp)
143 blks.ofdm_demod.add_options(parser, expert_grp)
145 (options, args) = parser.parse_args ()
148 fg = my_graph(rx_callback, options)
150 r = gr.enable_realtime_scheduling()
152 # print "Warning: failed to enable realtime scheduling"
154 fg.start() # start flow graph
156 # generate and send packets
157 nbytes = int(1e6 * options.megabytes)
160 pkt_size = int(options.size)
163 #r = ''.join([chr(random.randint(0,255)) for i in range(pkt_size-2)])
164 #pkt_contents = struct.pack('!H', pktno) + r
166 pkt_contents = struct.pack('!H', pktno) + (pkt_size - 2) * chr(pktno & 0xff)
168 send_pkt(pkt_contents)
170 #sys.stderr.write('.')
171 #if options.discontinuous and pktno % 5 == 4:
176 fg.wait() # wait for it to finish
179 if __name__ == '__main__':
182 except KeyboardInterrupt: