Re: Changing the data type of an array




Fernando Gomez wrote:
> Hi,
>
> I have a very specific question. I am sure there must be a way to do
> this efficiently yet I have been unable to do so.
>
> I am using MATLAB to communicate with a remote instrument (logic
> analyzer). That instrument provides the result of the measurement as
> an array of unsigned bytes. But as I know that it is indeed an array
> of 16 bit signed integers (int16), I have to go through a data
> conversion process. To do the conversion, currently I am reading the
> byte array with a "for" loop which builds a new array whose length is
> have the original array length. As you can imagine, this is way too
> slow when the data measured is large. I wonder if there is a
> better/faster way of doing this with MATLAB (I bet there is, but I
> have been unable to find it). For instance, if I was doing this in C,
> I would just use an integer pointer that would point to the byte
> array. Any help would be appreciated.

If I understand what you're saying, what you get
is an array of 2N uint8's, and you want to interpret
every pair of bytes as a signed int16.

One quick and dirty way to do it would be to use
low-level file I/O. Write your byte array A to a file
with fwrite(fid, A, 'uint8') and read it back with
A = fread(fid, inf, 'int16').

You may have an endian issue doing it this way (the
byte order your machine expects for int16's is different
from the order in the file). I think you can use an
argument in FOPEN or FREAD to fix that if it occurs.

Other ways that occur to me involve using bitwise
calls like BITSHIFT and BITOR, the same way you'd
assemble bytes into words in C. Those are vectorized
so you can do the whole array at once.

- Randy

.



Relevant Pages

  • Re: Byte swapping efficiently
    ... signed integers into an array is about the best I can expect. ... Data(DataX, DataY) = ByteSwap ... DataX = DataX + 1 ... The Datatwo-dimensional array is declared as private single, ...
    (microsoft.public.vb.general.discussion)
  • Re: An interesting new RNG.
    ... for signed integers. ... All you need is an array, ... Most implementations would ask for a few seeds from which the ... A nice idea for an RNG. ...
    (comp.lang.c)
  • Re: PowerBasic rocks!
    ... integers into an array of 32-bit signed integers. ... We'll acquire 64M samples once a second or so, so the signal averaging ... Well, the old array iterator LODSW, followed by ops, followed by ... I don't know MMX instructions, so you'll have to look that up ...
    (sci.electronics.design)
  • PowerBasic rocks!
    ... I just tried a test loop to add an array of 64 million 16-bit signed ... integers into an array of 32-bit signed integers. ... I could rewrite this using pointers and it might be faster. ...
    (sci.electronics.design)
  • Changing the data type of an array
    ... I am using MATLAB to communicate with a remote instrument (logic ... But as I know that it is indeed an array ... of 16 bit signed integers, I have to go through a data ... conversion process. ...
    (comp.soft-sys.matlab)