Huffman编码(哈夫曼编码)的Matlab实现(共4页).doc
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《Huffman编码(哈夫曼编码)的Matlab实现(共4页).doc》由会员分享,可在线阅读,更多相关《Huffman编码(哈夫曼编码)的Matlab实现(共4页).doc(4页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上clear allfprintf(Reading data.)data=imread(cameraman.tif);data=uint8(data);%读入数据,并将数据限制为uint8fprintf(Done!n)%编码压缩fprintf(compressing data.);zipped,info=norm2huff(data);fprintf(Done!n)%解压缩fprintf(compressing data.);unzipped=huff2norm(zipped,info);fprintf(Done!n)%测试是否无失真isOK=isequal(data(
2、:),unzipped(:)%显示压缩效果whos data zipped unzipped function zipped,info=norm2huff(vector)ifisa(vector,uint8), error(input argument must be a uint8 vector)endvector=vector(:);%将输入向量转换为行向量f=frequency(vector);%计算个元素出现的概率simbols=find(f=0);f=f(simbols);%将元素按出现的概率排列f,sortindex=sot(f);simbols=simbols(sortindex
3、);%产生码字 generate the codeword as the 52 bits of a doublelen=length(simbols);simbols_index=num2cell(1:len);codeword_tmp=cell(len,1);while length(f)1, index1=simbols_index1; index2=simbols_index2; codeword_tmp(index1)=addnode(codeword_tmp(index1),uint8(0); codeword_tmp(index2)=addnode(codeword_tmp(ind
4、ex2),uint8(1); f=sum(f(1:2) f(3:end); simbols_index=index1 index2 simbols_index(3:end); %将数据重新排列,是两个节点的频率尽量与前一个节点的频率想当 resort data in order to have two nodes with lower frequency as first to f,sortindex=sort(f); simbols_index=simbols_index(sortindex);end%对应相应的元素与码字codeword=cell(256:1);codeword(simbo
5、ls)=codeword_tmp;%计算总的字符串长度len=0;for index=1:length(vector), len=len+length(codeworddouble(vector(index)+1);end%产生01序列string=repmat(uint8(0),1,len);pointer=1;for index=1:length(vector), code=codeworddouble(vector(index)+1; len=length(code); string(pointer+(0:len-1)=code; pointer=pointer+len;end%如果需要
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Huffman 编码 哈夫曼 Matlab 实现
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内