root / gr-digital / examples / ofdm / benchmark_add_channel.py @ 1033576a
History | View | Annotate | Download (4.4 kB)
| 1 | 1033576a | Tom Rondeau | #!/usr/bin/env python
|
|---|---|---|---|
| 2 | 1033576a | Tom Rondeau | #
|
| 3 | 1033576a | Tom Rondeau | # Copyright 2010,2011 Free Software Foundation, Inc.
|
| 4 | 1033576a | Tom Rondeau | #
|
| 5 | 1033576a | Tom Rondeau | # This file is part of GNU Radio
|
| 6 | 1033576a | Tom Rondeau | #
|
| 7 | 1033576a | Tom Rondeau | # GNU Radio is free software; you can redistribute it and/or modify
|
| 8 | 1033576a | Tom Rondeau | # it under the terms of the GNU General Public License as published by
|
| 9 | 1033576a | Tom Rondeau | # the Free Software Foundation; either version 3, or (at your option)
|
| 10 | 1033576a | Tom Rondeau | # any later version.
|
| 11 | 1033576a | Tom Rondeau | #
|
| 12 | 1033576a | Tom Rondeau | # GNU Radio is distributed in the hope that it will be useful,
|
| 13 | 1033576a | Tom Rondeau | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 14 | 1033576a | Tom Rondeau | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 15 | 1033576a | Tom Rondeau | # GNU General Public License for more details.
|
| 16 | 1033576a | Tom Rondeau | #
|
| 17 | 1033576a | Tom Rondeau | # You should have received a copy of the GNU General Public License
|
| 18 | 1033576a | Tom Rondeau | # along with GNU Radio; see the file COPYING. If not, write to
|
| 19 | 1033576a | Tom Rondeau | # the Free Software Foundation, Inc., 51 Franklin Street,
|
| 20 | 1033576a | Tom Rondeau | # Boston, MA 02110-1301, USA.
|
| 21 | 1033576a | Tom Rondeau | #
|
| 22 | 1033576a | Tom Rondeau | |
| 23 | 1033576a | Tom Rondeau | from gnuradio import gr |
| 24 | 1033576a | Tom Rondeau | from gnuradio import eng_notation |
| 25 | 1033576a | Tom Rondeau | from gnuradio.eng_option import eng_option |
| 26 | 1033576a | Tom Rondeau | from optparse import OptionParser |
| 27 | 1033576a | Tom Rondeau | |
| 28 | 1033576a | Tom Rondeau | import random, math, sys |
| 29 | 1033576a | Tom Rondeau | |
| 30 | 1033576a | Tom Rondeau | class my_top_block(gr.top_block): |
| 31 | 1033576a | Tom Rondeau | def __init__(self, ifile, ofile, options): |
| 32 | 1033576a | Tom Rondeau | gr.top_block.__init__(self)
|
| 33 | 1033576a | Tom Rondeau | |
| 34 | 1033576a | Tom Rondeau | SNR = 10.0**(options.snr/10.0) |
| 35 | 1033576a | Tom Rondeau | time_offset = options.time_offset |
| 36 | 1033576a | Tom Rondeau | phase_offset = options.phase_offset*(math.pi/180.0)
|
| 37 | 1033576a | Tom Rondeau | |
| 38 | 1033576a | Tom Rondeau | # calculate noise voltage from SNR
|
| 39 | 1033576a | Tom Rondeau | power_in_signal = abs(options.tx_amplitude)**2 |
| 40 | 1033576a | Tom Rondeau | noise_power = power_in_signal/SNR |
| 41 | 1033576a | Tom Rondeau | noise_voltage = math.sqrt(noise_power) |
| 42 | 1033576a | Tom Rondeau | print "Noise voltage: ", noise_voltage |
| 43 | 1033576a | Tom Rondeau | |
| 44 | 1033576a | Tom Rondeau | frequency_offset = options.frequency_offset / options.fft_length |
| 45 | 1033576a | Tom Rondeau | |
| 46 | 1033576a | Tom Rondeau | self.src = gr.file_source(gr.sizeof_gr_complex, ifile)
|
| 47 | 1033576a | Tom Rondeau | #self.throttle = gr.throttle(gr.sizeof_gr_complex, options.sample_rate)
|
| 48 | 1033576a | Tom Rondeau | self.channel = gr.channel_model(noise_voltage, frequency_offset,
|
| 49 | 1033576a | Tom Rondeau | time_offset, noise_seed=random.randint(0,100000)) |
| 50 | 1033576a | Tom Rondeau | self.phase = gr.multiply_const_cc(complex(math.cos(phase_offset), |
| 51 | 1033576a | Tom Rondeau | math.sin(phase_offset))) |
| 52 | 1033576a | Tom Rondeau | self.snk = gr.file_sink(gr.sizeof_gr_complex, ofile)
|
| 53 | 1033576a | Tom Rondeau | |
| 54 | 1033576a | Tom Rondeau | self.connect(self.src, self.channel, self.phase, self.snk) |
| 55 | 1033576a | Tom Rondeau | |
| 56 | 1033576a | Tom Rondeau | |
| 57 | 1033576a | Tom Rondeau | # /////////////////////////////////////////////////////////////////////////////
|
| 58 | 1033576a | Tom Rondeau | # main
|
| 59 | 1033576a | Tom Rondeau | # /////////////////////////////////////////////////////////////////////////////
|
| 60 | 1033576a | Tom Rondeau | |
| 61 | 1033576a | Tom Rondeau | def main(): |
| 62 | 1033576a | Tom Rondeau | # Create Options Parser:
|
| 63 | 1033576a | Tom Rondeau | usage = "benchmack_add_channel.py [options] <input file> <output file>"
|
| 64 | 1033576a | Tom Rondeau | parser = OptionParser (usage=usage, option_class=eng_option, conflict_handler="resolve")
|
| 65 | 1033576a | Tom Rondeau | parser.add_option("-n", "--snr", type="eng_float", default=30, |
| 66 | 1033576a | Tom Rondeau | help="set the SNR of the channel in dB [default=%default]")
|
| 67 | 1033576a | Tom Rondeau | parser.add_option("", "--seed", action="store_true", default=False, |
| 68 | 1033576a | Tom Rondeau | help="use a random seed for AWGN noise [default=%default]")
|
| 69 | 1033576a | Tom Rondeau | parser.add_option("-f", "--frequency-offset", type="eng_float", default=0, |
| 70 | 1033576a | Tom Rondeau | help="set frequency offset introduced by channel [default=%default]")
|
| 71 | 1033576a | Tom Rondeau | parser.add_option("-t", "--time-offset", type="eng_float", default=1.0, |
| 72 | 1033576a | Tom Rondeau | help="set timing offset between Tx and Rx [default=%default]")
|
| 73 | 1033576a | Tom Rondeau | parser.add_option("-p", "--phase-offset", type="eng_float", default=0, |
| 74 | 1033576a | Tom Rondeau | help="set phase offset (in degrees) between Tx and Rx [default=%default]")
|
| 75 | 1033576a | Tom Rondeau | parser.add_option("-m", "--use-multipath", action="store_true", default=False, |
| 76 | 1033576a | Tom Rondeau | help="Use a multipath channel [default=%default]")
|
| 77 | 1033576a | Tom Rondeau | parser.add_option("", "--fft-length", type="intx", default=None, |
| 78 | 1033576a | Tom Rondeau | help="set the number of FFT bins [default=%default]")
|
| 79 | 1033576a | Tom Rondeau | parser.add_option("", "--tx-amplitude", type="eng_float", |
| 80 | 1033576a | Tom Rondeau | default=1.0,
|
| 81 | 1033576a | Tom Rondeau | help="tell the simulator the signal amplitude [default=%default]")
|
| 82 | 1033576a | Tom Rondeau | |
| 83 | 1033576a | Tom Rondeau | (options, args) = parser.parse_args () |
| 84 | 1033576a | Tom Rondeau | |
| 85 | 1033576a | Tom Rondeau | if len(args) != 2: |
| 86 | 1033576a | Tom Rondeau | parser.print_help(sys.stderr) |
| 87 | 1033576a | Tom Rondeau | sys.exit(1)
|
| 88 | 1033576a | Tom Rondeau | |
| 89 | 1033576a | Tom Rondeau | if options.fft_length is None: |
| 90 | 1033576a | Tom Rondeau | sys.stderr.write("Please enter the FFT length of the OFDM signal.\n")
|
| 91 | 1033576a | Tom Rondeau | sys.exit(1)
|
| 92 | 1033576a | Tom Rondeau | |
| 93 | 1033576a | Tom Rondeau | ifile = args[0]
|
| 94 | 1033576a | Tom Rondeau | ofile = args[1]
|
| 95 | 1033576a | Tom Rondeau | |
| 96 | 1033576a | Tom Rondeau | # build the graph
|
| 97 | 1033576a | Tom Rondeau | tb = my_top_block(ifile, ofile, options) |
| 98 | 1033576a | Tom Rondeau | |
| 99 | 1033576a | Tom Rondeau | r = gr.enable_realtime_scheduling() |
| 100 | 1033576a | Tom Rondeau | if r != gr.RT_OK:
|
| 101 | 1033576a | Tom Rondeau | print "Warning: Failed to enable realtime scheduling." |
| 102 | 1033576a | Tom Rondeau | |
| 103 | 1033576a | Tom Rondeau | tb.start() # start flow graph
|
| 104 | 1033576a | Tom Rondeau | tb.wait() # wait for it to finish
|
| 105 | 1033576a | Tom Rondeau | |
| 106 | 1033576a | Tom Rondeau | if __name__ == '__main__': |
| 107 | 1033576a | Tom Rondeau | try:
|
| 108 | 1033576a | Tom Rondeau | main() |
| 109 | 1033576a | Tom Rondeau | except KeyboardInterrupt: |
| 110 | 1033576a | Tom Rondeau | pass |