Montag, 5. Dezember 2011

EE4209/EE5809 Digital Audio : Rice Code 1.4

//rice_code4.c
// This is a complete C program for Rice coding-- Kojeve, Hyppolite, Lauer on Self-consciousness as Desire


#define N 128
#define MAX_CODE_SIZE 32
#include
#include

typedef struct rice_code_info
{
short int abs_residual; //
int m; // the m parameter used in this sample;
int sign; // 1 or -1;
int index; // original location in the sample
unsigned char rice_codedata[MAX_CODE_SIZE];
int code_size;

} RICE_CODE_INFO;

RICE_CODE_INFO r_info[N];

int m_param = 32;

int source_data[6] = {25, 34,42, 47,66,76};
//char* r_code[6];

void rice_encode(RICE_CODE_INFO r_info[], int sourc_data[], int m)
{
// qcode and rcode

int quotient ; // quotient
int remainder, remain_part;
int i=0,j, k, log2M, v=1, and_1=0, s=0;
int p =0; // print out the rice code

log2M = (int)floor(log2(m));

printf("log2M is :%d\n ", log2M);
printf(" the MSB is the sign bit\n");

for (i=0; i<6; i++)
{

r_info[i].abs_residual = source_data[i];
r_info[i].sign=1;

quotient = r_info[i].abs_residual/m; // find quotient

remainder = r_info[i].abs_residual%m; // find remainder

if (r_info[i].sign == -1) {
r_info[i].rice_codedata[0]='1'; // negative sign
}

else {
r_info[i].rice_codedata[0]='0'; // positive sign
}


if (quotient ==0 )
{
// printf("quotient is 0\n");

r_info[i].rice_codedata[1]='0';
printf(" data= %d, quotient = %d, remainder = %d\n", source_data[i], quotient, remainder);
}


else {
printf(" data = %d ,quotient = %d , remainder = %d\n", source_data[i], quotient, remainder);

for (j =0; j<=quotient; j++) {
r_info[i].rice_codedata[j+1] = '1'; // quotient in unary code

}



} // else
r_info[i].rice_codedata[quotient+1]='0'; // to delimit the end of qcode

// q+r : r in truncated binary encoding : from q+1 to (q+1 +M )
// only M size is needed. The remaining bits are discarded.
remain_part = remainder;


for (k=log2M-1, s=0; k >=0 , s<=log2M-1; k--, s++) {
//shift the remainder
remain_part = remainder >> k ;
//printf(" after shift, r is : %d , k is %d ", remain_part, k);

if (v & remain_part )
r_info[i].rice_codedata[1+quotient+1+s] ='1';

else
r_info[i].rice_codedata[ 1 + quotient+1+s] ='0';


}

r_info[i].code_size = ( 1+quotient +1 + log2M );
// size of rice code : (sign bit + quotient + delimit_bit_0 + remainder)

//print out the rice code


for (p=0; p<r_info[i].code_size; p++) {

printf("%c", r_info[i].rice_codedata[p]);
// printf(" code_size = %d\n", r_info[i].code_size);
}
printf("\n");

printf("v is %d\n", v);
printf("v <<4 is %d ", v<<4);
printf("v >>4 is %d\n", v>>4);
printf(" 7>>1 is %d ",7>>1 );

}


}

void rice_decode( RICE_CODE_INFO r_info[], int m)
{
int quotient=0, remainder=0 ,log2M, remain_const=0;
int number=0, i=0, j=0, k=0, r=0,s=0;

printf(" rice-decode()\n");
log2M = (int)floor(log2(m));
for (i=0; i<6; i++) {

for(j =0;j<r_info[i].code_size; j++) {
printf("%c", r_info[i].rice_codedata[j]);
}

printf("\n");

printf("find the quotient ...\n");
for (k=1; k<(r_info[i].code_size-(log2M) );k++) {
if (r_info[i].rice_codedata[k] =='1') {
quotient++;
}

} // for - to find the quotient
printf("the quotient is : %d ;", quotient);

// find the remainder : start from : sign bit+ quotient + delimit bit
for (r= (1+quotient +1), s=log2M-1 ; r<(r_info[i].code_size), s>=0; r++,s-- ) {
if (r_info[i].rice_codedata[r] == '1') {
// printf("%d " , r_info[i].rice_codedata[r]);
// need to convert the char constant to integer
remain_const = r_info[i].rice_codedata[r] -48;
remainder += ( remain_const ) * (pow(2, s));

}
}

//log2M--;
number = remainder + quotient*m;
printf(",remainder is %d\n", remainder);


quotient =0; //reset the q forthe next loop
remainder =0; // reset the remainder for the next loop
log2M = (int)floor(log2(m));

printf(" the number is ..:%d\n", number);

}

}

int main( int argc, char* argv[] )
{
printf("Rice code\n");
rice_encode( r_info, source_data, m_param);

printf(" 25/32 = %d\n", 25/32);
printf(" 66/32 = %d\n", 66/32);

//--------------
printf("----------------\n");
printf(" decoding rice code . . .\n");
rice_decode( r_info, m_param);

return 0;
}



Keine Kommentare: