help-octave
[Top][All Lists]
Advanced

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

help for vectorisation


From: Schirmacher, Rolf
Subject: help for vectorisation
Date: Fri, 20 Nov 2009 11:13:01 +0100

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 = 2*rand(10000,1)-1; # random data at [-1 .. 1]

# scale to 24 bit integers
  DATA = DATA*2^23; 
  DATA = round(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


reply via email to

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