Working on pick_bitrate.
[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, gen_info):
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     if samples_per_symbol is not None and xrate is not None:  # completely determined
36         return (float(converter_rate) / xrate / samples_per_symbol,
37                 samples_per_symbol, xrate)
38
39     # If no parameters are give, use the default bit rate
40     if bitrate is None and samples_per_symbol is None and xrate is None:
41         bitrate = _default_bitrate
42
43     # If only bitrate is specified, return max xrate and appropriate
44     # samples per symbol to reach bit rate
45     if samples_per_symbol is None and xrate is None:
46         xrates.sort()
47         for i in xrange(len(xrates)):
48             if((converter_rate / float(bits_per_symbol) / xrates[i]) >= 2*bitrate):
49                 decim = xrates[i]
50             else:
51                 break
52             
53         sps = converter_rate / float(bits_per_symbol) / decim / bitrate
54         br = converter_rate / float(bits_per_symbol) / decim / sps
55
56         return (br, sps, int(decim))
57
58
59     # now we have a target bitrate and possibly an xrate or
60     # samples_per_symbol constraint, but not both of them.
61     ret = _pick_best(bitrate, bits_per_symbol,
62                       _filter_info(gen_info(converter_rate, xrates),
63                                    samples_per_symbol, xrate))
64
65     print "Actual Bitrate:", eng_notation.num_to_str(ret[0])
66     return ret
67
68
69
70 def pick_tx_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
71                     interp_rate, converter_rate, possible_interps):
72     """
73     Given the 4 input parameters, return at configuration that matches
74
75     @param bitrate: desired bitrate or None
76     @type bitrate: number or None
77     @param bits_per_symbol: E.g., BPSK -> 1, QPSK -> 2, 8-PSK -> 3
78     @type bits_per_symbol: integer >= 1
79     @param samples_per_symbol: samples/baud (aka samples/symbol)
80     @type samples_per_symbol: number or None
81     @param interp_rate: USRP interpolation factor
82     @type interp_rate: integer or None
83     @param converter_rate: converter sample rate in Hz
84     @type converter_rate: number
85     @param possible_interps: a list of possible rates
86     @type possible_interps: a list of integers
87
88     @returns tuple (bitrate, samples_per_symbol, interp_rate)
89     """
90
91     return _pick_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
92                          interp_rate, converter_rate, possible_interps, _gen_tx_info)
93
94     rates = list(possible_interps)
95     rates.sort()
96
97     for i in xrange(len(rates)):
98         if((converter_rate / float(bits_per_symbol) / rates[i]) >= 2*bitrate):
99             interp = rates[i]
100         else:
101             break
102
103     sps = converter_rate / float(bits_per_symbol) / interp / bitrate
104     br = converter_rate / float(bits_per_symbol) / interp / sps
105
106     return (br, sps, int(interp))