23081117c9ee8809f54819d570b3eb4be88cd7e9
[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             print "Requested bitrate out of bounds"
72             sys.exit(1)
73             
74         samples_per_symbol = converter_rate / bits_per_symbol / rate / bitrate
75         bitrate = converter_rate / bits_per_symbol / xrate / samples_per_symbol
76
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
80
81     # If bitrate and SPS are specified
82     if(xrate is None):
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
86             xrates.sort()
87             for x in xrates:
88                 if(x > xrate):
89                     xrate = x
90                     break
91             if(xrate > max(xrates)):
92                 xrate = max(xrates)
93             
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)))
98
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)))
104         
105     return (bitrate, samples_per_symbol, int(xrate))
106
107
108 def pick_tx_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
109                     interp_rate, converter_rate, possible_interps):
110     """
111     Given the 4 input parameters, return at configuration that matches
112
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
125
126     @returns tuple (bitrate, samples_per_symbol, interp_rate)
127     """
128
129     return _pick_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
130                          interp_rate, converter_rate, possible_interps)
131
132
133 def pick_rx_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
134                     decim_rate, converter_rate, possible_decims):
135     """
136     Given the 4 input parameters, return at configuration that matches
137
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
150
151     @returns tuple (bitrate, samples_per_symbol, decim_rate)
152     """
153
154     return _pick_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
155                          decim_rate, converter_rate, possible_decims)