Huffman编码(哈夫曼编码)的Matlab实现

上传人:cn****1 文档编号:513668411 上传时间:2024-01-07 格式:DOC 页数:8 大小:23.50KB
返回 下载 相关 举报
Huffman编码(哈夫曼编码)的Matlab实现_第1页
第1页 / 共8页
Huffman编码(哈夫曼编码)的Matlab实现_第2页
第2页 / 共8页
Huffman编码(哈夫曼编码)的Matlab实现_第3页
第3页 / 共8页
Huffman编码(哈夫曼编码)的Matlab实现_第4页
第4页 / 共8页
Huffman编码(哈夫曼编码)的Matlab实现_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《Huffman编码(哈夫曼编码)的Matlab实现》由会员分享,可在线阅读,更多相关《Huffman编码(哈夫曼编码)的Matlab实现(8页珍藏版)》请在金锄头文库上搜索。

1、clearallprintf(Rding data.)dat=iread(cameama.ti);data=ut8(a);%读入数据,并将数据限制为uit8fprintf(Done!n)%编码压缩prnt(comressinga.);zipd,infonom2huf(data);printf(Don!n)%解压缩fprint(compessing data.);unippedhuf2norm(zippe,nfo);fpnt(Done!n)%测试与否无失真isOK=sequl(data(:),unipd(:))%显示压缩效果who ata zipedunzppe unction zipe,fo=

2、norm2huff(eco)ifisa(veco,uint8), error(ipu aruntmust be a uin ector)ndveor=ectr(:);%将输入向量转换为行向量fequny(vcto);计算个元素浮现旳概率simbls=fid(f);=f(smbols);%将元素按浮现旳概率排列f,sotinde=ot();simbols=simbls(sortiex);%产生码字geerat thoor as th52 bits of a dubleen=engt(imbl);mbos_dxnum2cll(1:ln);codewd_tmp=e(len,);whie lngth(

3、), indxsbl_idex1; idex=simos_idex2; codordtmp(indx1)=adnode(codewodtmp(indx1),uin(0); cdeword_tmp(inex2)=addnod(odewrd_tmp(indx2),uint(); f=sm(f(1:2)(:nd); ibols_dex=index1indx2 simbols_idex(:e); 将数据重新排列,是两个节点旳频率尽量与前一种节点旳频率想当 rsordata in rdert hav two node withlowerfrquency as first f,sortidex=srt(f

4、); simbs_index=ilindex(sortindex);en%相应相应旳元素与码字cword=cell(256:);odeword(smbols)=codword_tp;计算总旳字符串长度len=0;for nde:ent(vecto), lenlnlnth(odeworddouble(vcto(inex)+1);en%产生1序列rn=repmat(uint(),,n);pnte=1;oriex=:leng(ecto), code=codewddouble(ctr(ndex))+1; nength(od); sti(piner+(:le-1))=code; ointr=ointe+

5、ln;end%如果需要,加零len=leth(strng);pad=-od(ln,8);if pad0, string=tringun8(zeros(1,ad);nd%保存实际有用旳码字cewod=codewor(simb);colen=zeo(size(coeword);eghts=2.(0:);maxcodeen=;forindex 1:lth(oword), lnengh(odeordndex); if lnmacodln, maxcodlen=en; ed ilen, code=sum(igts(oewrnde=1); cod=bise(cde,len+1); cwordidx=cde

6、; oelen(ine)=len; endendcodeword=oword:计算压缩后旳向量coslengt(ng)/8;stngshae(string,8,cols);weights=2(0:7);zippeduint8(wigts*doubl(strng);%存储一种稀疏矩阵uffdes=sarse(1,1);% init paeatrixor ndex:numel(owor), hufoes(owor(index),1)=simbols(ine);nd%产生信息构造体inf.ad=pad;inforatiocl./lngth(vector);nfo.lengh=length(vecto

7、r);info.macoele=macodelen; ucti codewod_new=adnode(coeword_,ite)odeword_new=cell(ze(codeod_old);for index=1:lngh(codeword_ld), cord_newiex=tem odeword_oldidex;end functin ctorhuffnorm(zipped,nfo)%HUFF2OR fmn 解码器%HUFF2NORM(X,NFO)根据信息体构造ifo 返回向量 zpd 旳解码成果%矩阵参数以X(:)形式输入iia(pped,uint8), eror(npu aument

8、st b ant8vctor)nd%产生0序列n=lengt(ippd);tigrpmat(ui8(0),n.*8);btndex=1:8;or id+1:ln, string(bitndex+8.(ide-1)=uit8(iget(zpped(nex),inex));end调节字符串srin=logical(sring(:);%remo 0paddnglen=legh(strig);%解码wigh=2(0:1);etor=rpmat(uint8(0),1,info,lengt);vcorex;codeindex=;code=0;for idex:le, code=btset(ce,codei

9、dx,string(inde); odindex=codeindex+1; byte=code(biset(cod,cdendex),ifo); f byte0, etor(veoinde)byt1; odendex=1; cod=0; ectindevectoinex+; ndnd uctin by=dco(cde,info)bytenfohuffcode(de);ctin f=frequeny(ecto)FREQUENCY计算元素浮现概率ifsa(vecor,int8), erro(iput argumntust be a uit8 vetor)ndf=rema(0,1,56);%扫描向量ln=lenh(ctr);foridex=:25, (idex+1)=um(ctor=uint8(index);e%归一化f=f/len;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 活动策划

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号