Help - Search - Members - Calendar
Full Version: about the audio quantization of 16bit to 8bit
Hydrogenaudio Forums > Hydrogenaudio Forum > General Audio
weiwei
hi, every expert:
i have an intractable problem in the hand. I want to quantize 16bit pcm audio to 8bit pcm audio while remain the LSB bit (Least Significant Bit) unchanged. For example, if the LSB of 16bit audio is "0", then the LSB of 8bit audio is "0", if the LSB of 16bit audio is "1", then the LSB of 8bit audio is "1". At the same time, the 16bit audio and 8bit audio have near quality.
Does anybody can know how to do this? I refer to the G.711 code, but i didn't have the idea. Please help me!

Thank u so so much!
benski
C99 code:
CODE

// val16 == int16_t sample
uint8_t val8 = (val16 + 32768) >> 8; // convert from 16bit signed to 8bit unsigned
val8 &= ~1; // clear LSB
val8 |= val16&1; // set LSB to LSB of val16


The audio won't have the same quality. You are losing 9bits of precision (8 bits + an extra 1 bit to save the LSB, which is presumably some in-band signal). The remaining 7 bits gives you a noise floor around -42dB. The LSB requirement prevents the use of dither to improve the audio quality.
SebastianG
QUOTE(benski @ Jul 4 2008, 04:20) *

The LSB requirement prevents the use of dither to improve the audio quality.

You certainly can add dithering:
CODE

// val16 == int16_t sample
int32_t bit = (val16 & 1) << 8;
int32_t dither = ...; // unsigned 9 bit random number (0..511)
int32_t q = (val16 + dither + 32768 - bit) & ~((int32_t)0x1FF);
if (q<0) q=0; else if (q>0xFE00) q=0xFE00;
uint8_t q8 = (q+bit) >> 8;

weiwei
QUOTE(benski @ Jul 3 2008, 20:20) *

C99 code:
CODE

// val16 == int16_t sample
uint8_t val8 = (val16 + 32768) >> 8; // convert from 16bit signed to 8bit unsigned
val8 &= ~1; // clear LSB
val8 |= val16&1; // set LSB to LSB of val16


The audio won't have the same quality. You are losing 9bits of precision (8 bits + an extra 1 bit to save the LSB, which is presumably some in-band signal). The remaining 7 bits gives you a noise floor around -42dB. The LSB requirement prevents the use of dither to improve the audio quality.


Thanks for your reply, your method is so direct and simple. My expectation is that if we can do this using G.711 a-law coding algorithm? In the G.711 coding, the 3bit LSB was trancated, that is the problem, Also we should modify the table seg_aend, but i cant know the detail of implementation? You know what i mean? Wish for your help!

Best Regards!

benski
QUOTE(SebastianG @ Jul 3 2008, 23:20) *

QUOTE(benski @ Jul 4 2008, 04:20) *

The LSB requirement prevents the use of dither to improve the audio quality.

You certainly can add dithering:


Ahh, good point. Just treat it like dithering to 7bit audio. smile.gif


weiwei
QUOTE(SebastianG @ Jul 3 2008, 21:20) *

QUOTE(benski @ Jul 4 2008, 04:20) *

The LSB requirement prevents the use of dither to improve the audio quality.

You certainly can add dithering:
CODE

// val16 == int16_t sample
int32_t bit = (val16 & 1) << 8;
int32_t dither = ...; // unsigned 9 bit random number (0..511)
int32_t q = (val16 + dither + 32768 - bit) & ~((int32_t)0x1FF);
if (q<0) q=0; else if (q>0xFE00) q=0xFE00;
uint8_t q8 = (q+bit) >> 8;



Thanks for your help. I transplant this code to matlab. Here is the code.
[x,fs,nbits] = wavread(filename);
x = fix((x+1)*32768);
x = uint16(x);
bit = bitshift(uint16(bitand(x,uint16(1))),8);
dither = ....; % rand number 0..511
dither = uint16(dither);
temp1 = x + dither + 32768 - x;
temp2 = uint16(65024);
q = bitand(temp1,temp2);
q(find(q<0)) = 0;
q(find(q>65024)) = 65024;
y = bitshift(q+x,-8);

After testing the code, i found that all of the y are below 256, that is all y<256. If the signal y is normal, the value of y may be in the range [0 512]. That is if we read 8bit audio, take procedure as follows :
[y,fs,nbits] = wavread(filename); % 8bits audio
y = (y+1)*256;
then all the y must in the range [0 512], but the tranplating code cant get this result, the new y is out of nomal.What is problem, and how can sovle it? Please help me!

Best regards!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.