root / gnuradio-examples / python / digital / pick_bitrate.py @ 977b0e09
History | View | Annotate | Download (5.9 kB)
| 1 | #
|
|---|---|
| 2 | # Copyright 2005,2006 Free Software Foundation, Inc.
|
| 3 | #
|
| 4 | # This file is part of GNU Radio
|
| 5 | #
|
| 6 | # GNU Radio is free software; you can redistribute it and/or modify
|
| 7 | # it under the terms of the GNU General Public License as published by
|
| 8 | # the Free Software Foundation; either version 3, or (at your option)
|
| 9 | # any later version.
|
| 10 | #
|
| 11 | # GNU Radio is distributed in the hope that it will be useful,
|
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 14 | # GNU General Public License for more details.
|
| 15 | #
|
| 16 | # You should have received a copy of the GNU General Public License
|
| 17 | # along with GNU Radio; see the file COPYING. If not, write to
|
| 18 | # the Free Software Foundation, Inc., 51 Franklin Street,
|
| 19 | # Boston, MA 02110-1301, USA.
|
| 20 | #
|
| 21 | |
| 22 | from gnuradio import eng_notation |
| 23 | |
| 24 | _default_bitrate = 500e3
|
| 25 | |
| 26 | _valid_samples_per_symbol = (2,3,4,5,6,7) |
| 27 | |
| 28 | def _gen_tx_info(converter_rate, xrates): |
| 29 | results = [] |
| 30 | for samples_per_symbol in _valid_samples_per_symbol: |
| 31 | for interp in xrates: |
| 32 | bitrate = converter_rate / interp / samples_per_symbol |
| 33 | results.append((bitrate, samples_per_symbol, interp)) |
| 34 | results.sort() |
| 35 | return results
|
| 36 | |
| 37 | def _gen_rx_info(converter_rate, xrates): |
| 38 | results = [] |
| 39 | for samples_per_symbol in _valid_samples_per_symbol: |
| 40 | for decim in xrates: |
| 41 | bitrate = converter_rate / decim / samples_per_symbol |
| 42 | results.append((bitrate, samples_per_symbol, decim)) |
| 43 | results.sort() |
| 44 | return results
|
| 45 | |
| 46 | def _filter_info(info, samples_per_symbol, xrate): |
| 47 | if samples_per_symbol is not None: |
| 48 | info = [x for x in info if x[1] == samples_per_symbol] |
| 49 | if xrate is not None: |
| 50 | info = [x for x in info if x[2] == xrate] |
| 51 | return info
|
| 52 | |
| 53 | def _pick_best(target_bitrate, bits_per_symbol, info): |
| 54 | """
|
| 55 | @returns tuple (bitrate, samples_per_symbol, interp_rate_or_decim_rate)
|
| 56 | """ |
| 57 | if len(info) == 0: |
| 58 | raise RuntimeError, "info is zero length!" |
| 59 | |
| 60 | if target_bitrate is None: # return the fastest one |
| 61 | return info[-1] |
| 62 | |
| 63 | # convert bit rate to symbol rate
|
| 64 | target_symbolrate = target_bitrate / bits_per_symbol |
| 65 | |
| 66 | # Find the closest matching symbol rate.
|
| 67 | # In the event of a tie, the one with the lowest samples_per_symbol wins.
|
| 68 | # (We already sorted them, so the first one is the one we take)
|
| 69 | |
| 70 | best = info[0]
|
| 71 | best_delta = abs(target_symbolrate - best[0]) |
| 72 | for x in info[1:]: |
| 73 | delta = abs(target_symbolrate - x[0]) |
| 74 | if delta < best_delta:
|
| 75 | best_delta = delta |
| 76 | best = x |
| 77 | |
| 78 | # convert symbol rate back to bit rate
|
| 79 | return ((best[0] * bits_per_symbol),) + best[1:] |
| 80 | |
| 81 | def _pick_bitrate(bitrate, bits_per_symbol, samples_per_symbol, |
| 82 | xrate, converter_rate, xrates, gen_info): |
| 83 | """
|
| 84 | @returns tuple (bitrate, samples_per_symbol, interp_rate_or_decim_rate)
|
| 85 | """ |
| 86 | if not isinstance(bits_per_symbol, int) or bits_per_symbol < 1: |
| 87 | raise ValueError, "bits_per_symbol must be an int >= 1" |
| 88 | |
| 89 | if samples_per_symbol is not None and xrate is not None: # completely determined |
| 90 | return (float(converter_rate) / xrate / samples_per_symbol, |
| 91 | samples_per_symbol, xrate) |
| 92 | |
| 93 | if bitrate is None and samples_per_symbol is None and xrate is None: |
| 94 | bitrate = _default_bitrate |
| 95 | |
| 96 | # now we have a target bitrate and possibly an xrate or
|
| 97 | # samples_per_symbol constraint, but not both of them.
|
| 98 | |
| 99 | ret = _pick_best(bitrate, bits_per_symbol, |
| 100 | _filter_info(gen_info(converter_rate, xrates), samples_per_symbol, xrate)) |
| 101 | print "Actual Bitrate:", eng_notation.num_to_str(ret[0]) |
| 102 | return ret
|
| 103 | |
| 104 | # ---------------------------------------------------------------------------------------
|
| 105 | |
| 106 | def pick_tx_bitrate(bitrate, bits_per_symbol, samples_per_symbol, |
| 107 | interp_rate, converter_rate, possible_interps): |
| 108 | """
|
| 109 | Given the 4 input parameters, return at configuration that matches
|
| 110 | |
| 111 | @param bitrate: desired bitrate or None
|
| 112 | @type bitrate: number or None
|
| 113 | @param bits_per_symbol: E.g., BPSK -> 1, QPSK -> 2, 8-PSK -> 3
|
| 114 | @type bits_per_symbol: integer >= 1
|
| 115 | @param samples_per_symbol: samples/baud (aka samples/symbol)
|
| 116 | @type samples_per_symbol: number or None
|
| 117 | @param interp_rate: USRP interpolation factor
|
| 118 | @type interp_rate: integer or None
|
| 119 | @param converter_rate: converter sample rate in Hz
|
| 120 | @type converter_rate: number
|
| 121 | @param possible_interps: a list of possible rates
|
| 122 | @type possible_interps: a list of integers
|
| 123 | |
| 124 | @returns tuple (bitrate, samples_per_symbol, interp_rate)
|
| 125 | """ |
| 126 | print "Requested TX Bitrate:", bitrate and eng_notation.num_to_str(bitrate) or 'Auto', |
| 127 | return _pick_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
|
| 128 | interp_rate, converter_rate, possible_interps, _gen_tx_info) |
| 129 | |
| 130 | |
| 131 | def pick_rx_bitrate(bitrate, bits_per_symbol, samples_per_symbol, |
| 132 | decim_rate, converter_rate, possible_decims): |
| 133 | """
|
| 134 | Given the 4 input parameters, return at configuration that matches
|
| 135 | |
| 136 | @param bitrate: desired bitrate or None
|
| 137 | @type bitrate: number or None
|
| 138 | @param bits_per_symbol: E.g., BPSK -> 1, QPSK -> 2, 8-PSK -> 3
|
| 139 | @type bits_per_symbol: integer >= 1
|
| 140 | @param samples_per_symbol: samples/baud (aka samples/symbol)
|
| 141 | @type samples_per_symbol: number or None
|
| 142 | @param decim_rate: USRP decimation factor
|
| 143 | @type decim_rate: integer or None
|
| 144 | @param converter_rate: converter sample rate in Hz
|
| 145 | @type converter_rate: number
|
| 146 | @param possible_decims: a list of possible rates
|
| 147 | @type possible_decims: a list of integers
|
| 148 | |
| 149 | @returns tuple (bitrate, samples_per_symbol, decim_rate)
|
| 150 | """ |
| 151 | print "Requested RX Bitrate:", bitrate and eng_notation.num_to_str(bitrate) or 'Auto' |
| 152 | return _pick_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
|
| 153 | decim_rate, converter_rate, possible_decims, _gen_rx_info) |