EliasDelta Coding
適用範圍:
EliasDeltaCoding和EliasGamma Coding一樣,也是一種對正整數進行編碼的統一編碼,由PeterElias發明。適用於預先無法獲知最大編碼整數的情況,而且小整數出現頻率高,大整數出現頻率低。
編碼原理:
對任何正整數NUM,對INT(Log2(NUM))+1進行Gamma編碼,尾碼上NUM二進位串除去最高位的子串。如5的編碼為011,01。
編碼樣本:
NUM |
EliasDelta Code |
Implied probability |
1 = 20 + 0 |
1 |
1/2 |
2 = 21 + 0 |
0100 |
1/16 |
3 = 21 + 1 |
0101 |
1/16 |
4 = 22 + 0 |
01100 |
1/32 |
5 = 22 + 1 |
01101 |
1/32 |
6 = 22 + 2 |
01110 |
1/32 |
7 = 22 + 3 |
01111 |
1/32 |
8 = 23 + 0 |
00100000 |
1/256 |
9 = 23 + 1 |
00100001 |
1/256 |
編碼、解碼演算法:
/****************************************************Encode_EliasDelta:Encoding algorithm of EliasDelta Coding.*****************************************************/int Encode_EliasDelta(int *pSourceData,char *pEncodedData,int nSourceDataLen,int &nEncodedDataLen){int k=-1; for(int i=0;i<nSourceDataLen;i++) { int num = pSourceData[i]; int numPow= int(log10(num)/log10(2));int num1 = numPow+1;int num1Pow= int(log10(num1)/log10(2));for (int j=0; j <num1Pow ; j++) pEncodedData[++k]=0;pEncodedData[++k]=1; for (j=num1Pow-1; j >= 0; j--) { if (num1 & 1 << j) pEncodedData[++k]=1; else pEncodedData[++k]=0;} for (j=numPow-1; j >= 0; j--) { if (num & 1 << j) pEncodedData[++k]=1; else pEncodedData[++k]=0; }nEncodedDataLen=k+1;}return 1;}/****************************************************Decode_EliasDelta:Decoding algorithm of EliasDelta Coding.*****************************************************/int Decode_EliasDelta(int *pDecodedData,char *pEncodedData,int &nDecodedDataLen,int nEncodedDataLen){int i=0,j=0;while (1) {//Decode num1 int num1Pow = 0; while (!pEncodedData[i++]) num1Pow++; if(num1Pow >=48) break; int num1 = 0; for (int h=num1Pow-1; h >= 0; h--)if (pEncodedData[i++]) num1 |= 1 << h; num1 |= 1 << num1Pow; //Decode numint numPow = 0;numPow = num1-1;int num =0;for( h=numPow-1;h>=0;h--){if(pEncodedData[i++])num |= 1 <<h;}num |= 1 << numPow; pDecodedData[j++]=num; } nDecodedDataLen=j;return 1;}