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