Huffman编码的Matlab实现_第1页
Huffman编码的Matlab实现_第2页
Huffman编码的Matlab实现_第3页
Huffman编码的Matlab实现_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、clea r a 11f printf ( Re a ding d ata、)d ata= i m r ea d ( c a me r a marix tif):da t a=uin t 8 (da t a) ;$读入数据,并将数据限制为u i n t 8f pri n t f (* Don e ! n)%编码压缩fprintf C pre s sing da t );z i pp e d, info = n orm 2 huff (data);fprintf (1)one! n)%解压缩f p rin t f ( pressin g data、);u nzi p ped=h u ff 2 n

2、 o r m(zi p ped, in f o ):f p r intf ( Done!n )%测试就是否无失真isOK=i s e q u a 1 (data(:), unz i p p ed(:)%显示压缩效果whos da t a z ippe d u n z i pped fun c ti o n zip p ed, info = nor m2 huff(vect or) ifisa( v ect o r, uint 8 *),er r or ( i npu tar gume n t must be a uint 8 vector)endvecto r =ve c t o r (:)%

3、将输入向量转换为行向量f=freq u en c y (vec t or);%汁算个元素出现得概率 simbo 1 s=fi n d (f=0);f二f (simbol s );%将元素按岀现得概率排列f, s o rti n d ex = s ot (f ):simbols = simbols( s ortindex):%产生码字 ge n e r ate the c odeword a s th e 5 2 bi t s of a d o u bl e len= 1 e ng t h( s imb o Is);s i mb ols_in d ex=num2cell (1:len);c od

4、ew ord_tmp= c ell(le n, 1):wh i le le n g t h ( f )1,in d exl= s i mb o ls_index 1;index 2 =simb o 1 s _in d ex 2 ;codeword_tm p (i n dexl)=a d dno d e( c ode w ord_tm p (in d e xl) u i n t8(0); c o d e wo rd_ t mp( i nd e x 2 ) = a ddno d e (co d e word_tm p (index 2 ), u i nt8( 1 ):f=sum (f (1:2)

5、f (3:end):simbol s_in d ex二indexl inde x2 simb o ls_ i ndex (3:e nd);%将数据重新排列,就是两个节点得频率尽量与前一个节点得频率想当r esort da t a in orde r to h av e two no d es w i th 1 ow e r fr e que n cy afirst toft so r t in dex=sort (f):sim b ols_index=s i mbols_index(so r tindex);end玄对应相应得元素与码字codew ord =cell( 2 56 :1);c o

6、dew o r d (sim b ols) =codew o rd_tmp;%计算总得字符串长度len=0;for index二 1: length (vector) tle n =1 e n+ 1 ength (codew ord doubl e (vector (index )+1);end%产生01序列st r ing=repma t (u i n t8(0), 1, 1 en);poin t er= 1 :for inde x =1:1 e ngth (vect or),co d e=cod e wo rd d o u b 1 e(v e ct o r (inde x )+1;le n

7、 =length(co de);strin g (pointer+(0 :len-1) =code;poin t e r=poin t e r+len;en d%如果需要,加零len= 1 ength ( s tring);p ad=8-mod (1 e n, 8);辻 pad 0 ,s t ring= s tring u in t 8(zeros( 1 , pad);end%保存实际有用得码字cod e wo r d=co d ew o rd (simbo 1 s);c ode 1 en=z e r o s (s i ze (codewor d );weigh t s=2 * (0:23);

8、max c ode 1 e n= 0 ;fo r in d ex 1 : length ( c o d e w ord),len=lengt h (cod e wordi n de x );if 1 e nm a x c od e 1 e n,max cod e le n =len;e ndif len0,c o de=sum(w e ig h t s ( codeword index =1):cod e =bitset (cod e , le n +1);codeword! in d ex二 code:co d ele n (ind e x)=len;en d end co deword 二

9、code w or d :%计算压缩后得向星c o ls= 1 eng t h( s tri n g) / 8 ;s t r i ng= r eshape(strin g , 8, co I s);w eight s=2n(0:7);zip p e d = u int8 (weights*d o u b 1 e (s t rin g):%存储一个稀疏矩阵h uf f c o des=sp a r s e(l, 1);% init s p arse ma t r ixfor in d e x=l: n u m e 1 ( c odeword),huff cod e s ( c o dewo r

10、d (ind e x),1 ) =s i m b ols (index);end%产生信息结构体i nfox pa d = p a d ;info、ratio 二cols、/leng t h(v e c tor):in fox 1 en g th=l e ngth(vec t or);info、max c od e len=maxc o de 1 en;fun c t i o n codewor d _new=a d dno d e (co d eword_old item)codew ord _ne w=cell ( s iz e (cod e word_ol d );for index=l

11、: len g t h(code wor d _old),c o d e w o rd_newin d e x = item co d ewo r d _oldi n de x ;endfu n c t ion v e c t or=huf f 2norm (zipped, info)%HUFF2N0RM Huffman 解码器%IIUFF2N0RM(X, INFO)根据信息体结构info返回向量zipped得解码结果%矩阵参数以x(:)形式输入i f i s a ( z i p ped, uint8) error ( i n pu t a r gumen t mus t be a uint8

12、 vector* )end%产生01序列1 e n=l e ngth(zi p ped);str i ng=repma t (uint 8 (0) , L len、* 8 );b it i n dex= 1 :8;for i n d e x+1:1 en,s t r in g (b i tindex+8、* ( in d e x -1) =uint8 ( b itget ( z ipped(index), bitind ex);end%调整字符串st r ing= 1 o gica 1 (st r ing (:): % r e m o ve 0 padd i n glen 二lengt h (

13、 s t ring);%解码weig h t s =2、 (0:51);v ecto r =re p mat (uintS ( 0 ) , 1 , in f o, length);vec t orin d e x=l;c o deinde x =1;:U a I / g 划一盼 pU2:(x ap u T) gjuinjoi o sa) urns = ( j+x p ut) j% 92: 0 =x a PUT 珂 :(JO ) OQA) q ) 2 U 二UQ(9 9 3 U 0) 1 b indoj= j pua(joiooa g 1 uIn b a q is n in iuoluuSj b n d ut )jojj a4( ,8)u I n ojo a a)stj iADNan&aiH% (j o io a a) Aou a nbjj=j uot)ounj :(spoo) s a poo j jnq、o j ui qi A q (oju i i a p o o) poo a p= a 1X q u o i io u nj puQ pun! I +XQpUTJ O 103 A = X p U I J OJOQA:0 = POD J I =xap

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论