版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
……装………订…………线……………装………订…………线………专业班级信息09-1学号姓名成绩一、实验目的1、学习Matlab软件的使用和编程2、进一步深入理解LZW编码算法的原理二、实验内容1、使用MATLAB软件编写程序将“/WED/WE/WEE/WEB/WET”进行编译码。三、实验原理LZW算法中,首先建立一个字符串表,把每一个第一次出现的字符串放入串表中,并用一个数字来表示,这个数字与此字符串在串表中的位置有关,并将这个数字存入压缩文件中,如果这个字符串再次出现时,即可用表示它的数字来代替,并将这个数字存入文件中。压缩完成后将串表丢弃。如"print"字符串,如果在压缩时用266表示,只要再次出现,均用266表示,并将"print"字符串存入串表中,在图象解码时遇到数字266,即可从串表中查出266所代表的字符串"print",在解压缩时,串表可以根据压缩数据重新生成。四、LZW编码的Matlab源程序及运行结果%FromthearticlereferencedbytheoriginalauthorsP='/WED/WE/WEE/WEB/WET';%lzwInput=uint8('/WED/WE/WEE/WEB/WET');lzwInput=uint8(P);[lzwOutput,lzwTable]=norm2lzw(lzwInput);fprintf('\n');fprintf('Input:');%disp(P);fprintf('%02x',lzwInput);fprintf('\n');fprintf('Output:');fprintf('%02x',lzwOutput);fprintf('\n');forii=257:length(lzwTable.codes) fprintf('Code:%02x,LastCode%02x+%02xLength%3d\n',ii,lzwTable.codes(ii).lastCode,...lzwTable.codes(ii).c,lzwTable.codes(ii).codeLength)end;%TeststhespecialdecodercaseP='Anotherproblemonlongfilesisthatfrequentlythecompressionratiobegins...';%'todegradeasmoreofthefileisreadin.Thereasonforthisissimple.Since...';%'thestringtableisoffinitesize,afteracertainnumberofstringshavebeen...';%'defined,nomorecanbeadded.Butthestringtableisonlygoodfortheportion...';%'ofthefilethatwasreadinwhileitwasbuilt.Latersectionsofthefilemay...';%'havedifferentcharacteristics,andreallyneedadifferentstringtable.The...';%'conventionalwaytosolvethisproblemistomonitorthecompressionratio.After...';%'thestringtableisfull,thecompressorwatchestoseeifthecompressionratio...';%'degrades.Afteracertainamountofdegradation,theentiretableisflushed,and...';%'getsrebuiltfromscratch.Theexpansioncodeisflaggedwhenthishappensbyseeing...';%'aspecialcodefromthecompressionroutine.Analternativemethodwouldbetokeep...';%'trackofhowfrequentlystringsareused,andtoperiodicallyflushvaluesthatare...';%'rarelyused.Anadaptivetechniquelikethismaybetoodifficulttoimplementina...';%'reasonablysizedprogram.Onefinaltechniqueforcompressingthedataistotakethe...';%'LZWcodesandrunthemthroughanadaptiveHuffmancodingfilter.Thiswillgenerally...';%'exploitafewmorepercentagepointsofcompression,butatthecostofconsiderable...';%'morecomplexityinthecode,aswellasquiteabitmoreruntime.';lzwInput=uint8(P);[lzwOutput,lzwTable]=norm2lzw(lzwInput);fprintf('\n');fprintf('Input:');fprintf('%02x',lzwInput);%disp(P);fprintf('\n');fprintf('Output:');fprintf('%02x',lzwOutput);fprintf('\n');forii=257:length(lzwTable.codes) fprintf('Code:%02x,LastCode%02x+%02xLength%3d\n',ii,lzwTable.codes(ii).lastCode,lzwTable.codes(ii).c,lzwTable.codes(ii).codeLength)end;[lzwOutputd,lzwTabled]=lzw2norm(lzwOutput);fprintf('\n');fprintf('Input:');fprintf('%02x',lzwOutput);fprintf('\n');fprintf('Output:');fprintf('%02x',lzwOutputd);P1=char(lzwOutputd);%disp(P1);fprintf('\n');forii=257:length(lzwTabled.codes) fprintf('Code:%02x,LastCode%02x+%02xLength%3d\n',ii,lzwTabled.codes(ii).lastCode,lzwTabled.codes(ii).c,lzwTabled.codes(ii).codeLength)end;function[output,table]=lzw2norm(vector,maxTableSize,restartTable)%LZW2NORMLZWDataCompression(decoder)%Forvectors,LZW2NORM(X)istheuncompressedvectorofXusingtheLZWalgorithm.%[...,T]=LZW2NORM(X)returnsalsothetablethatthealgorithmproduces.%%Formatrices,X(:)isusedasinput.%%maxTableSizecanbeusedtosetamaximumlengthofthetable.Default%is4096entries,useInfforunlimited.Usualsizesare12,14and16%bits.%% IfrestartTableisspecified,thenthetableisflushedwhenitreaches% itsmaximumsizeandanewtableisbuilt.%%Inputmustbeofuint16type,whiletheoutputisauint8.%Tableisacellarray,eachelementcontainigthecorrespondingcode.%%Thisisanimplementationofthealgorithmpresentedinthearticle%%SeealsoNORM2LZW%$Author:GiuseppeRidino'$%$Revision:1.0$$Date:10-May-200414:16:08$%Howitdecodes:%%ReadOLD_CODE%outputOLD_CODE%CHARACTER=OLD_CODE%WHILEtherearestillinputcharactersDO%ReadNEW_CODE%IFNEW_CODEisnotinthetranslationtableTHEN%STRING=gettranslationofOLD_CODE%STRING=STRING+CHARACTER%ELSE%STRING=gettranslationofNEW_CODE%ENDofIF%outputSTRING%CHARACTER=firstcharacterinSTRING%addtranslationofOLD_CODE+CHARACTERtothetranslationtable%OLD_CODE=NEW_CODE%ENDofWHILE%Ensuretohandleuint8inputvectorandconvert%toarowif~isa(vector,'uint16'), error('inputargumentmustbeauint16vector')Endvector=vector(:)';if(nargin<2) maxTableSize=4096; restartTable=0;end;if(nargin<3) restartTable=0;end; functioncode=findCode(lastCode,c) %Lookupcodevalue %if(isempty(lastCode)) % fprintf('findCode:----+%02x=',c); %else % fprintf('findCode:%04x+%02x=',lastCode,c); %end; if(isempty(lastCode)) code=c+1; %fprintf('%04x\n',code); return; Else ii=table.codes(lastCode).prefix; jj=find([table.codes(ii).c]==c); code=ii(jj); % if(isempty(code)) % fprintf('----\n'); % else % fprintf('%04x\n',code); % end; return; end; End function[]=addCode(lastCode,c) %Addanewcodetothetable e.c=c; %NBusingvariableinparenttoavoidallocationcost e.lastCode=lastCode; e.prefix=[]; e.codeLength=table.codes(lastCode).codeLength+1; table.codes(table.nextCode)=e; table.codes(lastCode).prefix=[table.codes(lastCode).prefixtable.nextCode]; table.nextCode=table.nextCode+1; %if(isempty(lastCode)) % fprintf('addCode:----+%02x=%04x\n',c,table.nextCode-1); %else % fprintf('addCode:%04x+%02x=%04x\n',lastCode,c,table.nextCode-1); %end; End functionstr=getCode(code) %Outputthestringforacode l=table.codes(code).codeLength; str=zeros(1,l); forii=l:-1:1 str(ii)=table.codes(code).c; code=table.codes(code).lastCode; end; End function[]=newTable %Buildtheinitialtableconsistingofallcodesoflength1.Thestrings %arestoredasprefixCode+character,sothattestingisveryquick.To %speedupsearching,westorealistofcodesthateachcodeistheprefix %for. e.c=0; e.lastCode=-1; e.prefix=[]; e.codeLength=1; table.nextCode=2; if(~isinf(maxTableSize)) table.codes(1:maxTableSize)=e;%Pre-allocateforspeed Else table.codes(1:65536)=e;%Pre-allocateforspeed end; forc=1:255 e.c=c; e.lastCode=-1; e.prefix=[]; e.codeLength=1; table.codes(table.nextCode)=e; table.nextCode=table.nextCode+1; end; End%%Mainloop%e.c=0;e.lastCode=-1;e.prefix=[];e.codeLength=1;newTable;output=zeros(1,3*length(vector),'uint8');%assumecompressionof33%outputIndex=1;lastCode=vector(1);output(outputIndex)=table.codes(vector(1)).c;outputIndex=outputIndex+1;character=table.codes(vector(1)).c;、、tic;forvectorIndex=2:length(vector), % ifmod(vectorIndex,1000)==0 % fprintf('Index:%5d,Time%.1fs,TableLength%4d,Complete%.1f%%\n',outputIndex,toc,table.nextCode-1,vectorIndex/length(vector)*100);%*ceil(log2(size(table,2)))/8); % tic; % end; element=vector(vectorIndex); if(element>=table.nextCode) %addcodesnotintable,aspecialcase. str=[getCode(lastCode)character]; else, str=getCode(element); End output(outputIndex+(0:length(str)-1))=str; outputIndex=outputIndex+length(str); if((length(output)-outputIndex)<1.5*(length(vector)-vectorIndex)) output=[outputzeros(1,3*(length(vector)-vectorIndex),'uint8')]; end; if(length(str)<1) keyboard; end; character=str(1); if(table.nextCode<=maxTableSize) addCode(lastCode,character); if(restartTable&&table.nextCode==maxTableSize+1) %fprintf('Newtable\n'); newTable; end; end; lastCode=element;end;output=output(1:outputIndex-1);table.codes=table.codes(1:table.nextCode-1);Endfunction[output,table]=norm2lzw(vector,maxTableSize,restartTable)%NORM2LZWLZWDataCompressionEncoder%Forvectors,NORM2LZW(X)isthecompressedvectorofXusingtheLZWalgorithm.%[...,T]=NORM2LZW(X)returnsalsothetablethatthealgorithmproduces.%Formatrices,X(:)isusedasinput.%maxTableSizecanbeusedtosetamaximumlengthofthetable.Default%is4096entries,useInfforunlimited.Usualsizesare12,14and16%bits.% IfrestartTableisspecified,thenthetableisflushedwhenitreaches% itsmaximumsizeandanewtableisbuilt.%Inputmustbeofuint8type,whiletheoutputisauint16.%Tableisacellarray,eachelementcontainingthecorrespondingcode.%Thisisanimplementationofthealgorithmpresentedinthearticle%SeealsoLZW2NORM%$Author:GiuseppeRidino'$%$Revision:1.0$$Date:10-May-200414:16:08$%Revision:%Changethecodetablestructuretoimprovetheperformance.%date:22-Apr-2007%by:HaiyongXu% Reworkthecodetablecompletelytogetreasonableperformance.% date:24-Jun-2007% by:DuncanBarclay%Howitencodes:%STRING=getinputcharacter%WHILEtherearestillinputcharactersDO%CHARACTER=getinputcharacter%IFSTRING+CHARACTERisinthestringtablethen%STRING=STRING+character%ELSE%outputthecodeforSTRING%addSTRING+CHARACTERtothestringtable%STRING=CHARACTER%ENDofIF%ENDofWHILE
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年甘肃省天水市秦安县教育局所属事业单位选调18人考前冲刺密卷附完整答案详解【考点梳理】
- 2026河北石家庄新铁职业高级中学教职工招聘200人考前冲刺密卷附答案详解(培优A卷)
- 2026年福建厦门大学会计发展研究中心行政秘书招聘1人笔试题库及答案详解【典优】
- 2026四川乐山市沙湾区增量政策性岗位招募26人模拟试卷及完整答案详解(网校专用)
- 2026财达证券股份有限公司资产管理业务委员会招聘8人考前冲刺试卷(典优)附答案详解
- 2026四川攀枝花市西区信访局临时聘用1人考前冲刺试卷及参考答案详解【B卷】
- 2026年池州市精信人力资源服务有限公司招聘(三)笔试题库(夺分金卷)附答案详解
- 2026年亳州蒙城县乡村振兴就业见习人员公开招募215名考前冲刺试卷附答案详解(研优卷)
- 2026年合肥长丰县公证处服务外包用人招聘考前冲刺密卷【培优A卷】附答案详解
- 2026重庆市血液中心非在编采血护士招聘1人模拟试卷及完整答案详解【考点梳理】
- 2026公司安全生产管理制度及文件汇编(2026版)
- 2026教育局干部面试题及答案
- 2025年学前教育教师招聘真题及答案
- 水电工程测量规范(NBT 35029-2023 )
- 初创公司财务规章制度
- 老年活动中心范例
- 高处作业吊篮安装、拆卸、使用技术规程(2025版)
- 国际化高端会计人才选拔试题
- 江西省宜春市丰城市第九中学2025-2026学年高二上学期开学考试数学试题(含解析)
- 精神康复治疗师考核试题及答案
- 2026届人教版高三数学一轮复习零基础(三角函数板块:正余弦定理与解三角形)讲义、专题练习、答案汇编
评论
0/150
提交评论