www.pudn.com > PCMandLiner.rar > PCMandLiner.c


//单片机数字信号处理的必备工具--线性码与A律PCM编码转换的互相转换 
///////////////////////////////////////////////////////////////////////////// 
// A律PCM编码转换为线性码 
int PcmToLinear(char pcm) 
{ 
    char tmp = 0 ; 
	int result = 0 ; 
/* get rid of character bit */ 
	char p=0x80; 
//	if ( pcm < 0x80 ) 
	if ( pcm > 0 ) 
		tmp = pcm ^ 0x55 ; 
	else 
		tmp = (pcm ^ 0x55 ) & 0x7f; 
 
/* convert tmp */ 
	if ( tmp >= 0x70 ) 
//		result = 1024 + ((tmp-0x70)*64) + 32; 
		result = 1024 + ((tmp-0x70)*64) ; 
	else if ( result >= 0x60 ) 
//		result = 512 + ((tmp-0x60)*32) + 16 ; 
		result = 512 + ((tmp-0x60)*32)  ; 
	else if ( tmp >= 0x50 ) 
//		result = 256 + (tmp-0x50)*16 + 8 ; 
		result = 256 + (tmp-0x50)*16  ; 
	else if ( tmp >= 0x40 ) 
//		result = 128 + ((tmp-0x40)*8) + 4 ; 
		result = 128 + ((tmp-0x40)*8)  ; 
	else if ( tmp >= 0x30 ) 
//		result = 64 + ((tmp-0x30)*4) + 2 ; 
		result = 64 + ((tmp-0x30)*4)  ; 
	else if ( tmp >= 0x20 ) 
//		result = 32 + ((tmp-0x20)*2) + 1 ; 
		result = 32 + ((tmp-0x20)*2)  ; 
	else if ( tmp >= 0x10 ) 
		result = 16 + (tmp-0x10); 
	else 
		result = 0 + tmp ; 
// may need, or not! 
 
	if ( pcm >= 0 ) result = 0-result ; 
		//result = 0x10000 + result ; 
 
	return result ; 
} 
 
// 线性码转换为A律PCM编码 
char LinearToPcm(int linear) 
{ 
	int tmp = linear ; 
	char result = 0 ; 
	 
	if ( tmp < 0 )	tmp = -tmp ; 
/* convert tmp */ 
	if ( tmp >= 1024 ) 
		result = 0x70 + ((tmp-1024)/64); 
	else 
		if ( tmp >= 512 ) 
			result = 0x60 + ((tmp-512)/32); 
		else 
			if ( tmp >= 256 ) 
				result = 0x50 + (tmp-256)/16; 
			else 
				if ( tmp >= 128 ) 
					result = 0x40 + ((tmp-128)/8); 
				else 
					if ( tmp >= 64 ) 
						result = 0x30 + ((tmp-64)/4); 
					else 
						if ( tmp >= 32 ) 
							result = 0x20 + ((tmp-32)/2); 
						else 
							if (tmp >= 16 ) 
								result = 0x10 + (tmp-16) ; 
							else  
								tmp = tmp ; 
 
	if ( linear >= 0 ) 
		result = result^0x55 | 0x80 ; 
	else 
		result = ( result^0x55 ) ; 
 
  return ( result ); 
}