您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 其它行业文档 > 算术编解码matlab实现
%Author:Quguangxiangquguangxiang@126.comOctober23,2011%Postaladdress:%RoomA2012,Songgardenbuildingno.2,NanHuCampus,ChinaUniversityofMiningandTechnology,%XuZhou,Jiangsu,P.R.China,221008%%自编程序.clcclearallcloseallTip={...'请按下列要求输入字符串:''1、字符串长度适宜;''2、可以输入的字符仅限于:a,b,c,d,e,f;''3、输入的字符一定要用英文状态下的单引号引起来,例如:''efbfcafdcc''。'};disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')disp(Tip)disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')string_s=input('请按要求输入字符串:');[j,k]=size(string_s);%计算字符串的长度pa=0.1;pb=0.1;pc=0.3;pd=0.1;pe=0.1;pf=0.3;%初始化概率disp(['输入的字符串是:',string_s])%显示输入的字符串formatlong%定义数据显示精度fori=1:k%开始算数编码ifi==1%为字符串的第一个字符编码,a1,a2分别表示个字符串区间的端点switch1%用“开关语句”检测是什么字符,在做相应的编码处理casestring_s(i)=='a'a1=0;a2=0+pa;casestring_s(i)=='b'a1=pa;a2=pa+pb;casestring_s(i)=='c'a1=pa+pb;a2=pa+pb+pc;casestring_s(i)=='d'a1=pa+pb+pc;a2=pa+pb+pc+pd;casestring_s(i)=='e'a1=pa+pb+pc+pd;a2=pa+pb+pc+pd+pe;casestring_s(i)=='f'a1=pa+pb+pc+pd+pe;a2=1;endl=a2-a1;%计算各字符串编码区间的长度endif(i=2)&(i=k)%在第一个字符已编码的基础上为后续字符编码switch1casestring_s(i)=='a'aa=a1;ab=a1+l*pa;a1=aa;a2=ab;casestring_s(i)=='b'aa=a1+l*pa;ab=a1+l*(pa+pb);a1=aa;a2=ab;casestring_s(i)=='c'aa=a1+l*(pa+pb);ab=a1+l*(pa+pb+pc);a1=aa;a2=ab;casestring_s(i)=='d'aa=a1+l*(pa+pb+pc);ab=a1+l*(pa+pb+pc+pd);a1=aa;a2=ab;casestring_s(i)=='e'aa=a1+l*(pa+pb+pc+pd);ab=a1+l*(pa+pb+pc+pd+pe);a1=aa;a2=ab;casestring_s(i)=='f'aa=a1+l*(pa+pb+pc+pd+pe);ab=a1+l*(pa+pb+pc+pd+pe+pf);a1=aa;a2=ab;endl=a2-a1;%计算各字符串编码区间的长度endenddisp('编码区间的起始值是:');disp(a1)%显示编码区间的起始值disp('编码区间的终止值是:');disp(a2)%显示编码区间的终止值disp('本程序选择区间中点做为编码是:');disp((a1+a2)/2)%显示编码%打印是否译码的提示语句disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')disp('是否要译码,请选择:')disp('1、译码')disp('2、不译码?')disp('说明:输入相应的数字进行选择')disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')%检测程序运行者的选择YN=input('你的选择是:');%选择译码ifYN==1bm=(a1+a2)/2;SuansuJiema(bm,k)end%选择不译码ifYN==2disp('您选择了不译码。')endfunctionSuansuJiema(bm,num)formatlong%定义数据显示精度pa=0.1;pb=0.1;pc=0.3;pd=0.1;pe=0.1;pf=0.3;%初始化概率i=1;ym={};ym{1}(1)=YM(bm);%调用单个字符译码子程序YM对信息流的第一个码元译码bm0=bm;%为第二个码元及以后各码元的译码做铺垫运算whilei=1&inumswitch1caseym{1}(i)=='a'bm0=(bm0-0)/pa;caseym{1}(i)=='b'bm0=(bm0-pa)/pb;caseym{1}(i)=='c'bm0=(bm0-pa-pb)/pc;caseym{1}(i)=='d'bm0=(bm0-pa-pb-pc)/pd;caseym{1}(i)=='e'bm0=(bm0-pa-pb-pc-pd)/pe;caseym{1}(i)=='f'bm0=(bm0-pa-pb-pc-pd-pe)/pf;endi=i+1;ym{1}(i)=YM(bm0);%调用单个字符译码子程序YM对第二个码元及以后各码元译码end%打印输出译码结果disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')disp(['译码结果是:',ym{1}])disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')functionym=YM(A)pa=0.1;pb=0.1;pc=0.3;pd=0.1;pe=0.1;pf=0.3;%初始化概率%用“开关语句”检测编码所处的区间,以便做相应的译码处理switch1case0=A&Apaym='a';casepa=A&Apa+pbym='b';casepa+pb=A&Apa+pb+pcym='c';casepa+pb+pc=A&Apa+pb+pc+pdym='d';casepa+pb+pc+pd=A&Apa+pb+pc+pd+peym='e';casepa+pb+pc+pd+pe=A&A1ym='f';caseA=1|A0disp('该码字为错误编码')end
本文标题:算术编解码matlab实现
链接地址:https://www.777doc.com/doc-5206858 .html