~ubuntu-branches/ubuntu/precise/gnuradio/precise

« back to all changes in this revision

Viewing changes to gnuradio-examples/python/ofdm/benchmark_ofdm.py

  • Committer: Bazaar Package Importer
  • Author(s): Kamal Mostafa
  • Date: 2010-03-13 07:46:01 UTC
  • mfrom: (2.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100313074601-zjsa893a87bozyh7
Tags: 3.2.2.dfsg-1ubuntu1
* Fix build for Ubuntu lucid (LP: #260406)
  - add binary package dep for libusrp0, libusrp2-0: adduser
  - debian/rules clean: remove pre-built Qt moc files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#
 
3
# Copyright 2006, 2007, 2009 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 3, 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, blks2
 
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_top_block(gr.top_block):
 
36
    def __init__(self, callback, options):
 
37
        gr.top_block.__init__(self)
 
38
 
 
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
 
45
 
 
46
            frequency_offset = options.frequency_offset / options.fft_length
 
47
            print "Frequency offset: ", frequency_offset
 
48
 
 
49
            if options.multipath_on:
 
50
                taps = [1.0, .2, 0.0, .1, .08, -.4, .12, -.2, 0, 0, 0, .3]
 
51
            else:
 
52
                taps = [1.0, 0.0]
 
53
 
 
54
        else:
 
55
            noise_voltage = 0.0
 
56
            frequency_offset = 0.0
 
57
            taps = [1.0, 0.0]
 
58
 
 
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)]
 
65
        else:
 
66
            stream_size = [0, 100000]
 
67
 
 
68
        z = [0,]
 
69
        self.zeros = gr.vector_source_c(z, True)
 
70
        self.txpath = transmit_path(options)
 
71
 
 
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 = gr.channel_model(noise_voltage, frequency_offset,
 
75
                                        options.clockrate_ratio, taps)
 
76
        self.rxpath = receive_path(callback, options)
 
77
                
 
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)
 
83
        
 
84
        if options.log:
 
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"))
 
88
            
 
89
# /////////////////////////////////////////////////////////////////////////////
 
90
#                                   main
 
91
# /////////////////////////////////////////////////////////////////////////////
 
92
 
 
93
def main():
 
94
    global n_rcvd, n_right
 
95
        
 
96
    n_rcvd = 0
 
97
    n_right = 0
 
98
        
 
99
    def send_pkt(payload='', eof=False):
 
100
        return tb.txpath.send_pkt(payload, eof)
 
101
        
 
102
    def rx_callback(ok, payload):
 
103
        global n_rcvd, n_right
 
104
        n_rcvd += 1
 
105
        (pktno,) = struct.unpack('!H', payload[0:2])
 
106
        if ok:
 
107
            n_right += 1
 
108
        print "ok: %r \t pktno: %d \t n_rcvd: %d \t n_right: %d" % (ok, pktno, n_rcvd, n_right)
 
109
 
 
110
        printlst = list()
 
111
        for x in payload[2:]:
 
112
            t = hex(ord(x)).replace('0x', '')
 
113
            if(len(t) == 1):
 
114
                t = '0' + t
 
115
            printlst.append(t)
 
116
        printable = ''.join(printlst)
 
117
 
 
118
        print printable
 
119
        print "\n"
 
120
                
 
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")
 
141
 
 
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)
 
146
    
 
147
    (options, args) = parser.parse_args ()
 
148
       
 
149
    # build the graph
 
150
    tb = my_top_block(rx_callback, options)
 
151
    
 
152
    r = gr.enable_realtime_scheduling()
 
153
    #    if r != gr.RT_OK:
 
154
    #        print "Warning: failed to enable realtime scheduling"
 
155
    
 
156
    tb.start()                       # start flow graph
 
157
    
 
158
    # generate and send packets
 
159
    nbytes = int(1e6 * options.megabytes)
 
160
    n = 0
 
161
    pktno = 0
 
162
    pkt_size = int(options.size)
 
163
 
 
164
    while n < nbytes:
 
165
        #r = ''.join([chr(random.randint(0,255)) for i in range(pkt_size-2)])
 
166
        #pkt_contents = struct.pack('!H', pktno) + r
 
167
 
 
168
        pkt_contents = struct.pack('!H', pktno) + (pkt_size - 2) * chr(pktno & 0xff)
 
169
 
 
170
        send_pkt(pkt_contents)
 
171
        n += pkt_size
 
172
        #sys.stderr.write('.')
 
173
        #if options.discontinuous and pktno % 5 == 4:
 
174
        #    time.sleep(1)
 
175
        pktno += 1
 
176
        
 
177
    send_pkt(eof=True)
 
178
    tb.wait()                       # wait for it to finish
 
179
 
 
180
 
 
181
if __name__ == '__main__':
 
182
    try:
 
183
        main()
 
184
    except KeyboardInterrupt:
 
185
        pass
 
186
 
 
187