summaryrefslogtreecommitdiff
path: root/gr-fec/python/fec/polar/helper_functions.py
blob: ffa4fc17dc9ff08f3e23eb3c075c3ef1e5937cd4 (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
#!/usr/bin/env python
#
# Copyright 2015 Free Software Foundation, Inc.
#
# 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.
#

import numpy as np


def is_power_of_two(num):
    if type(num) != int:
        return False  # make sure we only compute integers.
    return num != 0 and ((num & (num - 1)) == 0)


def bit_reverse(value, n):
    # is this really missing in NumPy???
    seq = np.int(value)
    rev = np.int(0)
    rmask = np.int(1)
    lmask = np.int(2 ** (n - 1))
    for i in range(n // 2):
        shiftval = n - 1 - (i * 2)
        rshift = np.left_shift(np.bitwise_and(seq, rmask), shiftval)
        lshift = np.right_shift(np.bitwise_and(seq, lmask), shiftval)
        rev = np.bitwise_or(rev, rshift)
        rev = np.bitwise_or(rev, lshift)
        rmask = np.left_shift(rmask, 1)
        lmask = np.right_shift(lmask, 1)
    if not n % 2 == 0:
        rev = np.bitwise_or(rev, np.bitwise_and(seq, rmask))
    return rev


def bit_reverse_vector(vec, n):
    return np.array([bit_reverse(e, n) for e in vec], dtype=vec.dtype)


def unpack_byte(byte, nactive):
    if np.amin(byte) < 0 or np.amax(byte) > 255:
        return None
    if not byte.dtype == np.uint8:
        byte = byte.astype(np.uint8)
    if nactive == 0:
        return np.array([], dtype=np.uint8)
    return np.unpackbits(byte)[-nactive:]


def pack_byte(bits):
    if len(bits) == 0:
        return 0
    if np.amin(bits) < 0 or np.amax(bits) > 1:  # only '1' and '0' in bits array allowed!
        return None
    bits = np.concatenate((np.zeros(8 - len(bits), dtype=np.uint8), bits))
    res = np.packbits(bits)[0]
    return res


def get_frozen_bit_positions(directory, n, k, p):
    import glob, os
    os.chdir(directory)
    prefix = 'frozen_bit_positions_'
    prefix_len = len(prefix)
    for file in glob.glob("*.npy"):
        if file.find(prefix) < 0:
            continue
        filename = file
        file = file[file.find(prefix) + prefix_len:]
        file = file[:-4]
        file = file.split('_')
        nstr = [x for x in file if x.startswith('n')]
        kstr = [x for x in file if x.startswith('k')]
        pstr = [x for x in file if x.startswith('p')]
        nstr = int(nstr[0][1:])
        kstr = int(kstr[0][1:])
        pstr = float(pstr[0][1:])
        if n == nstr and k == kstr:
            return np.load(filename)
    return np.arange(k)


def main():
    print 'helper functions'

    for i in range(8):
        print(i, 'is power of 2: ', is_power_of_two(i))
    n = 2 ** 6
    k = n // 2
    eta = 0.3

    # frozen_bit_positions = get_frozen_bit_positions('.', 256, 128, 0.11)
    # print(frozen_bit_positions)

    print(np.arange(16))
    print bit_reverse_vector(np.arange(16), 4)

if __name__ == '__main__':
    main()