Merge branch 'dfsg-orig'
[debian/gnuradio] / gnuradio-examples / python / digital / pick_bitrate2.py
1 #
2 # Copyright 2010 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 _sps_min = 2
26 _sps_max = 100
27
28 def _pick_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
29                   xrate, converter_rate, xrates):
30     """
31     @returns tuple (bitrate, samples_per_symbol, interp_rate_or_decim_rate)
32     """
33
34     if not isinstance(bits_per_symbol, int) or bits_per_symbol < 1:
35         raise ValueError, "bits_per_symbol must be an int >= 1"
36
37     converter_rate = float(converter_rate)
38     bits_per_symbol = float(bits_per_symbol)
39
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
43
44     # If only SPS is given
45     if (bitrate is None) and (samples_per_symbol is not None) and (xrate is None):
46         xrate = max(xrates)
47         bitrate = converter_rate / bits_per_symbol / xrate / samples_per_symbol
48         
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
53
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
57
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):
61         xrates.sort()
62         for i in xrange(len(xrates)):
63             if((converter_rate / bits_per_symbol / xrates[i]) >= 2*bitrate):
64                 rate = xrates[i]
65             else:
66                 break
67
68         try:
69             xrate = rate
70         except UnboundLocalError:
71             raise ValueError("Requested bitrate out of bounds")
72             
73         samples_per_symbol = converter_rate / bits_per_symbol / rate / bitrate
74         bitrate = converter_rate / bits_per_symbol / xrate / samples_per_symbol
75
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
79
80     # If bitrate and SPS are specified
81     if(xrate is None):
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
85             xrates.sort()
86             for x in xrates:
87                 if(x > xrate):
88                     xrate = x
89                     break
90             if(xrate > max(xrates)):
91                 xrate = max(xrates)
92             
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)))
97
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)))
103         
104     return (bitrate, samples_per_symbol, int(xrate))
105
106
107 def pick_tx_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
108                     interp_rate, converter_rate, possible_interps):
109     """
110     Given the 4 input parameters, return at configuration that matches
111
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
124
125     @returns tuple (bitrate, samples_per_symbol, interp_rate)
126     """
127
128     return _pick_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
129                          interp_rate, converter_rate, possible_interps)
130
131
132 def pick_rx_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
133                     decim_rate, converter_rate, possible_decims):
134     """
135     Given the 4 input parameters, return at configuration that matches
136
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
149
150     @returns tuple (bitrate, samples_per_symbol, decim_rate)
151     """
152
153     return _pick_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
154                          decim_rate, converter_rate, possible_decims)