添加huffman压缩编码,已完成编码,未验证
This commit is contained in:
@@ -115,6 +115,8 @@
|
|||||||
updata_ssh.py 添加jwt模块程序文件下载
|
updata_ssh.py 添加jwt模块程序文件下载
|
||||||
2023.11.18
|
2023.11.18
|
||||||
注码工具添加写缓存信息功能
|
注码工具添加写缓存信息功能
|
||||||
|
2023.11.21
|
||||||
|
分析工具修改上下限时自动重绘
|
||||||
|
添加huffman压缩编码,已完成编码,未验证
|
||||||
|
|
||||||
|
|
||||||
|
@@ -467,22 +467,17 @@ class protu(QObject):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
def int2str(num:int):
|
|
||||||
s=str(num//100)
|
|
||||||
s=s+str(num%100//10)
|
|
||||||
s=s+str(num%10)
|
|
||||||
return s
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
# if __name__ == "__main__":
|
||||||
u=protu()
|
# u=protu()
|
||||||
# u.init("utcp:7777")
|
# u.init("utcp:7777")
|
||||||
# u.send_file(0xee,"file/JQ_JCXB_V54.bin")
|
# u.send_file(0xee,"file/JQ_JCXB_V54.bin")
|
||||||
# u.send_file(0xed,"../Objects/checker_gen1_app_20230602.bin")
|
# u.send_file(0xed,"../Objects/checker_gen1_app_20230602.bin")
|
||||||
|
|
||||||
# 设置电阻 矫正值
|
# 设置电阻 矫正值
|
||||||
u.cmd=0x41
|
# u.cmd=0x41
|
||||||
data=bytearray([1,0,0x00,2,0,0x00,3,0,0x00,4,0,0x00,5,0,0x00,6,0,0x00,7,0,0x00,8,0,0x00,9,0,0x00,10,0,0x00,11,0,0x00,12,0,0x00,13,0,0x00,14,0,0x00,15,0,0x00,16,0,0x00,17,0,0x00,18,0,0x00,19,0,0x00,20,0,0x00])
|
# data=bytearray([1,0,0x00,2,0,0x00,3,0,0x00,4,0,0x00,5,0,0x00,6,0,0x00,7,0,0x00,8,0,0x00,9,0,0x00,10,0,0x00,11,0,0x00,12,0,0x00,13,0,0x00,14,0,0x00,15,0,0x00,16,0,0x00,17,0,0x00,18,0,0x00,19,0,0x00,20,0,0x00])
|
||||||
# 测量电阻
|
# 测量电阻
|
||||||
# u.cmd=0x42
|
# u.cmd=0x42
|
||||||
# data=bytearray([0])
|
# data=bytearray([0])
|
||||||
@@ -495,7 +490,7 @@ if __name__ == "__main__":
|
|||||||
# data=bytearray([0x02]) # 上升
|
# data=bytearray([0x02]) # 上升
|
||||||
# data=bytearray([0x03]) # 下降
|
# data=bytearray([0x03]) # 下降
|
||||||
|
|
||||||
print(u.encode(data).hex(' '))
|
# print(u.encode(data).hex(' '))
|
||||||
# with open("file/EX_Coder_Test_2023-07-6.json","rb") as f:
|
# with open("file/EX_Coder_Test_2023-07-6.json","rb") as f:
|
||||||
# json_obj=json.loads(f.read())
|
# json_obj=json.loads(f.read())
|
||||||
# d=scheme_to_byte(json_obj)
|
# d=scheme_to_byte(json_obj)
|
||||||
@@ -518,3 +513,115 @@ if __name__ == "__main__":
|
|||||||
# 59 6D 03 00 31 00 00 31 CF
|
# 59 6D 03 00 31 00 00 31 CF
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def sort_table(index_table):
|
||||||
|
for i in range(len(index_table)):
|
||||||
|
item=index_table[i]
|
||||||
|
for j in range(i+1,len(index_table)):
|
||||||
|
if(index_table[j][1]>item[1]):
|
||||||
|
index_table[i]=index_table[j]
|
||||||
|
index_table[j]=item
|
||||||
|
item=index_table[i]
|
||||||
|
|
||||||
|
|
||||||
|
def exp(num:int,e:int):
|
||||||
|
res=1
|
||||||
|
for i in range(e):
|
||||||
|
res*=num
|
||||||
|
return res
|
||||||
|
|
||||||
|
def clac_zl_table(num:int):
|
||||||
|
count=1
|
||||||
|
while(num>exp(2,count)):
|
||||||
|
count+=1
|
||||||
|
# 使用短编码的个数
|
||||||
|
diff=exp(2,count)-num
|
||||||
|
zl_table=[]
|
||||||
|
for i in range(diff):
|
||||||
|
zl_table.append(i)
|
||||||
|
# 使用长编码的个数
|
||||||
|
diff2=num-exp(2,count-1)
|
||||||
|
for i in range(diff,diff+diff2):
|
||||||
|
zl_table.append(i*2)
|
||||||
|
zl_table.append(i*2+1)
|
||||||
|
print(zl_table)
|
||||||
|
return zl_table,count,diff
|
||||||
|
|
||||||
|
def huffman(data:bytearray):
|
||||||
|
count_table=bytearray(256)
|
||||||
|
for i in data:
|
||||||
|
count_table[int(i)]+=1
|
||||||
|
index_table=[]
|
||||||
|
for i in range(len(count_table)):
|
||||||
|
if(int(count_table[i])>0):
|
||||||
|
index_table.append((i,count_table[i]))
|
||||||
|
# print(index_table)
|
||||||
|
sort_table(index_table)
|
||||||
|
# print(index_table)
|
||||||
|
num=len(index_table)
|
||||||
|
# clac_zl_table(num)
|
||||||
|
ret=bytearray()
|
||||||
|
for i in index_table:
|
||||||
|
ret.append(i[0])
|
||||||
|
return ret
|
||||||
|
|
||||||
|
|
||||||
|
# 根据值获取其序号
|
||||||
|
def value_to_index(index_table:list,value):
|
||||||
|
for i in range(len(index_table)):
|
||||||
|
if(value==index_table[i]):
|
||||||
|
return i
|
||||||
|
return -1
|
||||||
|
|
||||||
|
|
||||||
|
# 生成指定位数的mask
|
||||||
|
def calc_mask(num:int):
|
||||||
|
ret=0
|
||||||
|
for i in range(num):
|
||||||
|
ret<<=1
|
||||||
|
ret|=1
|
||||||
|
return ret
|
||||||
|
|
||||||
|
|
||||||
|
# 把指定bit位数的数据添加进数组 bit_count<8
|
||||||
|
def array_add_bit(data:bytearray,bit_index:int,dat:int,bit_count:int):
|
||||||
|
off=bit_index%8
|
||||||
|
data[bit_index//8]|=((calc_mask(bit_count)&dat)<<off)&0xff
|
||||||
|
data[bit_index//8+1]|=((calc_mask(bit_count)&dat)>>(8-off))&0xff
|
||||||
|
bit_index+=bit_count
|
||||||
|
return data,bit_index
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def huffman_encode(data:bytearray):
|
||||||
|
ret=bytearray(1024)
|
||||||
|
index=0
|
||||||
|
index_table=huffman(data)
|
||||||
|
zl_table,count,diff=clac_zl_table(len(index_table))
|
||||||
|
print(index_table)
|
||||||
|
print(zl_table)
|
||||||
|
print(count,diff)
|
||||||
|
for i in data:
|
||||||
|
pos=value_to_index(index_table,i)
|
||||||
|
mask_bit_count=count
|
||||||
|
if(pos<diff):
|
||||||
|
mask_bit_count-=1
|
||||||
|
ret,index=array_add_bit(ret,index,zl_table[pos],mask_bit_count)
|
||||||
|
print(ret[0:(index+7)//8].hex(' '))
|
||||||
|
ret=bytearray([len(index_table)])+index_table+bytearray([8-index%8])+ret[0:(index+7)//8]
|
||||||
|
print(ret.hex(' '))
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def huffman_decode(data:bytearray):
|
||||||
|
zl_table,count,diff=clac_zl_table(data[0])
|
||||||
|
index_table=data[1:data[0]+1]
|
||||||
|
data=data[data[0]+1:]
|
||||||
|
index_all=len(data[1:])*8-data[0]
|
||||||
|
ret=bytearray()
|
||||||
|
index=0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
huffman_encode(b"1234567891234233434237873678378263738")
|
Reference in New Issue
Block a user