Elias Delta Coding

2 篇文章 0 订阅
2 篇文章 0 订阅

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 num
		int 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值