Fixing pick_bitrate2 for transmit side with all cases tested.
[debian/gnuradio] / gnuradio-examples / python / digital / pick_bitrate2.py
index c0188dc3ce399f448750fe9aa0b4e12632179e12..23081117c9ee8809f54819d570b3eb4be88cd7e9 100644 (file)
+#
+# Copyright 2010 Free Software Foundation, Inc.
+# 
+# This file is part of GNU Radio
+# 
+# GNU Radio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+# 
+# GNU Radio is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with GNU Radio; see the file COPYING.  If not, write to
+# the Free Software Foundation, Inc., 51 Franklin Street,
+# Boston, MA 02110-1301, USA.
+# 
+
 from gnuradio import eng_notation
 
-def pick_rx_bitrate(bitrate, bits_per_symbol,
-                    converter_rate, possible_decims):
+_default_bitrate = 500e3
+_sps_min = 2
+_sps_max = 100
+
+def _pick_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
+                  xrate, converter_rate, xrates):
+    """
+    @returns tuple (bitrate, samples_per_symbol, interp_rate_or_decim_rate)
     """
-    Given the 4 input parameters, return at configuration that matches
 
-    @param bitrate: desired bitrate or None
-    @type bitrate: number or None
-    @param bits_per_symbol: E.g., BPSK -> 1, QPSK -> 2, 8-PSK -> 3
-    @type bits_per_symbol: integer >= 1
-    @param converter_rate: converter sample rate in Hz
-    @type converter_rate: number
-    @param possible_decims: a list of possible rates
-    @type possible_decims: a list of integers
+    if not isinstance(bits_per_symbol, int) or bits_per_symbol < 1:
+        raise ValueError, "bits_per_symbol must be an int >= 1"
 
-    @returns tuple (bitrate, samples_per_symbol, decim_rate)
-    """
+    converter_rate = float(converter_rate)
+    bits_per_symbol = float(bits_per_symbol)
+
+    # completely determined; if bitrate is specified, this overwrites it
+    if (samples_per_symbol is not None) and (xrate is not None):
+        bitrate = converter_rate / bits_per_symbol / xrate / samples_per_symbol
+
+    # If only SPS is given
+    if (bitrate is None) and (samples_per_symbol is not None) and (xrate is None):
+        xrate = max(xrates)
+        bitrate = converter_rate / bits_per_symbol / xrate / samples_per_symbol
+        
+    # If only xrate is given, just set SPS to 2 and calculate bitrate
+    if (bitrate is None) and (samples_per_symbol is None) and (xrate is not None):
+        samples_per_symbol = 2.0
+        bitrate = converter_rate / bits_per_symbol / xrate / samples_per_symbol
 
-    rates = list(possible_decims)
-    rates.sort()
+    # If no parameters are give, use the default bit rate
+    if (bitrate is None) and (samples_per_symbol is None) and (xrate is None):
+        bitrate = _default_bitrate
 
-    for i in xrange(len(rates)):
-        if((converter_rate / float(bits_per_symbol) / rates[i]) >= 2*bitrate):
-            decim = rates[i]
-        else:
-            break
+    # If only bitrate is specified, return max xrate and appropriate
+    # samples per symbol (minimum of 2.0) to reach bit rate
+    if (samples_per_symbol is None) and (xrate is None):
+        xrates.sort()
+        for i in xrange(len(xrates)):
+            if((converter_rate / bits_per_symbol / xrates[i]) >= 2*bitrate):
+                rate = xrates[i]
+            else:
+                break
 
-    sps = converter_rate / float(bits_per_symbol) / decim / bitrate
-    br = converter_rate / float(bits_per_symbol) / decim / sps
+        try:
+            xrate = rate
+        except UnboundLocalError:
+            print "Requested bitrate out of bounds"
+            sys.exit(1)
+            
+        samples_per_symbol = converter_rate / bits_per_symbol / rate / bitrate
+        bitrate = converter_rate / bits_per_symbol / xrate / samples_per_symbol
 
-    return (br, sps, int(decim))
+    # If bitrate and xrate are specified
+    if(samples_per_symbol is None):
+        samples_per_symbol = converter_rate / xrate / bits_per_symbol / bitrate
 
+    # If bitrate and SPS are specified
+    if(xrate is None):
+        xrate = converter_rate / samples_per_symbol / bits_per_symbol / bitrate
+        if((xrate in xrates) == False):
+            # Find the closest avaiable rate larger than the calculated one
+            xrates.sort()
+            for x in xrates:
+                if(x > xrate):
+                    xrate = x
+                    break
+            if(xrate > max(xrates)):
+                xrate = max(xrates)
+            
+            bitrate = converter_rate / bits_per_symbol / xrate / samples_per_symbol
+            print "Could not find suitable rate for specified SPS and Bitrate"
+            print "Using rate = %d for bitrate of %sbps" % \
+                  (xrate, (eng_notation.num_to_str(bitrate)))
 
-def pick_tx_bitrate(bitrate, bits_per_symbol,
-                    converter_rate, possible_interps):
+    if((xrate in xrates) == False):
+        raise ValueError(("Invalid rate (rate = %d)" % xrate))
+    if((samples_per_symbol < _sps_min) or (samples_per_symbol > _sps_max)):
+        raise ValueError(("Invalid samples per symbol (sps = %.2f). Must be in [%.0f, %.0f]." \
+                          % (xrate, _sps_min, _sps_max)))
+        
+    return (bitrate, samples_per_symbol, int(xrate))
+
+
+def pick_tx_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
+                    interp_rate, converter_rate, possible_interps):
     """
     Given the 4 input parameters, return at configuration that matches
 
@@ -41,6 +114,10 @@ def pick_tx_bitrate(bitrate, bits_per_symbol,
     @type bitrate: number or None
     @param bits_per_symbol: E.g., BPSK -> 1, QPSK -> 2, 8-PSK -> 3
     @type bits_per_symbol: integer >= 1
+    @param samples_per_symbol: samples/baud (aka samples/symbol)
+    @type samples_per_symbol: number or None
+    @param interp_rate: USRP interpolation factor
+    @type interp_rate: integer or None
     @param converter_rate: converter sample rate in Hz
     @type converter_rate: number
     @param possible_interps: a list of possible rates
@@ -49,16 +126,30 @@ def pick_tx_bitrate(bitrate, bits_per_symbol,
     @returns tuple (bitrate, samples_per_symbol, interp_rate)
     """
 
-    rates = list(possible_interps)
-    rates.sort()
+    return _pick_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
+                         interp_rate, converter_rate, possible_interps)
+
+
+def pick_rx_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
+                    decim_rate, converter_rate, possible_decims):
+    """
+    Given the 4 input parameters, return at configuration that matches
 
-    for i in xrange(len(rates)):
-        if((converter_rate / float(bits_per_symbol) / rates[i]) >= 2*bitrate):
-            interp = rates[i]
-        else:
-            break
+    @param bitrate: desired bitrate or None
+    @type bitrate: number or None
+    @param bits_per_symbol: E.g., BPSK -> 1, QPSK -> 2, 8-PSK -> 3
+    @type bits_per_symbol: integer >= 1
+    @param samples_per_symbol: samples/baud (aka samples/symbol)
+    @type samples_per_symbol: number or None
+    @param decim_rate: USRP decimation factor
+    @type decim_rate: integer or None
+    @param converter_rate: converter sample rate in Hz
+    @type converter_rate: number
+    @param possible_decims: a list of possible rates
+    @type possible_decims: a list of integers
 
-    sps = converter_rate / float(bits_per_symbol) / interp / bitrate
-    br = converter_rate / float(bits_per_symbol) / interp / sps
+    @returns tuple (bitrate, samples_per_symbol, decim_rate)
+    """
 
-    return (br, sps, int(interp))
+    return _pick_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
+                         decim_rate, converter_rate, possible_decims)