aboutsummaryrefslogtreecommitdiffstats
path: root/op25/gr-op25_repeater/apps/tdma/bit_utils.py
blob: 7238807ecc51e0492e16293b4b9b4fba217c93a1 (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
# P25 TDMA Decoder (C) Copyright 2013 KA1RBI
# 
# This file is part of OP25
# 
# OP25 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.
# 
# OP25 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 OP25; see the file COPYING. If not, write to the Free
# Software Foundation, Inc., 51 Franklin Street, Boston, MA
# 02110-1301, USA.

import numpy as np

def rev_int(n,l):
	j=0
	for i in range(l):
		b=n&1
		n=n>>1
		j = (j << 1) | b
	return j

def bits_to_dibits(bits):
	d = []
	for i in range(len(bits)>>1):
		d.append((bits[i*2]<<1) + bits[i*2+1])
	return d

def dibits_to_bits(dibits):
	b = []
	for d in dibits:
		b.append((d>>1)&1)
		b.append(d&1)
	return b

def mk_array(n, l):
	a = []
	for i in range(0,l):
		a.insert(0, n & 1)
		n = n >> 1
	return np.array(a)

def mk_int(a):
	res= 0
	for i in range(0, len(a)):
		res = res * 2
		res = res + (a[i] & 1)
	return res

def mk_str(a):
	return ''.join(['%s' % (x&1) for x in a])

def check_l(a,b):
	ans = 0
	assert len(a) == len(b)
	for i in range(len(a)):
		if (a[i] == b[i]):
			ans += 1
	return ans

def fixup(a):
	res = []
	for c in a:
		if c == 3:
			res.append(1)
		else:	# -3
			res.append(3)

	return res

def find_sym(pattern, symbols):
	for i in range(0, len(symbols)-len(pattern)):
		chunk = symbols[i : i + len(pattern)]
		if chunk == pattern:
			return i
	return -1