summaryrefslogtreecommitdiff
path: root/gnuradio-examples/python/digital/pick_bitrate2.py
blob: 92539560c3427515c96588537d5436d88f66f28c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#
# Copyright 2010 Free Software Foundation, Inc.
# 
# This file is part of GNU Radio
# 
# GNU Radio is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
# 
# GNU Radio is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with GNU Radio; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
# 

from gnuradio import eng_notation

_default_bitrate = 500e3
_sps_min = 2
_sps_max = 100

def _pick_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
                  xrate, converter_rate, xrates):
    """
    @returns tuple (bitrate, samples_per_symbol, interp_rate_or_decim_rate)
    """

    if not isinstance(bits_per_symbol, int) or bits_per_symbol < 1:
        raise ValueError, "bits_per_symbol must be an int >= 1"

    converter_rate = float(converter_rate)
    bits_per_symbol = float(bits_per_symbol)

    # completely determined; if bitrate is specified, this overwrites it
    if (samples_per_symbol is not None) and (xrate is not None):
        bitrate = converter_rate / bits_per_symbol / xrate / samples_per_symbol

    # If only SPS is given
    if (bitrate is None) and (samples_per_symbol is not None) and (xrate is None):
        xrate = max(xrates)
        bitrate = converter_rate / bits_per_symbol / xrate / samples_per_symbol
        
    # If only xrate is given, just set SPS to 2 and calculate bitrate
    if (bitrate is None) and (samples_per_symbol is None) and (xrate is not None):
        samples_per_symbol = 2.0
        bitrate = converter_rate / bits_per_symbol / xrate / samples_per_symbol

    # If no parameters are give, use the default bit rate
    if (bitrate is None) and (samples_per_symbol is None) and (xrate is None):
        bitrate = _default_bitrate

    # If only bitrate is specified, return max xrate and appropriate
    # samples per symbol (minimum of 2.0) to reach bit rate
    if (samples_per_symbol is None) and (xrate is None):
        xrates.sort()
        for i in xrange(len(xrates)):
            if((converter_rate / bits_per_symbol / xrates[i]) >= 2*bitrate):
                rate = xrates[i]
            else:
                break

        try:
            xrate = rate
        except UnboundLocalError:
            raise ValueError("Requested bitrate out of bounds")
            
        samples_per_symbol = converter_rate / bits_per_symbol / rate / bitrate
        bitrate = converter_rate / bits_per_symbol / xrate / samples_per_symbol

    # If bitrate and xrate are specified
    if(samples_per_symbol is None):
        samples_per_symbol = converter_rate / xrate / bits_per_symbol / bitrate

    # If bitrate and SPS are specified
    if(xrate is None):
        xrate = converter_rate / samples_per_symbol / bits_per_symbol / bitrate
        if((xrate in xrates) == False):
            # Find the closest avaiable rate larger than the calculated one
            xrates.sort()
            for x in xrates:
                if(x > xrate):
                    xrate = x
                    break
            if(xrate > max(xrates)):
                xrate = max(xrates)
            
            bitrate = converter_rate / bits_per_symbol / xrate / samples_per_symbol
            print "Could not find suitable rate for specified SPS and Bitrate"
            print "Using rate = %d for bitrate of %sbps" % \
                  (xrate, (eng_notation.num_to_str(bitrate)))

    if((xrate in xrates) == False):
        raise ValueError(("Invalid rate (rate = %d)" % xrate))
    if((samples_per_symbol < _sps_min) or (samples_per_symbol > _sps_max)):
        raise ValueError(("Invalid samples per symbol (sps = %.2f). Must be in [%.0f, %.0f]." \
                          % (samples_per_symbol, _sps_min, _sps_max)))
        
    return (bitrate, samples_per_symbol, int(xrate))


def pick_tx_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
                    interp_rate, converter_rate, possible_interps):
    """
    Given the 4 input parameters, return at configuration that matches

    @param bitrate: desired bitrate or None
    @type bitrate: number or None
    @param bits_per_symbol: E.g., BPSK -> 1, QPSK -> 2, 8-PSK -> 3
    @type bits_per_symbol: integer >= 1
    @param samples_per_symbol: samples/baud (aka samples/symbol)
    @type samples_per_symbol: number or None
    @param interp_rate: USRP interpolation factor
    @type interp_rate: integer or None
    @param converter_rate: converter sample rate in Hz
    @type converter_rate: number
    @param possible_interps: a list of possible rates
    @type possible_interps: a list of integers

    @returns tuple (bitrate, samples_per_symbol, interp_rate)
    """

    return _pick_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
                         interp_rate, converter_rate, possible_interps)


def pick_rx_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
                    decim_rate, converter_rate, possible_decims):
    """
    Given the 4 input parameters, return at configuration that matches

    @param bitrate: desired bitrate or None
    @type bitrate: number or None
    @param bits_per_symbol: E.g., BPSK -> 1, QPSK -> 2, 8-PSK -> 3
    @type bits_per_symbol: integer >= 1
    @param samples_per_symbol: samples/baud (aka samples/symbol)
    @type samples_per_symbol: number or None
    @param decim_rate: USRP decimation factor
    @type decim_rate: integer or None
    @param converter_rate: converter sample rate in Hz
    @type converter_rate: number
    @param possible_decims: a list of possible rates
    @type possible_decims: a list of integers

    @returns tuple (bitrate, samples_per_symbol, decim_rate)
    """

    return _pick_bitrate(bitrate, bits_per_symbol, samples_per_symbol,
                         decim_rate, converter_rate, possible_decims)