这是啥语言啊?看不懂。
试着解释一下吧:
function huff() // 函数名?
clc; // ?
fid=fopen('seq1.txt','r'); // 打开文本文件seq1.txt?
seq=fread(fid,'*char'); // 将seq1.txt的内容读到seq中?
fclose(fid); // 关闭打开的文件seq1.txt
seq=reshape(seq,1,length(seq)); // 调用reshape处理seq,得到新的seq?
[alpha prob]=probmodel(seq); // 调用probmodel处理seq, 得到alpha, prob?
if ~isempty(alpha) // alpha 是否empty? ?
[huf entropy avglength redundancy]=huffman(alpha,prob);
// if not empty, 对输入alpha, prob 调用huffman,得到huf, entropy, avglength,
// redundancy?
if ~isempty(huf) // huf 是否 empty?
lp=length(prob); // lp = prob的元素数目
for i=1:lp // for 循环
str=huf(i).sym; // str = huf(i)表示的symbol, 一个字符? 例如A
str=strcat(str,' :'); // str = "A:"
str=strcat(str,num2str(huf(i).prob)); // huf(i).prob为字符A的的出现概率,
// 比如0.12,则str = "A:0.12"
str=strcat(str,' :'); // str = "A:0.12:"
str=strcat(str,huf(i).code); // huf(i).code为字符A对应的huffman码字,
// 该码字很可能是类似如010101011的字符串。
disp(str); // disp "A:0.12:010101011" ?
end // for 循环体结束标志
disp(strcat('Entropy = ',num2str(entropy))); // entropy是个数值,熵, disp Entropy = xxx?
disp(strcat('Average length = ',num2str(avglength))); // disp Average length = xxx?
disp(strcat('Redundancy = ',num2str(redundancy))); // disp Redundancy = xxx? 冗余度?
encseq=huffencode(huf,seq); // 对输入huf,seq调用huffencode得到huffman编码的encseq?
disp('Sequence :'); // disp Sequence:
disp(seq); // disp seq.
disp('Encoded Sequence :'); // disp Encoded Sequence :
disp(encseq); // disp encseq?
decseq=huffdecode(huf,encseq); // 对输入huf, encseq调用huffdecode得到解码后的decseq?
disp('Decoded Sequence :'); // disp Decoded Sequence :
disp(decseq); // disp decseq? 此处的decseq应该
// 等于 "seq = fread(fid,'*char')"里的seq?
end
else
display('Empty Sequence....');
end //
end
整体的思路类似如下:
用Huffman压缩一个seq1.txt的文本文件,首先统计每个字符出现的概率,然后根据字符出现的概率进行Huffman编码,然后得到一个编码后的encseq,然后对这个encseq进行Huffman解码得到decseq,
没问题的话decseq应该等于seq1.txt里的内容。
|