您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > Huffman编码(哈夫曼编码)的Matlab实现
clearallfprintf('Readingdata...')data=imread('cameraman.tif');data=uint8(data);%读入数据,并将数据限制为uint8fprintf('Done!\n')%编码压缩fprintf('compressingdata...');[zipped,info]=norm2huff(data);fprintf('Done!\n')%解压缩fprintf('compressingdata...');unzipped=huff2norm(zipped,info);fprintf('Done!\n')%测试是否无失真isOK=isequal(data(:),unzipped(:))%显示压缩效果whosdatazippedunzippedfunction[zipped,info]=norm2huff(vector)if~isa(vector,'uint8'),error('inputargumentmustbeauint8vector')endvector=vector(:)';%将输入向量转换为行向量f=frequency(vector);%计算个元素出现的概率simbols=find(f~=0);f=f(simbols);%将元素按出现的概率排列[f,sortindex]=sot(f);simbols=simbols(sortindex);%产生码字generatethecodewordasthe52bitsofadoublelen=length(simbols);simbols_index=num2cell(1:len);codeword_tmp=cell(len,1);whilelength(f)1,index1=simbols_index{1};index2=simbols_index{2};codeword_tmp(index1)=addnode(codeword_tmp(index1),uint8(0));codeword_tmp(index2)=addnode(codeword_tmp(index2),uint8(1));f=[sum(f(1:2))f(3:end)];simbols_index=[{[index1index2]}simbols_index(3:end)];%将数据重新排列,是两个节点的频率尽量与前一个节点的频率想当resortdatainordertohavetwonodeswithlowerfrequencyasfirstto[f,sortindex]=sort(f);simbols_index=simbols_index(sortindex);end%对应相应的元素与码字codeword=cell(256:1);codeword(simbols)=codeword_tmp;%计算总的字符串长度len=0;forindex=1:length(vector),len=len+length(codeword{double(vector(index))+1});end%产生01序列string=repmat(uint8(0),1,len);pointer=1;forindex=1:length(vector),code=codeword{double(vector(index))+1};len=length(code);string(pointer+(0:len-1))=code;pointer=pointer+len;end%如果需要,加零len=length(string);pad=8-mod(len,8);ifpad0,string=[stringuint8(zeros(1,pad))];end%保存实际有用的码字codeword=codeword(simbols);codelen=zeros(size(codeword));weights=2.^(0:23);maxcodelen=0;forindex1:length(codeword),len=length(codeword{index});iflenmaxcodelen,maxcodelen=len;endiflen0,code=sum(weights(codeword{index}==1));code=bitset(code,len+1);codeword{index}=code;codelen(index)=len;endendcodeword=[codeword{:}]%计算压缩后的向量cols=length(string)/8;string=reshape(string,8,cols);weights=2.^(0:7);zipped=uint8(weights*double(string));%存储一个稀疏矩阵huffcodes=sparse(1,1);%initsparsematrixforindex=1:numel(codeword),huffcodes(codeword(index),1)=simbols(index);end%产生信息结构体info.pad=pad;info.ratio=cols./length(vector);info.length=length(vector);info.maxcodelen=maxcodelen;functioncodeword_new=addnode(codeword_old,item)codeword_new=cell(size(codeword_old));forindex=1:length(codeword_old),codeword_new{index}=[itemcodeword_old{index}];endfunctionvector=huff2norm(zipped,info)%HUFF2NORMHuffman解码器%HUFF2NORM(X,INFO)根据信息体结构info返回向量zipped的解码结果%%矩阵参数以X(:)形式输入if~isa(zipped,'uint8'),error('inputargumentmustbeauint8vector')end%产生01序列len=length(zipped);string=repmat(uint8(0),1,len.*8);bitindex=1:8;forindex+1:len,string(bitindex+8.*(index-1))=uint8(bitget(zipped(index),bitindex));end%调整字符串string=logical(string(:)');%remove0paddinglen=length(string);%解码weights=2.^(0:51);vector=repmat(uint8(0),1,info,length);vectorindex=1;codeindex=1;code=0;forindex=1:len,code=bitset(code,codeindex,string(index));]codeindex=codeindex+1;byte=decode(bitset(code,codeindex),info);ifbyte0,%vector(vectorindex)=byte-1;codeindex=1;code=0;vectorindex=vectorindex+1;endendfunctionbyte=decode(code,info)byte=info.huffcodes(code);functionf=frequency(vector)%FREQUENCY计算元素出现概率if~isa(vector,'uint8'),error('inputargumentmustbeauint8vector')endf=repmat(0,1,256);%扫描向量len=length(vector);forindex=0:256,%f(index+1)=sum(vector==uint8(index));end%归一化f=f./len;
本文标题:Huffman编码(哈夫曼编码)的Matlab实现
链接地址:https://www.777doc.com/doc-5169377 .html