Hi All, If anyone knows of a bit rounding algorithm, please forward the information to me. I am trying to round-off 24 bits to 12-bits. Thanks MORPHEUS p.s. the 24 bits is the result of an additing between two 24 bit numbers. I need to round off the result and feed it to a 12-bit DAC. THNX
Depends on your tolerance to quantization noise and bias.
The simplest approach is to simply truncate the 12 lower bits. This results in an error between 0 and +1 lsb, which introduces a bias of 1/2 LSB.
The bias can be reduced using simple rounding: Add 1/2 of the retained LSB weight before truncating. In your case, you'd add 0x0800 (a '1' in the top discarded bit) to the 24 bit value before truncating off the 12 LSBs. This reduces the bias to 1/2 of the lsb weight of the 24 bit word, but does not totally eliminate the bias.
A bias remains because 0.5 (0x800 in the lowest 12 bits) always rounds up. 0.5 is equidistant to 0 and 1, so it introduces a small bias. The bias can be eliminated by modifying the rounding algorithm to either round to or away from zero or round towards even or odd. Note with simple rounding, +0.5 rounds up to 1 and -0.5 rounds up to 0. With the round to or round away from zero, the direction of rounding is modified when the value is negative so the +0.5 rounds up to 1 and -0.5 rounds down to -1 (or both round to 0 if the sense is reversed). This is called symmetric rounding. It can be accomplished by adding 0.5-LSB (in your case 0x7FF) and then connecting the most significant (sign bit) to the carry in of the adder (invert the carry in to reverse the sense). If the carry-in is 0, then 0.5 is rounded down, and if it is 1 then 0.5 is rounded up. Symmetric rounding uses the sign to reverse the direction of rounding based on the sign of the input.
Rounding to even or odd is similar, except the value of the input bit corresponding to the LSB of the rounded output is used as the carry in so that rounding of n.5 is always to the even (or to the odd) value. Round to even/odd is useful when you are rounding as part of an arithmetic process before complete results are available because it is relatively easy to pre-compute the LSB value.
If quantization noise is an issue, then you can improve noise performance with feedback of the error introduced by rounding. That is a bit more complicated, so I'll save discussion of it for another time.
input truncation simple symmetric rnd to even
-2.7 -3 -3 -3 -3
-2.5 -3 -2 -3 -2
-2.3 -3 -2 -2 -2
-1.5 -2 -1 -2 -2
-0.5 -1 0 -1 0
0.5 0 1 1 0
1.5 1 2 2 2
2.5 2 3 3 2
--Ray Andraka, P.E.
President, the Andraka Consulting Group, Inc.
MORPHEUS, I know you are asking specifically about rounding, but I am a little concerned about the addition of two 24 bit numbers with a 24 bit result. You need a 25 bit result. Do you have overflow issues here? Doug
Thanks Dough, I am aware of the overflow, infact, my adder is 25 bits but I was just concerned with the rounding of the 24 data bits. I was going to account for the sign bit as part of the rounding algo(once I got one). Regards MORPHEUS