Imported Debian patch 3.0-1
[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 2, 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 _default_bitrate = 500e3
24
25 _valid_samples_per_symbol = (2,3,4,5,6,7)
26
27 def _gen_tx_info(converter_rate):
28     results = []
29     for samples_per_symbol in _valid_samples_per_symbol:
30         for interp in range(16, 512 + 1, 4):
31             bitrate = converter_rate / interp / samples_per_symbol
32             results.append((bitrate, samples_per_symbol, interp))
33     results.sort()
34     return results
35
36 def _gen_rx_info(converter_rate):
37     results = []
38     for samples_per_symbol in _valid_samples_per_symbol:
39         for decim in range(8, 256 + 1, 2):
40             bitrate = converter_rate / decim / samples_per_symbol
41             results.append((bitrate, samples_per_symbol, decim))
42     results.sort()
43     return results
44     
45 def _filter_info(info, samples_per_symbol, xrate):
46     if samples_per_symbol is not None:
47         info = [x for x in info if x[1] == samples_per_symbol]
48     if xrate is not None:
49         info = [x for x in info if x[2] == xrate]
50     return info
51
52 def _pick_best(target_bitrate, bits_per_symbol, info):
53     """
54     @returns tuple (bitrate, samples_per_symbol, interp_rate_or_decim_rate)
55     """
56     if len(info) == 0:
57         raise RuntimeError, "info is zero length!"
58
59     if target_bitrate is None:     # return the fastest one
60         return info[-1]
61     
62     # convert bit rate to symbol rate
63     target_symbolrate = target_bitrate / bits_per_symbol
64     
65     # Find the closest matching symbol rate.
66     # In the event of a tie, the one with the lowest samples_per_symbol wins.
67     # (We already sorted them, so the first one is the one we take)
68
69     best = info[0]
70     best_delta = abs(target_symbolrate - best[0])
71     for x in info[1:]:
72         delta = abs(target_symbolrate - x[0])
73         if delta < best_delta:
74             best_delta = delta
75             best = x
76
77     # convert symbol rate back to bit rate
78     return ((best[0] * bits_per_symbol),) + best[1:]
79
80 def _pick_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
81                   xrate, converter_rate, gen_info):
82     """
83     @returns tuple (bitrate, samples_per_symbol, interp_rate_or_decim_rate)
84     """
85     if not isinstance(bits_per_symbol, int) or bits_per_symbol < 1:
86         raise ValueError, "bits_per_symbol must be an int >= 1"
87     
88     if samples_per_symbol is not None and xrate is not None:  # completely determined
89         return (float(converter_rate) / xrate / samples_per_symbol,
90                 samples_per_symbol, xrate)
91
92     if bitrate is None and samples_per_symbol is None and xrate is None:
93         bitrate = _default_bitrate
94
95     # now we have a target bitrate and possibly an xrate or
96     # samples_per_symbol constraint, but not both of them.
97
98     return _pick_best(bitrate, bits_per_symbol,
99                       _filter_info(gen_info(converter_rate), samples_per_symbol, xrate))
100     
101 # ---------------------------------------------------------------------------------------
102
103 def pick_tx_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
104                     interp_rate, converter_rate=128e6):
105     """
106     Given the 4 input parameters, return at configuration that matches
107
108     @param bitrate: desired bitrate or None
109     @type bitrate: number or None
110     @param bits_per_symbol: E.g., BPSK -> 1, QPSK -> 2, 8-PSK -> 3
111     @type bits_per_symbol: integer >= 1
112     @param samples_per_symbol: samples/baud (aka samples/symbol)
113     @type samples_per_symbol: number or None
114     @param interp_rate: USRP interpolation factor
115     @type interp_rate: integer or None
116     @param converter_rate: converter sample rate in Hz
117     @type converter_rate: number
118
119     @returns tuple (bitrate, samples_per_symbol, interp_rate)
120     """
121     return _pick_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
122                          interp_rate, converter_rate, _gen_tx_info)
123
124
125 def pick_rx_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
126                     decim_rate, converter_rate=64e6):
127     """
128     Given the 4 input parameters, return at configuration that matches
129
130     @param bitrate: desired bitrate or None
131     @type bitrate: number or None
132     @param bits_per_symbol: E.g., BPSK -> 1, QPSK -> 2, 8-PSK -> 3
133     @type bits_per_symbol: integer >= 1
134     @param samples_per_symbol: samples/baud (aka samples/symbol)
135     @type samples_per_symbol: number or None
136     @param decim_rate: USRP decimation factor
137     @type decim_rate: integer or None
138     @param converter_rate: converter sample rate in Hz
139     @type converter_rate: number
140
141     @returns tuple (bitrate, samples_per_symbol, decim_rate)
142     """
143     return _pick_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
144                          decim_rate, converter_rate, _gen_rx_info)