-
Notifications
You must be signed in to change notification settings - Fork 0
/
shannon.py
111 lines (90 loc) · 3.02 KB
/
shannon.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
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
import math
import sys
from PyQt5 import QtWidgets
from shannon_ui import Shannon_MainWindow
class Mywindow(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.ui = Shannon_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.Shannon_Coding)
def Shannon_Coding(self):
Q, sp = self.Read_Input()
if Q == 0 and sp == 0:
self.ui_error.show()
return
shannon_code = [''] * len(sp)
sp.sort(reverse=True)
for i in range(len(sp)):
P_acc = 0
code = ''
l = int(-math.log2(sp[i]) + 1)
for j in range(i):
P_acc += sp[j]
while True:
P_acc *= 2
if P_acc >= 1:
code += '1'
else:
code += '0'
if len(code) == l:
break
P_acc -= int(P_acc)
if P_acc == 0:
if (len(code) < l):
code += '0' * (l - len(code))
break
shannon_code[i] = code[:l]
# print(shannon_code)
outcome = ''
for i in range(len(sp)):
outcome = outcome + str(sp[i]) + ' ' + shannon_code[i] + ' ' + '\n'
self.ui.textEdit_2.setPlainText(outcome)
self.Calculate_Performance_Index(sp, shannon_code)
def Read_Input(self):
Q = int(self.ui.lineEdit.text()) # num of symbol
text = self.ui.textEdit.toPlainText()
sp = self.Get_Symbol_Probability(text) # symbol probability
if Q < 10 or len(sp) != Q or sum(sp) < 0.99999:
# self.ui_error.show()
# print(sum(sp))
return 0, 0, 0, 0
else:
# print(Q,N,R,sp)
return Q, sp
def Str_Find_HH(self, str): # HH:huan hang '\n'
n = len(str)
pos = list()
for i in range(n):
if str[i] == '\n':
pos.append(i)
# print(pos)
return pos
def Get_Symbol_Probability(self, str):
n = len(str)
pos = self.Str_Find_HH(str)
sp = list()
n_pos = len(pos)
for i in range(n_pos):
if i == 0:
sp.append(float(str[0:pos[i]]))
else:
sp.append(float(str[pos[i - 1] + 1:pos[i]]))
# print(sp)
return sp
def Calculate_Performance_Index(self, sp, codes):
HU = 0
l_avr = 0
for i in range(len(sp)):
HU += -sp[i] * math.log2(sp[i])
for i in range(len(sp)):
l_avr += sp[i] * len(codes[i])
effi = HU / l_avr / math.log2(2)
self.ui.lineEdit_2.setText(str(round(HU, 5)))
self.ui.lineEdit_3.setText(str(round(l_avr, 2)))
self.ui.lineEdit_4.setText(str(round(effi * 100, 4)) + '%')
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
mywindow = Mywindow()
mywindow.show()
sys.exit(app.exec_())