I am receiving packets of binary data encoding audio samples as 24bit unsigned integers. These need to be converted to 32bit signed (2's complement) integers for output.
However, due to the way the data is chunked, it is possible that a 24bit word be split across a packet boundary. I am therefore buffering each byte as an 8bit unsigned integer, ready to be recast in groups of 3 (+ 1byte of zero-padding) when all packets have been received.
I have written the following function to do this, where input
is a 1x3 array of type uint8
:
% This function takes 3 8-bit decimal values as input and converts them to
% a single, signed, 32 bit decimal number.
% NOTE - little endianness is assumed
function output = convert24bitTo32bit(input)
sign = bitget(input(3), 8); % get sign bit
bytes(3) = bitset(input(3), 8, 0); % zero sign bit
value = typecast([bytes, uint8(0)], 'int32') - logical(sign)*(2^23);
end
An example can be run using the below snippets:
% Test 255: 11111111 00000000 00000000
input = uint8([255 0 0]);
output = convert24bitTo32bit(input);
fprintf('\n In: 255 \t Out: %d', output)
% Test -2: 01111111 11111111 11111111
input = uint8([254 255 255]);
output = convert24bitTo32bit(input);
fprintf('\n In: -2 \t Out: %d', output)
This function does the job, but is the slowest process in my processing by several orders of magnitude.
Is there a more efficient way to achieve the same result? Or a built-in Matlab function that can handle more esoteric data type conversions?
Thanks