On Fri, Nov 20, 2009 at 11:13 AM, Schirmacher, Rolf
<address@hidden> wrote:
Hello,
I need to convert normalised numbers [-1. ... 1.[ to hex strings in 24 bit
fractional format. The most efficient code I have at the moment is as below:
DATA = "" # random data at [-1 .. 1]
# scale to 24 bit integers
DATA = "">
DATA = "">
# first part: check data range
tic;
for idx = 1:length(DATA)
if DATA(idx) > (2^23)-1
error(['Data are outside legal range, idx:' num2str(idx)]);
elseif DATA(idx) < -2^23
error(['Data are outside legal range, idx:' num2str(idx)]);
end
end
toc
# second part: convert to string array
tic;
MyString = repmat('0x000000', length(DATA), 1);
for idx= 1:length(DATA)
if sign(DATA(idx))== 1 % number is positive
unumber = uint32(DATA(idx));
elseif sign(DATA(idx))== 0 % number is positive
unumber = uint32(DATA(idx));
elseif sign(DATA(idx))== -1 % number is negative
unumber = uint32(-DATA(idx)); % change sign to positive
unumber = bitcmp(unumber,24); % do the complement
unumber = unumber +1; % subtract 1
endif
MyString(idx,:) = sprintf('0x%06x', unumber); % Formatted data (24-bit
fractional integer)
endfor
toc
However, the for-loops (especially the second one) are quite time consuming,
so I would like to vectorise them. But somehow everything I tried so far did
not work...
Thanks in advance for any hint
Rolf