aboutsummaryrefslogtreecommitdiffstats
path: root/python/misc_utils/arfcn.py
blob: bf227b4917d7dbb0f289a19565b6c391fffda001 (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
155
156
157
158
159
160
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @file
# @author (C) 2015 by Roman Khassraf <rkhassraf@gmail.com>
# @section LICENSE
#
# Gr-gsm 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.
#
# Gr-gsm 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 gr-gsm; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#
#

import collections

# first uplink freq, distance between uplink/downlink frequency, list of range tuple
# each tuple in a range tuple contains: first arfcn of the range, last arfcn of the range, offset of the range
# entries are ordered by relevance
__band_conf = collections.OrderedDict([
    ('P-GSM', {'f_start': 890.0e6, 'distance': 45e6, 'ranges': [(1, 124, 0)]}),
    ('DCS1800', {'f_start': 1710.2e6, 'distance': 95e6, 'ranges': [(512, 885, 512)]}),
    ('PCS1900', {'f_start': 1850.2e6, 'distance': 80e6, 'ranges': [(512, 810, 512)]}),
    ('E-GSM', {'f_start': 890.0e6, 'distance': 45e6, 'ranges': [(0, 124, 0), (975, 1023, 1024)]}),
    ('R-GSM', {'f_start': 890.0e6, 'distance': 45e6, 'ranges': [(0, 124, 0), (955, 1023, 1024)]}),
    ('GSM450', {'f_start': 450.6e6, 'distance': 10e6, 'ranges': [(259, 293, 259)]}),
    ('GSM480', {'f_start': 479e6, 'distance': 10e6, 'ranges': [(306, 340, 306)]}),
    ('GSM850', {'f_start': 824.2e6, 'distance': 45e6, 'ranges': [(128, 251, 128)]}),
])

__chan_spacing = 2e5


def get_bands():
    return __band_conf.keys()


def is_valid_arfcn(arfcn, band):
    """
    Returns True if arfcn is valid in the given band, else False
    """
    if band in __band_conf:
        conf = __band_conf.get(band)
        for arfcn_range in conf['ranges']:
            arfcn_start = arfcn_range[0]
            arfcn_end = arfcn_range[1]
            if arfcn_start <= arfcn <= arfcn_end:
                return True
    return False


def is_valid_uplink(freq, band):
    """
    Returns True if the given frequency is a valid uplink frequency in the given band
    """
    result = None
    if band in __band_conf:
        conf = __band_conf.get(band)
        result = False
        for arfcn_range in conf['ranges']:
            arfcn_start = arfcn_range[0]
            arfcn_end = arfcn_range[1]
            first_freq = arfcn2uplink(arfcn_start, band)
            last_freq = arfcn2uplink(arfcn_end, band)
            if first_freq is None or last_freq is None:
                result = False
            elif first_freq <= freq <= last_freq:
                result = True
    return result


def is_valid_downlink(freq, band):
    """
    Returns True if the given frequency is a valid downlink frequency in the given band
    """
    result = None
    if band in __band_conf:
        conf = __band_conf.get(band)
        result = False
        for arfcn_range in conf['ranges']:
            arfcn_start = arfcn_range[0]
            arfcn_end = arfcn_range[1]
            first_freq = arfcn2downlink(arfcn_start, band)
            last_freq = arfcn2downlink(arfcn_end, band)
            if first_freq is None or last_freq is None:
                result = False
            elif first_freq <= freq <= last_freq:
                result = True
    return result


def arfcn2uplink(arfcn, band):
    if band in __band_conf and is_valid_arfcn(arfcn, band):
        conf = __band_conf.get(band)
        f_start = conf['f_start']
        offset = None
        for arfcn_range in conf['ranges']:
            arfcn_start = arfcn_range[0]
            arfcn_end = arfcn_range[1]
            if arfcn_start <= arfcn <= arfcn_end:
                offset = arfcn_range[2]

        if offset is not None:
            f = f_start + (__chan_spacing * (arfcn - offset))
            return round(f, 1)
    return None


def arfcn2downlink(arfcn, band):
    if band in __band_conf and is_valid_arfcn(arfcn, band):
        conf = __band_conf.get(band)
        distance = conf['distance']
        return round(arfcn2uplink(arfcn, band) + distance, 1)
    return None


def uplink2arfcn(freq, band):
    if band in __band_conf and is_valid_uplink(freq, band):
        conf = __band_conf.get(band)
        f_start = conf['f_start']
        offset = None
        for arfcn_range in conf['ranges']:
            arfcn_start = arfcn_range[0]
            arfcn_end = arfcn_range[1]
            offset = arfcn_range[2]
            arfcn = int(round(offset + ((freq - f_start) / __chan_spacing), 0))
            if arfcn_start <= arfcn <= arfcn_end:
                return arfcn
    return None


def downlink2arfcn(freq, band):
    if band in __band_conf and is_valid_downlink(freq, band):
        conf = __band_conf.get(band)
        distance = conf['distance']
        freq_uplink = freq - distance
        return int(round(uplink2arfcn(freq_uplink, band), 0))
    return None


def get_arfcn_ranges(band):
    """
    Returns a list of arfcn tuples, each with first and last arfcn of the range.
    """
    result = []
    if band in __band_conf:
        conf = __band_conf.get(band)
        for arfcn_range in conf['ranges']:
            arfcn_tuple = (arfcn_range[0], arfcn_range[1])
            result.append(arfcn_tuple)
    return result