算術編碼在圖象數據壓縮標準(如jpeg,jbig)中扮演了重要的角色。在算術編碼中,消息用0到1之間的實數進行編碼。算術編碼用到了兩個基本的參數:符號的概率和它的編碼間隔。信源符號的概率決定壓縮編碼的效率,也決定編碼過程中信源符號的間隔,而這些間隔包含在0到1之間。編碼過程中的間隔決定了符號壓縮后的輸出。
算術編碼需要輸入的是符號,各個符號的概率還有需要編碼的符號序列,根據概率可以算出初始編碼間隔,先設幾個變量在后面可用:High——當前編碼的上限,Low——當前編碼的下限,high——中間變量,用來計算下一個編碼符號的當前間隔的上限,low——中間變量,用來計算下一個編碼符號的當前間隔的下限,d——當前間隔之間的距離。第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個編碼符號的概率。
編碼程序如下:
#include <iostream.h> #define M 100 #define N 4 class suanshu { int count,length; char number[N],n; long double chance[N],c; char code[M]; long double High,Low,high,low,d; public: suanshu() {High=0;Low=0;} void get_number(); void get_code(); void coding(); ~suanshu(){} };
void suanshu::get_number() { cout<<"please input the number and its chance."<<endl; for(int i=0;i<N;i++) { cin>>n>>c; number[i]=n; chance[i]=c; } if(i==20) cout<<"the number is full."<<endl; count=i; }
void suanshu::get_code() { cout<<"please input the code''s length:"; cin>>length; while(length>=M) { cout<<"the length is too larger,please input a smaller one."; cin>>length; } for(int i=0;i<length;i++) { cin>>code[i]; } }
void suanshu::coding() { int i,j=0; for(i=0;i<count;i++) if(code[0]==number[i]) break; while(j<i) Low+=chance[j++]; d=chance[j]; High=Low+d; for(i=1;i<length;i++) for(j=0;j<count;j++) { if(code[i]==number[j]) { if(j==0) { low=Low; high=Low+chance[j]*d; High=high; d*=chance[j]; } else { float chance_l=0.0; for(int k=0;k<=j-1;k++) chance_l+=chance[k]; low=Low+d*chance_l; high=Low+d*(chance_l+chance[j]); Low=low; High=high; d*=chance[j]; } } else continue; } cout<<"the result is:"<<Low<<endl; }
int main() { suanshu a; a.get_number(); a.get_code(); a.coding(); return 0; }
本程序在VC6.0和xp專業版下運行通過,這是我個人第一次用c++寫的比較完整的程序,還有些不盡人意的地方,比如變量和函數命名不太專業,以后會注意,慢慢也會好的。呵呵~
|