switch source package format to 3.0 quilt
[debian/gnuradio] / gnuradio-examples / python / ofdm / benchmark_ofdm.py
index 3b4761d5ee65185e9f20d662fed8784bd4c8e18b..6d6ca9f2aeb43a5a652acdde79ec0d84232803c9 100755 (executable)
@@ -1,12 +1,12 @@
 #!/usr/bin/env python
 #
-# Copyright 2005, 2006 Free Software Foundation, Inc.
+# Copyright 2006, 2007, 2009 Free Software Foundation, Inc.
 # 
 # This file is part of GNU Radio
 # 
 # GNU Radio is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2, or (at your option)
+# the Free Software Foundation; either version 3, or (at your option)
 # any later version.
 # 
 # GNU Radio is distributed in the hope that it will be useful,
@@ -20,8 +20,7 @@
 # Boston, MA 02110-1301, USA.
 # 
 
-from gnuradio import gr, gru, modulation_utils
-from gnuradio import usrp
+from gnuradio import gr, blks2
 from gnuradio import eng_notation
 from gnuradio.eng_option import eng_option
 from optparse import OptionParser
@@ -31,79 +30,62 @@ import random, time, struct, sys, math, os
 # from current dir
 from transmit_path import transmit_path
 from receive_path import receive_path
-import ofdm
 
-class awgn_channel(gr.hier_block):
-    def __init__(self, fg, sample_rate, noise_voltage, frequency_offset):
 
-        self.input = gr.add_const_cc(0)
-
-        self.noise_adder = gr.add_cc()
-        self.noise = gr.noise_source_c(gr.GR_GAUSSIAN,noise_voltage)
-        self.offset = gr.sig_source_c(1, gr.GR_SIN_WAVE, frequency_offset, 1.0, 0.0)
-        self.mixer_offset = gr.multiply_cc()
-
-        fg.connect(self.input, (self.mixer_offset,0))
-        fg.connect(self.offset,(self.mixer_offset,1))
-        fg.connect(self.mixer_offset, (self.noise_adder,1))
-        fg.connect(self.noise, (self.noise_adder,0))
-
-        gr.hier_block.__init__(self, fg, self.input, self.noise_adder)
-
-class multipath_channel(gr.hier_block):
-    def __init__(self, fg):
-
-        self.taps = [1.0, .2, 0.0, .1, .08, -.4, .12, -.2, 0, 0, 0, .3]
-        self.chan = gr.fir_filter_ccc(1, self.taps)
-        
-        gr.hier_block.__init__(self, fg, self.chan, self.chan)
-
-class my_graph(gr.flow_graph):
+class my_top_block(gr.top_block):
     def __init__(self, callback, options):
-        gr.flow_graph.__init__(self)
+        gr.top_block.__init__(self)
 
-        channel_on = True
+        if not options.channel_off:
+            SNR = 10.0**(options.snr/10.0)
+            power_in_signal = abs(options.tx_amplitude)**2.0
+            noise_power_in_channel = power_in_signal/SNR
+            noise_voltage = math.sqrt(noise_power_in_channel/2.0)
+            print "Noise voltage: ", noise_voltage
 
-        SNR = 10.0**(options.snr/10.0)
-        frequency_offset = options.frequency_offset / options.fft_length
-        
-        power_in_signal = options.occupied_tones
-        noise_power_in_channel = power_in_signal/SNR
-        noise_power_required = noise_power_in_channel * options.fft_length / options.occupied_tones
-        noise_voltage = math.sqrt(noise_power_required)
-
-        self.txpath = transmit_path(self, options)
-        self.throttle = gr.throttle(gr.sizeof_gr_complex, options.sample_rate)
-        self.rxpath = receive_path(self, callback, options)
-
-        if channel_on:
-            self.channel = awgn_channel(self, options.sample_rate, noise_voltage, frequency_offset)
-            self.multipath = multipath_channel(self)
-
-            if options.discontinuous:
-                z = 20000*[0,]
-                self.zeros = gr.vector_source_c(z, True)
-                packet_size = 15*((4+8+4+1500+4) * 8)
-                self.mux = gr.stream_mux(gr.sizeof_gr_complex, [packet_size-0, int(10e5)])
-
-                # Connect components
-                self.connect(self.txpath, (self.mux,0))
-                self.connect(self.zeros, (self.mux,1))
-                self.connect(self.mux, self.throttle, self.channel, self.rxpath)
-                self.connect(self.mux, gr.file_sink(gr.sizeof_gr_complex, "tx_ofdm.dat"))
+            frequency_offset = options.frequency_offset / options.fft_length
+            print "Frequency offset: ", frequency_offset
 
+            if options.multipath_on:
+                taps = [1.0, .2, 0.0, .1, .08, -.4, .12, -.2, 0, 0, 0, .3]
             else:
-                #self.connect(self.txpath, self.throttle, self.multipath, self.channel)
-                self.connect(self.txpath, self.throttle, self.channel)
-                self.connect(self.channel, self.rxpath)
-                self.connect(self.txpath, gr.file_sink(gr.sizeof_gr_complex, "tx_ofdm.dat"))
-            
+                taps = [1.0, 0.0]
+
         else:
-            self.connect(self.txpath, self.throttle, self.rxpath)
-            self.connect(self.txpath, gr.file_sink(gr.sizeof_gr_complex, "tx"))
-            self.connect(self.rxpath.ofdm_demod.ofdm_rx, gr.file_sink(options.fft_length*gr.sizeof_gr_complex, "rx"))
+            noise_voltage = 0.0
+            frequency_offset = 0.0
+            taps = [1.0, 0.0]
+
+        symbols_per_packet = math.ceil(((4+options.size+4) * 8) / options.occupied_tones)
+        samples_per_packet = (symbols_per_packet+2) * (options.fft_length+options.cp_length)
+        print "Symbols per Packet: ", symbols_per_packet
+        print "Samples per Packet: ", samples_per_packet
+        if options.discontinuous:
+            stream_size = [100000, int(options.discontinuous*samples_per_packet)]
+        else:
+            stream_size = [0, 100000]
 
+        z = [0,]
+        self.zeros = gr.vector_source_c(z, True)
+        self.txpath = transmit_path(options)
 
+        #self.mux = gr.stream_mux(gr.sizeof_gr_complex, stream_size)
+        self.throttle = gr.throttle(gr.sizeof_gr_complex, options.sample_rate)
+        self.channel = gr.channel_model(noise_voltage, frequency_offset,
+                                        options.clockrate_ratio, taps)
+        self.rxpath = receive_path(callback, options)
+                
+        #self.connect(self.zeros, (self.mux,0))
+        #self.connect(self.txpath, (self.mux,1))
+        #self.connect(self.mux, self.throttle, self.channel, self.rxpath)
+        #self.connect(self.mux, self.throttle, self.rxpath)
+        self.connect(self.txpath, self.throttle, self.channel, self.rxpath)
+        
+        if options.log:
+            self.connect(self.txpath, gr.file_sink(gr.sizeof_gr_complex, "txpath.dat"))
+            #self.connect(self.mux, gr.file_sink(gr.sizeof_gr_complex, "mux.dat"))
+            #self.connect(self.channel, gr.file_sink(gr.sizeof_gr_complex, "channel.dat"))
+            
 # /////////////////////////////////////////////////////////////////////////////
 #                                   main
 # /////////////////////////////////////////////////////////////////////////////
@@ -115,7 +97,7 @@ def main():
     n_right = 0
         
     def send_pkt(payload='', eof=False):
-        return fg.txpath.send_pkt(payload, eof)
+        return tb.txpath.send_pkt(payload, eof)
         
     def rx_callback(ok, payload):
         global n_rcvd, n_right
@@ -124,42 +106,54 @@ def main():
         if ok:
             n_right += 1
         print "ok: %r \t pktno: %d \t n_rcvd: %d \t n_right: %d" % (ok, pktno, n_rcvd, n_right)
+
+        printlst = list()
+        for x in payload[2:]:
+            t = hex(ord(x)).replace('0x', '')
+            if(len(t) == 1):
+                t = '0' + t
+            printlst.append(t)
+        printable = ''.join(printlst)
+
+        print printable
+        print "\n"
                 
     parser = OptionParser(option_class=eng_option, conflict_handler="resolve")
     expert_grp = parser.add_option_group("Expert")
-    parser.add_option("-s", "--size", type="eng_float", default=1450,
+    parser.add_option("-s", "--size", type="eng_float", default=400,
                       help="set packet size [default=%default]")
     parser.add_option("-M", "--megabytes", type="eng_float", default=1.0,
                       help="set megabytes to transmit [default=%default]")
     parser.add_option("-r", "--sample-rate", type="eng_float", default=1e5,
-                      help="set sample rate to RATE (%default)") 
+                      help="limit sample rate to RATE in throttle (%default)") 
     parser.add_option("", "--snr", type="eng_float", default=30,
                       help="set the SNR of the channel in dB [default=%default]")
     parser.add_option("", "--frequency-offset", type="eng_float", default=0,
                       help="set frequency offset introduced by channel [default=%default]")
-    parser.add_option("","--discontinuous", action="store_true", default=False,
-                      help="enable discontinous transmission (bursts of 5 packets)")
+    parser.add_option("", "--clockrate-ratio", type="eng_float", default=1.0,
+                      help="set clock rate ratio (sample rate difference) between two systems [default=%default]")
+    parser.add_option("","--discontinuous", type="int", default=0,
+                      help="enable discontinous transmission, burst of N packets [Default is continuous]")
+    parser.add_option("","--channel-off", action="store_true", default=False,
+                      help="Turns AWGN, freq offset channel off")
+    parser.add_option("","--multipath-on", action="store_true", default=False,
+                      help="enable multipath")
 
     transmit_path.add_options(parser, expert_grp)
     receive_path.add_options(parser, expert_grp)
-    ofdm.ofdm_mod.add_options(parser, expert_grp)
-    ofdm.ofdm_demod.add_options(parser, expert_grp)
+    blks2.ofdm_mod.add_options(parser, expert_grp)
+    blks2.ofdm_demod.add_options(parser, expert_grp)
     
     (options, args) = parser.parse_args ()
-
-    if(options.mtu < options.size):
-        sys.stderr.write("MTU (%.0f) must be larger than the packet size (%.0f)\n"
-                         % (options.mtu, options.size))
-        sys.exit(1)
-        
+       
     # build the graph
-    fg = my_graph(rx_callback, options)
+    tb = my_top_block(rx_callback, options)
     
     r = gr.enable_realtime_scheduling()
     #    if r != gr.RT_OK:
     #        print "Warning: failed to enable realtime scheduling"
-        
-    fg.start()                       # start flow graph
+    
+    tb.start()                       # start flow graph
     
     # generate and send packets
     nbytes = int(1e6 * options.megabytes)
@@ -167,12 +161,12 @@ def main():
     pktno = 0
     pkt_size = int(options.size)
 
-    
-    
     while n < nbytes:
-        r = ''.join([chr(random.randint(0,255)) for i in range(pkt_size-2)])
-        #pkt_contents = struct.pack('!H', pktno) + (pkt_size - 2) * chr(pktno & 0xff)
-        pkt_contents = struct.pack('!H', pktno) + r
+        #r = ''.join([chr(random.randint(0,255)) for i in range(pkt_size-2)])
+        #pkt_contents = struct.pack('!H', pktno) + r
+
+        pkt_contents = struct.pack('!H', pktno) + (pkt_size - 2) * chr(pktno & 0xff)
+
         send_pkt(pkt_contents)
         n += pkt_size
         #sys.stderr.write('.')
@@ -181,7 +175,7 @@ def main():
         pktno += 1
         
     send_pkt(eof=True)
-    fg.wait()                       # wait for it to finish
+    tb.wait()                       # wait for it to finish
 
 
 if __name__ == '__main__':