Huffman編碼(哈夫曼編碼)的Matlab實現(xiàn)_第1頁
Huffman編碼(哈夫曼編碼)的Matlab實現(xiàn)_第2頁
Huffman編碼(哈夫曼編碼)的Matlab實現(xiàn)_第3頁
已閱讀5頁,還剩2頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領

文檔簡介

第PAGE第7頁,共7頁【】clear 1【】allfprintf('Readingdata...')data=imread('cameraman.tif');data=uint8(data);%讀入數(shù)據(jù),并將數(shù)據(jù)限制為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(:)';%將輸入向量轉(zhuǎn)換為行向量f=frequency(vector);%盤算個元素消失的概率simbols=find(f~=0);f=f(simbols);%將元素按消失的概率分列[f,sortindex]=sot(f);simbols=simbols(sortindex);%產(chǎn)生碼字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%產(chǎn)生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;endpad>0,string=[stringuint8(zeros(1,pad))];end%保管現(xiàn)實有效的碼字codeword=codeword(simbols);codelen=zeros(size(codeword));weights=2.^(0:23);maxcodelen=0;forindex1:length(codeword),len=length(codeword{index});iflen>maxcodelen,maxcodelen=len;endiflen>0,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%產(chǎn)生信息構(gòu)造體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)依據(jù)信息體構(gòu)造info返回向量zipped的解碼成果%%矩陣參數(shù)以X(:)情勢輸入if~isa(zipped,'uint8'),error('inputargumentmustbeauint8vector')end%產(chǎn)生01序列l(wèi)en=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%調(diào)劑字符串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);ifbyte>0,%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

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論