help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: help for vectorisation


From: Jaroslav Hajek
Subject: Re: help for vectorisation
Date: Fri, 20 Nov 2009 12:08:14 +0100



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

Here's an approach that's going to work if your signed integers are 2's complement (almost all modern machines).
If I understood correctly what you're trying to do.

DATA = "" # random data at [-1 .. 1]
DATA = "" (typecast (int32 (2^31 * DATA), "uint32"), -8);
MyString = reshape (sprintf ("0x%06x",DATA), 8, []).'

Gosh, if people paid for Octave one-liners, I'd be a millionaire! :D

--
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz

reply via email to

[Prev in Thread] Current Thread [Next in Thread]