forked from Cccccyf/CCDM_for_probabilistic_shaping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCCDMUtils.py
86 lines (70 loc) · 1.85 KB
/
CCDMUtils.py
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
import numpy as np
def iterDiscreteQuant(pOpt, nSyms):
"""
Parameters
----------
pOpt: ndarray
the probability distribution given
nSyms: int
the symbol num
Returns: n_i: ndarray
the number of every symbol that conform the optimal distribution
p_iter_dis_quant: ndarray
the discrete probability after discrete quantification
-------
"""
m = len(pOpt)
ni = np.zeros(m)
t = np.log(np.reciprocal(pOpt))
pIterDisQuant = np.copy(t)
for i in range(nSyms):
#print(p_iter_dis_quant)
index = np.argmin(pIterDisQuant)
cj = ni[index] + 1
ni[index] = cj
pIterDisQuant[index] = (cj + 1) * np.log(cj + 1) - cj * np.log(cj) + t[index]
pIterDisQuant = ni / nSyms
# print(n_i)
# print(p_iter_dis_quant)
return ni, pIterDisQuant
def nChooseKLog2(n, k):
"""
Parameters
----------
n: n
k
Returns
-------
"""
nCK = 0
if k > n - k:
k = n - k
for i in range(1, k + 1):
nCK += np.log((n - (k - i)) / i) / np.log(2)
return nCK
def nChooseKsLog2(n, ks):
"""
get the num of combination
Parameters
----------
n:
ks: the list of the pick num
Returns
-------
the num of combination
"""
nn = n
nCKs = 0
ks = np.sort(ks)
for each in ks:
nCKs += nChooseKLog2(int(nn), int(each))
nn -= each
return nCKs
def initialize(pOpt, nSyms):
ni, pIterDisQuant = iterDiscreteQuant(pOpt, nSyms)
numInfoBits = np.floor(nChooseKsLog2(nSyms, ni))
return ni, pIterDisQuant, int(numInfoBits)
if __name__ == "__main__":
pOpt = [0.1, 0.2, 0.2, 0.5]
n = 10
print(initialize(pOpt, n))