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 print "Requested bitrate out of bounds"
74 samples_per_symbol = converter_rate / bits_per_symbol / rate / bitrate
75 bitrate = converter_rate / bits_per_symbol / xrate / samples_per_symbol
77 # If bitrate and xrate are specified
78 if(samples_per_symbol is None):
79 samples_per_symbol = converter_rate / xrate / bits_per_symbol / bitrate
81 # If bitrate and SPS are specified
83 xrate = converter_rate / samples_per_symbol / bits_per_symbol / bitrate
84 if((xrate in xrates) == False):
85 # Find the closest avaiable rate larger than the calculated one
91 if(xrate > max(xrates)):
94 bitrate = converter_rate / bits_per_symbol / xrate / samples_per_symbol
95 print "Could not find suitable rate for specified SPS and Bitrate"
96 print "Using rate = %d for bitrate of %sbps" % \
97 (xrate, (eng_notation.num_to_str(bitrate)))
99 if((xrate in xrates) == False):
100 raise ValueError(("Invalid rate (rate = %d)" % xrate))
101 if((samples_per_symbol < _sps_min) or (samples_per_symbol > _sps_max)):
102 raise ValueError(("Invalid samples per symbol (sps = %.2f). Must be in [%.0f, %.0f]." \
103 % (xrate, _sps_min, _sps_max)))
105 return (bitrate, samples_per_symbol, int(xrate))
108 def pick_tx_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
109 interp_rate, converter_rate, possible_interps):
111 Given the 4 input parameters, return at configuration that matches
113 @param bitrate: desired bitrate or None
114 @type bitrate: number or None
115 @param bits_per_symbol: E.g., BPSK -> 1, QPSK -> 2, 8-PSK -> 3
116 @type bits_per_symbol: integer >= 1
117 @param samples_per_symbol: samples/baud (aka samples/symbol)
118 @type samples_per_symbol: number or None
119 @param interp_rate: USRP interpolation factor
120 @type interp_rate: integer or None
121 @param converter_rate: converter sample rate in Hz
122 @type converter_rate: number
123 @param possible_interps: a list of possible rates
124 @type possible_interps: a list of integers
126 @returns tuple (bitrate, samples_per_symbol, interp_rate)
129 return _pick_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
130 interp_rate, converter_rate, possible_interps)
133 def pick_rx_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
134 decim_rate, converter_rate, possible_decims):
136 Given the 4 input parameters, return at configuration that matches
138 @param bitrate: desired bitrate or None
139 @type bitrate: number or None
140 @param bits_per_symbol: E.g., BPSK -> 1, QPSK -> 2, 8-PSK -> 3
141 @type bits_per_symbol: integer >= 1
142 @param samples_per_symbol: samples/baud (aka samples/symbol)
143 @type samples_per_symbol: number or None
144 @param decim_rate: USRP decimation factor
145 @type decim_rate: integer or None
146 @param converter_rate: converter sample rate in Hz
147 @type converter_rate: number
148 @param possible_decims: a list of possible rates
149 @type possible_decims: a list of integers
151 @returns tuple (bitrate, samples_per_symbol, decim_rate)
154 return _pick_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
155 decim_rate, converter_rate, possible_decims)