logo资料库

编程实现算术编码算法.doc

第1页 / 共8页
第2页 / 共8页
第3页 / 共8页
第4页 / 共8页
第5页 / 共8页
第6页 / 共8页
第7页 / 共8页
第8页 / 共8页
资料共8页,全文预览结束
编程实现算术编码算法 中国地质大学计算机学院信息安全专业 信息论实验报告 实验三 算术编码 一、 实验内容 编程实现算术编码算法 二、实验环境 1. 计算机 2. Windows 2000 或以上 3. DEVC++ 三、实验目的 1. 进一步熟悉算术编码算法; 2. 掌握 C 语言编程(尤其是数值的进制转换,数值与字符串之间的转换等) 四、实验要求 1. 提前预习实验,认真阅读实验原理。 2. 认真高效的完成实验,实验过程中服从实验室管理人员以及实验指导老 师的管理。 3. 认真填写实验报告。 五、实验原理 算术编码是把一个信源表示为实轴上 0 和 1 之间的一个区间,信源集合 中的每一个元素都用来缩短这个区间。 1. 算法流程 (1) 输入信源符号个数,信源概率分布,还有需要编码的符号序列, (2) 根据概率可以算出初始编码间隔, High——当前编码的上限, Low——当前编码的下限, high——中间变量,用来计算下一个编码符号的当前间隔的上限, low——中间变量,用来计算下一个编码符号的当前间隔的下限, d——当前间隔之间的距离。 (3)扫描需编码的符号序列,确定编码空间 第 1 个编码符号的当前间隔为其初始的编码间隔, 第 i 个编码符号的当前间隔为第 i-1 个编码后的[Low,High), 第 i+1 个编码符号的当前间隔算法如下:high=Low+d*第 i+1 个初始编码符 号对应的上限,low=Low+d*第 i+1 个编码符号对应的下限,然后 High=high, Low=low,d=d*第 i 个编码符号的概率。 六、参考书 1. 《信息论——基础理论及应用》傅祖芸,电子工业出版社
七、源代码 #include #include #include const double proc[]={0.10,0.10,0.10,0.1,0.1,0.1,0.1,0.1,0.15,0.05}; double result,areaBegin,areaEnd; int cord[1000],cordLength; char str[1000]; int strLength=0; bool readdat() { printf("***********固定模式***********\n"); printf("请输入字符串(0-9):\n"); scanf("%s",str); while(str[strLength]!='\0') strLength++; for(int i=0;i'9'||str[i]<'0') return 1; return 0; } void encord() { printf("编码:"); double w=0.0,len; areaBegin=0.0,areaEnd=1.0; for(int i=0;i
{ temp1*=2; temp2=int(temp1); temp1-=temp2; cord[j]=temp2; printf("%d",temp2); } printf("\n"); } void decord() { printf("译码:\n"); result=0.0; double wei=0.5; for(int i=0;iwei*len) wei+=proc[temp++]; temp--; areaEnd=areaBegin+wei*len;//计算新的空间 areaBegin=areaBegin+(wei-proc[temp]*len); printf("%d",temp); } printf("\n"); int main() { if(readdat()) printf("字符输入错误!!!\n"); else { encord(); decord();
} } return 0; LZ78 算法 #include #include #include #include #define MAXINDEXES 65535 #define MAXBUFFER_IN 256 unsigned int bufin_count,in=0; //in is the pointer to input buffer struct indexes_node{ ////用于构建一级索引的结点 unsigned short indexes[8]; indexes_node* next; void initNode(){ next=NULL; for(int i=0; i<8; i++) indexes[i]=0; } }; struct indexes_node *index_level1[256]={NULL};// storing all ASCII //97 types of chars in a common txtfile(ASCII:32~126 +\n +\t) index_level1[] 为一级索引,以 提高查找速度 char* dictionary[MAXINDEXES]={NULL}; //dictionary structure unsigned short p=1; // pointing to an element which is up to be used next time in dictionary[] array char buffer_in[MAXBUFFER_IN]; //input buffer block char ch[2]={'\0'},s[30]={'\0'}; // working memory ch[] stores the current char
in the inputbuffer and s[] stores the current lemma unsigned short tmp_index, index=0; //tmp_index indicates the location of s[] in the dictionary,index is the location of pre-s[] in the dictionary unsigned short find_lemma(char *); //if it finds a matched string,then it returns the index of the string in the array else returns 0 int insert_dictionary(char *); //insert a new lemma into the dictionary and update the index_level1[] array int main(){ FILE *fp_in, *fp_out; char infilename[40]={'\0'},outfilename[40]={'\0'};//the path of a txtfile up to be compressed short len=0; unsigned short sizeofdictionary=0; //只在 fwrite(&sizeofdictionary,1,sizeof(unsigned short),fp_out);用到 while(1){ printf("Please input the path of the txtfile up to be compressed->"); scanf("%s",infilename); if(!(fp_in=fopen(infilename,"r"))){ printf("Can't not find the txtfile!\n"); } else break; } while(1){ printf("Please input the path for the outfile(including outfile name)->"); scanf("%s",outfilename); if(!(fp_out=fopen(outfilename,"wb"))) printf("Memory is not enough!\n"); else break; } fwrite(&sizeofdictionary,1,sizeof(unsigned short),fp_out); while(!feof(fp_in)){ bufin_count=fread(buffer_in,1,MAXBUFFER_IN,fp_in); in=0; while(in
s[len++]=ch[0]; tmp_index=find_lemma(s); //the processor is much too time-consuming if(tmp_index!=0){ //matching index=tmp_index; continue; } // tmp_index==0: no matching insert_dictionary(s); fwrite(&index,1,sizeof(unsigned short),fp_out); fwrite(ch,1,sizeof(char),fp_out); index=0; len=0; for(int k=0;k<30;k++) s[k]='\0'; } } if(index){ //表示能匹配下去但输入缓冲中已无字符了 s[len-1]='\0'; index=find_lemma(s); fwrite(&index,1,sizeof(unsigned short),fp_out); fwrite(ch,1,sizeof(char),fp_out); } rewind(fp_out); fwrite(&p,1,sizeof(unsigned short),fp_out); fclose(fp_out); fclose(fp_in); printf("The LZ78 algorithm has done the compression successfully!\n\n"); scanf("%d",&len); //in order to let the outwindow pause! return 0; } int insert_dictionary(char *lemma){ unsigned short length=strlen(lemma); struct indexes_node *pnext; unsigned short i,j; if(p
dictionary[p][j]='\0'; strcpy(dictionary[p],lemma); p++; } else //字典已满而待压缩数据还没有结束的情况,此时直接输出码字而不插入字典 return -1; //indicating the dictionary is full //TODO: update the index_level1[],find the index i for c i=lemma[0]; if(i<0 || i>255){ printf("Error!\nsome Chinese words or marks in the following txt:\n==================================================\n%s\n",buffer_in); exit(0); } if(index_level1[i]==NULL){ index_level1[i]=(struct indexes_node *)malloc(sizeof(indexes_node)); index_level1[i]->initNode(); index_level1[i]->indexes[0]=p-1; } else { pnext=index_level1[i]; while(pnext->next)pnext=pnext->next; j=0; while(j<8){ if(pnext->indexes[j]==0) break; j++; } } if(j==8){ //the node is full pnext->next=(struct indexes_node *)malloc(sizeof(indexes_node)); pnext->next->initNode(); pnext->next->indexes[0]=p-1; else{ pnext->indexes[j]=p-1; } } } return 1; //insert successfully
unsigned short find_lemma(char * tmp_lemma){ unsigned short length=strlen(tmp_lemma); struct indexes_node *pnext; unsigned short i,j; if(length>0){ i=tmp_lemma[0]; if(i<0 || i>255){ printf("Error!\nsome Chinese words or marks in the following txt:\n===============================================\n%s\n",buffer_in); exit(0); } pnext=index_level1[i]; while(pnext){ j=0; while(j<8){ if(pnext->indexes[j]){ if(strcmp(dictionary[pnext->indexes[j]],tmp_lemma)==0) return pnext->indexes[j]; } else break; j++; pnext=pnext->next; } } } return 0; //not matching }
分享到:
收藏