New pick_bitrate2 will take any combination of bitrate, samples per symbol, and conve...
[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
26 def _pick_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
27                   xrate, converter_rate, xrates):
28     """
29     @returns tuple (bitrate, samples_per_symbol, interp_rate_or_decim_rate)
30     """
31
32     if not isinstance(bits_per_symbol, int) or bits_per_symbol < 1:
33         raise ValueError, "bits_per_symbol must be an int >= 1"
34
35     converter_rate = float(converter_rate)
36     bits_per_symbol = float(bits_per_symbol)
37
38     # completely determined; if bitrate is specified, this overwrites it
39     if (samples_per_symbol is not None) and (xrate is not None):
40         bitrate = converter_rate / bits_per_symbol / xrate / samples_per_symbol
41
42     # If only SPS is given
43     if (bitrate is None) and (samples_per_symbol is not None) and (xrate is None):
44         xrate = max(xrates)
45         bitrate = converter_rate / bits_per_symbol / xrate / samples_per_symbol
46         
47     # If only xrate is given
48     if (bitrate is None) and (samples_per_symbol is None) and (xrate is not None):
49         samples_per_symbol = 2.0
50         bitrate = converter_rate / bits_per_symbol / xrate / samples_per_symbol
51
52     # If no parameters are give, use the default bit rate
53     if (bitrate is None) and (samples_per_symbol is None) and (xrate is None):
54         bitrate = _default_bitrate
55
56     # If only bitrate is specified, return max xrate and appropriate
57     # samples per symbol (minimum of 2.0) to reach bit rate
58     if (samples_per_symbol is None) and (xrate is None):
59         xrates.sort()
60         for i in xrange(len(xrates)):
61             if((converter_rate / bits_per_symbol / xrates[i]) >= 2*bitrate):
62                 rate = xrates[i]
63             else:
64                 break
65
66         xrate = rate
67         samples_per_symbol = converter_rate / bits_per_symbol / rate / bitrate
68         bitrate = converter_rate / bits_per_symbol / xrate / samples_per_symbol
69
70     # If bitrate and xrate are specified
71     if(samples_per_symbol is None):
72         samples_per_symbol = converter_rate / xrate / bits_per_symbol / bitrate
73
74     # If bitrate and SPS are specified
75     if(xrate is None):
76         xrate = converter_rate / samples_per_symbol / bits_per_symbol / bitrate
77
78     return (bitrate, samples_per_symbol, int(xrate))
79
80
81 def pick_tx_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
82                     interp_rate, converter_rate, possible_interps):
83     """
84     Given the 4 input parameters, return at configuration that matches
85
86     @param bitrate: desired bitrate or None
87     @type bitrate: number or None
88     @param bits_per_symbol: E.g., BPSK -> 1, QPSK -> 2, 8-PSK -> 3
89     @type bits_per_symbol: integer >= 1
90     @param samples_per_symbol: samples/baud (aka samples/symbol)
91     @type samples_per_symbol: number or None
92     @param interp_rate: USRP interpolation factor
93     @type interp_rate: integer or None
94     @param converter_rate: converter sample rate in Hz
95     @type converter_rate: number
96     @param possible_interps: a list of possible rates
97     @type possible_interps: a list of integers
98
99     @returns tuple (bitrate, samples_per_symbol, interp_rate)
100     """
101
102     return _pick_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
103                          interp_rate, converter_rate, possible_interps)
104
105
106 def pick_rx_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
107                     decim_rate, converter_rate, possible_decims):
108     """
109     Given the 4 input parameters, return at configuration that matches
110
111     @param bitrate: desired bitrate or None
112     @type bitrate: number or None
113     @param bits_per_symbol: E.g., BPSK -> 1, QPSK -> 2, 8-PSK -> 3
114     @type bits_per_symbol: integer >= 1
115     @param samples_per_symbol: samples/baud (aka samples/symbol)
116     @type samples_per_symbol: number or None
117     @param decim_rate: USRP decimation factor
118     @type decim_rate: integer or None
119     @param converter_rate: converter sample rate in Hz
120     @type converter_rate: number
121     @param possible_decims: a list of possible rates
122     @type possible_decims: a list of integers
123
124     @returns tuple (bitrate, samples_per_symbol, decim_rate)
125     """
126
127     return _pick_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
128                          decim_rate, converter_rate, possible_decims)