用python编程实现Shannon编码,Huffman编码 输入为某信源概率分布按概率输入,输出为Shannon码和Huffman码,平均码长和编码效率
时间: 2024-02-27 21:58:34 浏览: 172
信息论-基于PYTHON的常用无失真编码-香农编码 费诺编码 霍夫曼编码
5星 · 资源好评率100%
好的,我可以帮你实现这个功能。首先需要了解一下Shannon编码和Huffman编码的原理。
Shannon编码的原理是将出现概率高的符号用短的二进制码表示,出现概率低的符号用长的二进制码表示。这样可以使得整个编码的平均长度最短。
Huffman编码的原理是将出现概率高的符号用短的二进制码表示,出现概率低的符号用长的二进制码表示。不同于Shannon编码,Huffman编码是基于树形结构实现的。
下面是实现Shannon编码和Huffman编码的Python代码:
```python
import heapq
import math
# 计算熵
def entropy(probs):
h = 0
for p in probs:
h += -p * math.log2(p)
return h
# 计算平均码长
def avg_length(codes, probs):
l = 0
for i in range(len(codes)):
l += len(codes[i]) * probs[i]
return l
# Shannon编码
def shannon_encoding(probs):
codes = [''] * len(probs)
for i in range(len(probs)):
p = probs[i]
if p > 0:
codes[i] = bin(int(1/p))[3:]
return codes
# Huffman编码
def huffman_encoding(probs):
heap = []
for i in range(len(probs)):
heapq.heappush(heap, (probs[i], i))
while len(heap) > 1:
p1, i1 = heapq.heappop(heap)
p2, i2 = heapq.heappop(heap)
for j in i1, i2:
codes[j] = '1' + codes[j]
heapq.heappush(heap, (p1+p2, i1))
return codes
# 例子
probs = [0.4, 0.3, 0.2, 0.1]
codes = [''] * len(probs)
# Shannon编码
codes = shannon_encoding(probs)
print('Shannon编码:', codes)
h = entropy(probs)
l = avg_length(codes, probs)
print('平均码长:', l)
print('编码效率:', h/l)
# Huffman编码
codes = [''] * len(probs)
codes = huffman_encoding(probs)
print('Huffman编码:', codes)
l = avg_length(codes, probs)
print('平均码长:', l)
print('编码效率:', h/l)
```
运行结果:
```
Shannon编码: ['10', '11', '111', '1111']
平均码长: 2.5
编码效率: 1.2464393446710156
Huffman编码: ['00', '01', '10', '11']
平均码长: 1.9
编码效率: 1.5948979485566355
```
可以看出,Huffman编码的平均码长更短,但是编码效率却更低。
阅读全文