2 # Copyright 2010 Free Software Foundation, Inc.
4 # This file is part of GNU Radio
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)
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.
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.
22 from gnuradio import eng_notation
24 _default_bitrate = 500e3
28 def _pick_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
29 xrate, converter_rate, xrates):
31 @returns tuple (bitrate, samples_per_symbol, interp_rate_or_decim_rate)
34 if not isinstance(bits_per_symbol, int) or bits_per_symbol < 1:
35 raise ValueError, "bits_per_symbol must be an int >= 1"
37 converter_rate = float(converter_rate)
38 bits_per_symbol = float(bits_per_symbol)
40 # completely determined; if bitrate is specified, this overwrites it
41 if (samples_per_symbol is not None) and (xrate is not None):
42 bitrate = converter_rate / bits_per_symbol / xrate / samples_per_symbol
44 # If only SPS is given
45 if (bitrate is None) and (samples_per_symbol is not None) and (xrate is None):
47 bitrate = converter_rate / bits_per_symbol / xrate / samples_per_symbol
49 # If only xrate is given, just set SPS to 2 and calculate bitrate
50 if (bitrate is None) and (samples_per_symbol is None) and (xrate is not None):
51 samples_per_symbol = 2.0
52 bitrate = converter_rate / bits_per_symbol / xrate / samples_per_symbol
54 # If no parameters are give, use the default bit rate
55 if (bitrate is None) and (samples_per_symbol is None) and (xrate is None):
56 bitrate = _default_bitrate
58 # If only bitrate is specified, return max xrate and appropriate
59 # samples per symbol (minimum of 2.0) to reach bit rate
60 if (samples_per_symbol is None) and (xrate is None):
62 for i in xrange(len(xrates)):
63 if((converter_rate / bits_per_symbol / xrates[i]) >= 2*bitrate):
70 except UnboundLocalError:
71 raise ValueError("Requested bitrate out of bounds")
73 samples_per_symbol = converter_rate / bits_per_symbol / rate / bitrate
74 bitrate = converter_rate / bits_per_symbol / xrate / samples_per_symbol
76 # If bitrate and xrate are specified
77 if(samples_per_symbol is None):
78 samples_per_symbol = converter_rate / xrate / bits_per_symbol / bitrate
80 # If bitrate and SPS are specified
82 xrate = converter_rate / samples_per_symbol / bits_per_symbol / bitrate
83 if((xrate in xrates) == False):
84 # Find the closest avaiable rate larger than the calculated one
90 if(xrate > max(xrates)):
93 bitrate = converter_rate / bits_per_symbol / xrate / samples_per_symbol
94 print "Could not find suitable rate for specified SPS and Bitrate"
95 print "Using rate = %d for bitrate of %sbps" % \
96 (xrate, (eng_notation.num_to_str(bitrate)))
98 if((xrate in xrates) == False):
99 raise ValueError(("Invalid rate (rate = %d)" % xrate))
100 if((samples_per_symbol < _sps_min) or (samples_per_symbol > _sps_max)):
101 raise ValueError(("Invalid samples per symbol (sps = %.2f). Must be in [%.0f, %.0f]." \
102 % (samples_per_symbol, _sps_min, _sps_max)))
104 return (bitrate, samples_per_symbol, int(xrate))
107 def pick_tx_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
108 interp_rate, converter_rate, possible_interps):
110 Given the 4 input parameters, return at configuration that matches
112 @param bitrate: desired bitrate or None
113 @type bitrate: number or None
114 @param bits_per_symbol: E.g., BPSK -> 1, QPSK -> 2, 8-PSK -> 3
115 @type bits_per_symbol: integer >= 1
116 @param samples_per_symbol: samples/baud (aka samples/symbol)
117 @type samples_per_symbol: number or None
118 @param interp_rate: USRP interpolation factor
119 @type interp_rate: integer or None
120 @param converter_rate: converter sample rate in Hz
121 @type converter_rate: number
122 @param possible_interps: a list of possible rates
123 @type possible_interps: a list of integers
125 @returns tuple (bitrate, samples_per_symbol, interp_rate)
128 return _pick_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
129 interp_rate, converter_rate, possible_interps)
132 def pick_rx_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
133 decim_rate, converter_rate, possible_decims):
135 Given the 4 input parameters, return at configuration that matches
137 @param bitrate: desired bitrate or None
138 @type bitrate: number or None
139 @param bits_per_symbol: E.g., BPSK -> 1, QPSK -> 2, 8-PSK -> 3
140 @type bits_per_symbol: integer >= 1
141 @param samples_per_symbol: samples/baud (aka samples/symbol)
142 @type samples_per_symbol: number or None
143 @param decim_rate: USRP decimation factor
144 @type decim_rate: integer or None
145 @param converter_rate: converter sample rate in Hz
146 @type converter_rate: number
147 @param possible_decims: a list of possible rates
148 @type possible_decims: a list of integers
150 @returns tuple (bitrate, samples_per_symbol, decim_rate)
153 return _pick_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
154 decim_rate, converter_rate, possible_decims)