构造二元的Huffman编码器和译码器(最好用matlab编码)

来源:百度知道 编辑:UC知道 时间:2024/06/15 14:16:37
字符集合s={s1,s2,s3,s4,s5,s6,s7};p={0.20,0.19,0.18,0.17,0.15,0.10,0.01}(其中,p 为s中各信源出现的频率)。用上述信源概率分布构造二元的Huffman编码器和译码器。

以前编的一个霍夫曼编码,你可以参考一下:
% b1; %%%输入代码中出现的字符,按ASCII排序
% m1; %%%b1中字符在codes中首次出现的位置
% n1; %%%codes中字符在b1中出现的位置
function coding_callback(Incodes)
global decod
clc;
digits(16);%%设置输出编码的小数点位数
Incodes=input('please input the encoding string:\n','s');
Incodes=['state tree'];
[b1, m1, n1] = unique(Incodes, 'first');
Incodes(sort(m1));
length(b1);
for ii=1:length(b1)
a(ii)=length(find(n1(1:end)==ii));
rate(ii)=a(ii)/length(n1); %%%b1中每个字符出现的概率区间
low1(1)=0; %%%b1区间下限
high1(1)=rate(1); %%%b1区间上限
if ii>1
low1(ii)=high1(ii-1);
high1(ii)=low1(ii)+rate(ii);
end
end
% high1 %%%每个字符的上限
% low1 %%%每个字符的下限
% rate %%%每个字符所占有比例
%%%%%编码过程%%%%%%
judge=n1(1);
rang(1)=high1(judge)-low1(judge);