how to transfer float to 8.24 format
I need help, how to create conversion function from float number to 8.24 representation format . I have connected microprocessor with SPI to adau. I'm looking for easier conversion function:
for example in volume controls is value
0dB represented as number 1 and it is {0x01, 0x00, 0x00, 0x00} array of hex bytes.
-10db is represented as float 0.31622779 and it is {0x00, 0x50, 0xF4, 0x4E} as array of hex byte
How can I calculate this four bytes 8.24 representation easier? Or how can I generate these four bytes from dB representation?
8.24
function
spi
number format
It's 10^(x/20) * 2^24 encoded as 32-bit signed integer, transferred in big-endian byteorder on SPI.
Using C:
// if your standard library doesn't define exp10f:
float exp10f( float x ) {
return powf( 10.f, x );
}
int32_t float_to_dsp( float x ) {
return (int32_t)( x * 0x1p24f );
// for older compilers you may need to write (float)( 1 << 24 ) instead of 0x1p24f
}
int32_t dB_to_dsp( float x ) {
return float_to_dsp( exp10f( x / 20.f ) );
}
If your SPI peripheral directly supports 32-bit transfers then it should take care of proper byteorder. If you need to provide it with an array of bytes and your microcontroller is little-endian then you'll need to byteswap the value before transferring it to the dsp, e.g. using htobe32()
Just want to add that some mapping examples can be found here:
https://wiki.analog.com/resources/tools-software/sigmastudio/usingsigmastudio/numericformats
They can sometimes help when visualizing formats.
Please log in to reply.