Merge commit 'upstream/3.2.2'
[debian/gnuradio] / gnuradio-examples / python / digital / pick_bitrate.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2005,2006 Free Software Foundation, Inc.
4
5 # This file is part of GNU Radio
6
7 # GNU Radio is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3, or (at your option)
10 # any later version.
11
12 # GNU Radio is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with GNU Radio; see the file COPYING.  If not, write to
19 # the Free Software Foundation, Inc., 51 Franklin Street,
20 # Boston, MA 02110-1301, USA.
21
22
23 from gnuradio import eng_notation
24
25 _default_bitrate = 500e3
26
27 _valid_samples_per_symbol = (2,3,4,5,6,7)
28
29 def _gen_tx_info(converter_rate, xrates):
30     results = []
31     for samples_per_symbol in _valid_samples_per_symbol:
32         for interp in xrates:
33             bitrate = converter_rate / interp / samples_per_symbol
34             results.append((bitrate, samples_per_symbol, interp))
35     results.sort()
36     return results
37
38 def _gen_rx_info(converter_rate, xrates):
39     results = []
40     for samples_per_symbol in _valid_samples_per_symbol:
41         for decim in xrates:
42             bitrate = converter_rate / decim / samples_per_symbol
43             results.append((bitrate, samples_per_symbol, decim))
44     results.sort()
45     return results
46     
47 def _filter_info(info, samples_per_symbol, xrate):
48     if samples_per_symbol is not None:
49         info = [x for x in info if x[1] == samples_per_symbol]
50     if xrate is not None:
51         info = [x for x in info if x[2] == xrate]
52     return info
53
54 def _pick_best(target_bitrate, bits_per_symbol, info):
55     """
56     @returns tuple (bitrate, samples_per_symbol, interp_rate_or_decim_rate)
57     """
58     if len(info) == 0:
59         raise RuntimeError, "info is zero length!"
60
61     if target_bitrate is None:     # return the fastest one
62         return info[-1]
63     
64     # convert bit rate to symbol rate
65     target_symbolrate = target_bitrate / bits_per_symbol
66     
67     # Find the closest matching symbol rate.
68     # In the event of a tie, the one with the lowest samples_per_symbol wins.
69     # (We already sorted them, so the first one is the one we take)
70
71     best = info[0]
72     best_delta = abs(target_symbolrate - best[0])
73     for x in info[1:]:
74         delta = abs(target_symbolrate - x[0])
75         if delta < best_delta:
76             best_delta = delta
77             best = x
78
79     # convert symbol rate back to bit rate
80     return ((best[0] * bits_per_symbol),) + best[1:]
81
82 def _pick_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
83                   xrate, converter_rate, xrates, gen_info):
84     """
85     @returns tuple (bitrate, samples_per_symbol, interp_rate_or_decim_rate)
86     """
87     if not isinstance(bits_per_symbol, int) or bits_per_symbol < 1:
88         raise ValueError, "bits_per_symbol must be an int >= 1"
89     
90     if samples_per_symbol is not None and xrate is not None:  # completely determined
91         return (float(converter_rate) / xrate / samples_per_symbol,
92                 samples_per_symbol, xrate)
93
94     if bitrate is None and samples_per_symbol is None and xrate is None:
95         bitrate = _default_bitrate
96
97     # now we have a target bitrate and possibly an xrate or
98     # samples_per_symbol constraint, but not both of them.
99
100     ret = _pick_best(bitrate, bits_per_symbol,
101                       _filter_info(gen_info(converter_rate, xrates), samples_per_symbol, xrate))
102     print "Actual Bitrate:", eng_notation.num_to_str(ret[0])
103     return ret
104     
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     print "Requested TX Bitrate:", bitrate and eng_notation.num_to_str(bitrate) or 'Auto',
128     return _pick_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
129                          interp_rate, converter_rate, possible_interps, _gen_tx_info)
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     print "Requested RX Bitrate:", bitrate and eng_notation.num_to_str(bitrate) or 'Auto'
153     return _pick_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
154                          decim_rate, converter_rate, possible_decims, _gen_rx_info)