Roman Khassraf | 9144861 | 2015-08-21 11:14:51 +0200 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | # -*- coding: utf-8 -*- |
| 3 | # @file |
| 4 | # @author Roman Khassraf <rkhassraf@gmail.com> |
| 5 | # @section LICENSE |
| 6 | # |
| 7 | # Gr-gsm is free software; you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU General Public License as published by |
| 9 | # the Free Software Foundation; either version 3, or (at your option) |
| 10 | # any later version. |
| 11 | # |
| 12 | # Gr-gsm is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | # GNU General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License |
| 18 | # along with gr-gsm; see the file COPYING. If not, write to |
| 19 | # the Free Software Foundation, Inc., 51 Franklin Street, |
| 20 | # Boston, MA 02110-1301, USA. |
| 21 | # |
| 22 | # |
| 23 | |
| 24 | # first uplink freq, first arfcn, last arfcn, downlink frequence distance |
| 25 | __band_conf = {'GSM450': {'first_freq': 450.6e6, 'first_arfcn': 259, 'last_arfcn': 293, 'downlink_dist': 10e6}, |
| 26 | 'GSM480': {'first_freq': 479e6, 'first_arfcn': 306, 'last_arfcn': 340, 'downlink_dist': 10e6}, |
| 27 | 'GSM850': {'first_freq': 824.2e6, 'first_arfcn': 128, 'last_arfcn': 251, 'downlink_dist': 45e6}, |
| 28 | 'P-GSM': {'first_freq': 890.2e6, 'first_arfcn': 1, 'last_arfcn': 124, 'downlink_dist': 45e6}, |
| 29 | 'E-GSM': {'first_freq': 880.2e6, 'first_arfcn': 975, 'last_arfcn': 1023, 'downlink_dist': 45e6}, |
| 30 | 'R-GSM': {'first_freq': 876.2e6, 'first_arfcn': 955, 'last_arfcn': 1023, 'downlink_dist': 45e6}, |
| 31 | 'DCS1800': {'first_freq': 1710.2e6, 'first_arfcn': 512, 'last_arfcn': 885, 'downlink_dist': 95e6}, |
| 32 | 'PCS1900': {'first_freq': 1850.2e6, 'first_arfcn': 512, 'last_arfcn': 810, 'downlink_dist': 80e6}} |
| 33 | |
| 34 | __chan_spacing = 2e5 |
| 35 | |
| 36 | |
| 37 | def get_bands(): |
| 38 | return __band_conf.keys() |
| 39 | |
| 40 | |
| 41 | def is_valid_arfcn(arfcn, band): |
| 42 | """ |
| 43 | Returns True if arfcn is valid in the given band, else False |
| 44 | """ |
| 45 | if band in __band_conf: |
| 46 | conf = __band_conf.get(band) |
| 47 | first_arfcn = conf['first_arfcn'] |
| 48 | last_arfcn = conf['last_arfcn'] |
| 49 | if first_arfcn <= arfcn <= last_arfcn: |
| 50 | return True |
| 51 | return False |
| 52 | |
| 53 | |
| 54 | def is_valid_uplink(freq, band): |
| 55 | """ |
| 56 | Returns True if the given frequency is a valid uplink frequency in the given band |
| 57 | """ |
| 58 | if band in __band_conf: |
| 59 | conf = __band_conf.get(band) |
| 60 | first_freq = arfcn2uplink(conf['first_arfcn'], band) |
| 61 | last_freq = arfcn2uplink(conf['last_arfcn'], band) |
| 62 | if first_freq is None or last_freq is None: |
| 63 | return False |
| 64 | if first_freq <= freq <= last_freq: |
| 65 | return True |
| 66 | return False |
| 67 | |
| 68 | |
| 69 | def is_valid_downlink(freq, band): |
| 70 | """ |
| 71 | Returns True if the given frequency is a valid downlink frequency in the given band |
| 72 | """ |
| 73 | if band in __band_conf: |
| 74 | conf = __band_conf.get(band) |
| 75 | first_freq = arfcn2downlink(conf['first_arfcn'], band) |
| 76 | last_freq = arfcn2downlink(conf['last_arfcn'], band) |
| 77 | if first_freq is None or last_freq is None: |
| 78 | return False |
| 79 | if first_freq <= freq <= last_freq: |
| 80 | return True |
| 81 | return False |
| 82 | |
| 83 | |
| 84 | def arfcn2uplink(arfcn, band): |
| 85 | if band in __band_conf and is_valid_arfcn(arfcn, band): |
| 86 | |
| 87 | conf = __band_conf.get(band) |
| 88 | freq_start = conf['first_freq'] |
| 89 | offset = conf['first_arfcn'] |
| 90 | # if (band == 'E-GSM' or band == 'R-GSM') and arfcn > 124: |
| 91 | # offset = 1024 |
| 92 | f = freq_start + (__chan_spacing * (arfcn - offset)) |
| 93 | return round(f, 1) |
| 94 | return None |
| 95 | |
| 96 | |
| 97 | def arfcn2downlink(arfcn, band): |
| 98 | if band in __band_conf and is_valid_arfcn(arfcn, band): |
| 99 | conf = __band_conf.get(band) |
| 100 | distance = conf['downlink_dist'] |
| 101 | return round(arfcn2uplink(arfcn, band) + distance, 1) |
| 102 | return None |
| 103 | |
| 104 | |
| 105 | def uplink2arfcn(freq, band): |
| 106 | if band in __band_conf and is_valid_uplink(freq, band): |
| 107 | conf = __band_conf.get(band) |
| 108 | freq_start = conf['first_freq'] |
| 109 | offset = conf['first_arfcn'] |
| 110 | return int(round(offset + ((freq - freq_start) / __chan_spacing), 0)) |
| 111 | return None |
| 112 | |
| 113 | |
| 114 | def downlink2arfcn(freq, band): |
| 115 | if band in __band_conf and is_valid_downlink(freq, band): |
| 116 | conf = __band_conf.get(band) |
| 117 | distance = conf['downlink_dist'] |
| 118 | freq_uplink = freq - distance |
| 119 | return int(round(uplink2arfcn(freq_uplink, band), 0)) |
| 120 | return None |