I recive 24 bits fixed-point data from a CODEC (ADC) by TDM transmission. Then data is stored into a int buffer (32 bits). I want to convert this to float data.
I tried:
g_analog_input_chL_mean = (((float)(*rx_block_pointer[buffer_cntr] << 8)) * (1.0/2147483648.0));
What is the best way to do this conversion?
Thanks, Costi
It depends on the scaling of your fixed point data and also, if the data is signed or unsigned. When you use float to represent 32 bit fixed point data, then you will loose some digits, since some bits are used to store the exponent. I do not really understand, why you do this 32 bit conversion since you could skip this step and write
g_analog_input_chL_mean = (float)(*rx_block_pointer[buffer_cntr])/(float)(0x7FFFFF);
If you insist on this 32 bit conversion, then use
g_analog_input_chL_mean = (float)(*rx_block_pointer[buffer_cntr] << 8)/(float)(0x7FFFFFFF);
Note, that I would always use the hex-scaling which is more typo safe than writing the decimal number. Yours is indeed wrong, since the maximum signed integer number is 2147483647, not 2147483648.
If the ADC value has an unsigned format, than you can use the same expressions, but with the sign-bit included:
g_analog_input_chL_mean = (float)(*rx_block_pointer[buffer_cntr])/(float)(0xFFFFFF);
or
g_analog_input_chL_mean = (float)(*rx_block_pointer[buffer_cntr] << 8)/(float)(0xFFFFFFFF);
Last point: The ADC value might not be scaled to one. So it might be necessary to muliply the whole Expression by your maximum floating point value.
EDIT
I now see the necessity of shifting it to 32 bit. The compiler will then be able to perform the correct sign interpretation when converting to float. Thanks, for the correction.