Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
trying to understand uint32, single or int16

Subject: trying to understand uint32, single or int16

From: Jose

Date: 23 Jul, 2010 15:06:15

Message: 1 of 14

Hello Guys, somebody can explain when I have to use uint32, single or int16, see code below...thanks in advance:


function [sdata]=makesdata;

global sdata

%function [sdata]=makesdata;
%creates a dummy structure sdata


sdata.ptsperchan=0;
sdata.usperadc=single(0);
sdata.stimtype=0;
sdata.datasize=0;
sdata.adcdata=[];




data =

  Columns 1 through 34

    4 36 64 0 3 0 0 0 74 36 64 0 3 0 0 0 32 0 0 0 15 0 0 0 204 42 33 1 0 0 0 0 104 249

  Columns 35 through 44

   18 0 39 39 64 0 204 42 33 1


How can iIunderstand the below code? why the use, uint 32, single and int16?

        sdata.ptsperchan=typecast(data(5:8),'uint32');
        sdata.usperadc=typecast(data(9:12),'single');
        sdata.stimtype=typecast(data(37:40),'uint32');
        sdata.datasize=typecast(data(41:44),'uint32');
        sdata.adcdata=typecast(adc,'int16');

Subject: trying to understand uint32, single or int16

From: Frédérick Cyr

Date: 23 Jul, 2010 15:27:05

Message: 2 of 14

"Jose " <jose.l.vega@gmail.com> wrote in message <i2cb57$su7$1@fred.mathworks.com>...
> Hello Guys, somebody can explain when I have to use uint32, single or int16, see code below...thanks in advance:

You can use uint32, single or int16 when you want to save memory since these datatype use respectively 32 bits, 32 bits and 16 bits instead of the usual 64 bits used by MATLAB doubles. However, you must consider that these differents data types can't hold as much information as the usual matlab doubles: uint32 is restricted to unsigned integers from 0 to (2^32 -1), single is a real number with less precision than a double and int16 is restricted to signed integers from -128 to 127.

I cannot say for sure when you MUST use it with MATLAB, but my guess is "try use the datatype that was already used for your application". By this, I mean, if you see that a structure is being filled with 32 bits unsigned integers (uint32), avoid replacing this value by a double.

> How can iIunderstand the below code? why the use, uint 32, single and int16?
>
> sdata.ptsperchan=typecast(data(5:8),'uint32');
> sdata.usperadc=typecast(data(9:12),'single');
> sdata.stimtype=typecast(data(37:40),'uint32');
> sdata.datasize=typecast(data(41:44),'uint32');
> sdata.adcdata=typecast(adc,'int16');

typecast function change a value into the specified data type (see help typecast). The instructions that you see here takes some data, convert it to uint32 or single and puts it in sdata structure field.

Subject: trying to understand uint32, single or int16

From: Frédérick Cyr

Date: 23 Jul, 2010 15:32:07

Message: 3 of 14

"Frédérick Cyr" <phraide@videotron.ca> wrote in message <i2ccc9$j5k$1@fred.mathworks.com>...

> You can use uint32, single or int16 when you want to save memory since these datatype use respectively 32 bits, 32 bits and 16 bits instead of the usual 64 bits used by MATLAB doubles. However, you must consider that these differents data types can't hold as much information as the usual matlab doubles: uint32 is restricted to unsigned integers from 0 to (2^32 -1), single is a real number with less precision than a double and int16 is restricted to signed integers from -128 to 127.

My bad, int16 are restricted to signed integers from -32768 to 32767

int8 are restricted from -128 to 127!

Subject: trying to understand uint32, single or int16

From: John D'Errico

Date: 23 Jul, 2010 15:37:04

Message: 4 of 14

"Jose " <jose.l.vega@gmail.com> wrote in message <i2cb57$su7$1@fred.mathworks.com>...
> Hello Guys, somebody can explain when I have to use uint32, single or int16, see code below...thanks in advance:
>
>
> function [sdata]=makesdata;
>
> global sdata
>
> %function [sdata]=makesdata;
> %creates a dummy structure sdata
>
>
> sdata.ptsperchan=0;
> sdata.usperadc=single(0);
> sdata.stimtype=0;
> sdata.datasize=0;
> sdata.adcdata=[];
>
>
>
>
> data =
>
> Columns 1 through 34
>
> 4 36 64 0 3 0 0 0 74 36 64 0 3 0 0 0 32 0 0 0 15 0 0 0 204 42 33 1 0 0 0 0 104 249
>
> Columns 35 through 44
>
> 18 0 39 39 64 0 204 42 33 1
>
>
> How can iIunderstand the below code? why the use, uint 32, single and int16?
>
> sdata.ptsperchan=typecast(data(5:8),'uint32');
> sdata.usperadc=typecast(data(9:12),'single');
> sdata.stimtype=typecast(data(37:40),'uint32');
> sdata.datasize=typecast(data(41:44),'uint32');
> sdata.adcdata=typecast(adc,'int16');

Because these various types of variable can handle numbers
of various magnitudes, for various goals.

For example, if your numbers will always be integers
in the range [0,255], then you can save memory by
storing them as uint8, since that takes no more than
1 byte to store. If they are known to fall in the interval
[0,65535], then uint16 is an option.

If you can stand the loss of precision by storing a floating
point number as a single rather than the default double,
then single may be for you.

The fact is, unless you are sure that you need the savings
in memory at some cost, then just leave your numbers
alone. Don't use these types unless you have a reason
to do so, as they can cause problems unless you take
care. If someone has given you code that needs a specific
class, then as the documentation specifies.

If your goal is to save cpu cycles, converting to uint8
rarely saves time, as matlab will probably do its arithmetic
as quickly with double arrays as fast as it does integers.
And the double class will represent all integers as large
as 2^53-1 exactly anyway.

John

Subject: trying to understand uint32, single or int16

From: Jose

Date: 23 Jul, 2010 15:41:04

Message: 5 of 14

 Fred, but I can´t understand why my bosss in his code use 'single' instead of 'uint32'..in this line
sdata.usperadc=typecast(data(9:12),'single');...and I want to understand it.



"Jose " <jose.l.vega@gmail.com> wrote in message <i2cb57$su7$1@fred.mathworks.com>...
> Hello Guys, somebody can explain when I have to use uint32, single or int16, see code below...thanks in advance:
>
>
> function [sdata]=makesdata;
>
> global sdata
>
> %function [sdata]=makesdata;
> %creates a dummy structure sdata
>
>
> sdata.ptsperchan=0;
> sdata.usperadc=single(0);
> sdata.stimtype=0;
> sdata.datasize=0;
> sdata.adcdata=[];
>
>
>
>
> data =
>
> Columns 1 through 34
>
> 4 36 64 0 3 0 0 0 74 36 64 0 3 0 0 0 32 0 0 0 15 0 0 0 204 42 33 1 0 0 0 0 104 249
>
> Columns 35 through 44
>
> 18 0 39 39 64 0 204 42 33 1
>
>
> How can iIunderstand the below code? why the use, uint 32, single and int16?
>
> sdata.ptsperchan=typecast(data(5:8),'uint32');
> sdata.usperadc=typecast(data(9:12),'single');
> sdata.stimtype=typecast(data(37:40),'uint32');
> sdata.datasize=typecast(data(41:44),'uint32');
> sdata.adcdata=typecast(adc,'int16');

Subject: trying to understand uint32, single or int16

From: Jose

Date: 23 Jul, 2010 15:55:22

Message: 6 of 14

Thnak you John,
i found this link can be useful to me:

http://www.rwc.uc.edu/koehler/comath/11.html

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <i2ccv0$rci$1@fred.mathworks.com>...
> "Jose " <jose.l.vega@gmail.com> wrote in message <i2cb57$su7$1@fred.mathworks.com>...
> > Hello Guys, somebody can explain when I have to use uint32, single or int16, see code below...thanks in advance:
> >
> >
> > function [sdata]=makesdata;
> >
> > global sdata
> >
> > %function [sdata]=makesdata;
> > %creates a dummy structure sdata
> >
> >
> > sdata.ptsperchan=0;
> > sdata.usperadc=single(0);
> > sdata.stimtype=0;
> > sdata.datasize=0;
> > sdata.adcdata=[];
> >
> >
> >
> >
> > data =
> >
> > Columns 1 through 34
> >
> > 4 36 64 0 3 0 0 0 74 36 64 0 3 0 0 0 32 0 0 0 15 0 0 0 204 42 33 1 0 0 0 0 104 249
> >
> > Columns 35 through 44
> >
> > 18 0 39 39 64 0 204 42 33 1
> >
> >
> > How can iIunderstand the below code? why the use, uint 32, single and int16?
> >
> > sdata.ptsperchan=typecast(data(5:8),'uint32');
> > sdata.usperadc=typecast(data(9:12),'single');
> > sdata.stimtype=typecast(data(37:40),'uint32');
> > sdata.datasize=typecast(data(41:44),'uint32');
> > sdata.adcdata=typecast(adc,'int16');
>
> Because these various types of variable can handle numbers
> of various magnitudes, for various goals.
>
> For example, if your numbers will always be integers
> in the range [0,255], then you can save memory by
> storing them as uint8, since that takes no more than
> 1 byte to store. If they are known to fall in the interval
> [0,65535], then uint16 is an option.
>
> If you can stand the loss of precision by storing a floating
> point number as a single rather than the default double,
> then single may be for you.
>
> The fact is, unless you are sure that you need the savings
> in memory at some cost, then just leave your numbers
> alone. Don't use these types unless you have a reason
> to do so, as they can cause problems unless you take
> care. If someone has given you code that needs a specific
> class, then as the documentation specifies.
>
> If your goal is to save cpu cycles, converting to uint8
> rarely saves time, as matlab will probably do its arithmetic
> as quickly with double arrays as fast as it does integers.
> And the double class will represent all integers as large
> as 2^53-1 exactly anyway.
>
> John

Subject: trying to understand uint32, single or int16

From: Walter Roberson

Date: 23 Jul, 2010 15:58:22

Message: 7 of 14

Jose wrote:
> Fred, but I can´t understand why my bosss in his code use 'single'
> instead of 'uint32'..in this line
> sdata.usperadc=typecast(data(9:12),'single');...and I want to understand
> it.

It's a byte array derived from some binary interface (or a simulation of
a binary interface). data(9:12) extracts four bytes from the data and
re-interprets that as a single precision floating point number, because
the binary interface happens to send a single precision floating point
number at that point in the array.

Subject: trying to understand uint32, single or int16

From: Steven_Lord

Date: 23 Jul, 2010 16:08:28

Message: 8 of 14



"Jose " <jose.l.vega@gmail.com> wrote in message
news:i2cd6g$d41$1@fred.mathworks.com...
> Fred, but I cant understand why my bosss in his code use 'single'
> instead of 'uint32'..in this line
> sdata.usperadc=typecast(data(9:12),'single');...and I want to understand
> it.

The easiest way to understand that is to ask your boss why he chose to do it
that way.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: trying to understand uint32, single or int16

From: Jose

Date: 23 Jul, 2010 16:28:23

Message: 9 of 14

Walter, this is the data send from the master to the slave:

case sm_adc

%sending 4 bytes;
--------
%sending 40 bytes:

data=data(1:40)
ptsperchan=byte2long(data(1:4));
%data(5:8)..my boss says...jose..is usperchan--number of microseonds per sample, held as a single precision floating point number --need to write a routine byte2single ---check the web for this format...Can you helpme with it too?

stimname=byte2string(data(9:29)))
stimtype=byte2long(data(33:36));
datasize=byte2long(data(37:40));

And this is the information receive in my slave (the original code of my first message):

case sm_adc
 
% receiving 44 bytes
        sdata.ptsperchan=typecast(data(5:8),'uint32');
        sdata.usperadc=typecast(data(9:12),'single');
        sdata.stimname=byte2string(data(13:33));
        sdata.stimtype=typecast(data(37:40),'uint32');
        sdata.datasize=typecast(data(41:44),'uint32');
        adc=msrecvraw (sock,sdata.datasize);

it has sense now?...please can you explain to me it better....I think He wrote single in usperadc, just to supply the subrutine byte2single that he was looking for.

Walter Roberson <roberson@hushmail.com> wrote in message <y_i2o.22765$lS1.4988@newsfe12.iad>...
> Jose wrote:
> > Fred, but I can´t understand why my bosss in his code use 'single'
> > instead of 'uint32'..in this line
> > sdata.usperadc=typecast(data(9:12),'single');...and I want to understand
> > it.
>
> It's a byte array derived from some binary interface (or a simulation of
> a binary interface). data(9:12) extracts four bytes from the data and
> re-interprets that as a single precision floating point number, because
> the binary interface happens to send a single precision floating point
> number at that point in the array.

Subject: trying to understand uint32, single or int16

From: Frédérick Cyr

Date: 23 Jul, 2010 16:35:21

Message: 10 of 14

"Jose " <jose.l.vega@gmail.com> wrote in message <i2cd6g$d41$1@fred.mathworks.com>...
> Fred, but I can´t understand why my bosss in his code use 'single' instead of 'uint32'..in this line
> sdata.usperadc=typecast(data(9:12),'single');...and I want to understand it.

It is true that given what we know about the code right now, "uint32" could store the data as well as a "single" since sdata.usperadc is filled with integers. However, sdata.usperadc is initialized with

sdata.usperadc=single(0);

This way, you boss specified that for a reason or another, he needed sdata.usperadc to be a single precision floating point variable. We can't know why from what we currently see in the code but it could be because he uses this variable with an algorithm that needs single precision floating point datatype or that he expects this variable to contain real numbers instead of integers at some point in the process or for some specific input.

This is what I meant by saying that we should use the appropriate datatype for our variables/functions if it is specified. Using the wrong datatype may cause a loss of data or errors.

Subject: trying to understand uint32, single or int16

From: Walter Roberson

Date: 23 Jul, 2010 16:37:31

Message: 11 of 14

Jose wrote:

> %data(5:8)..my boss says...jose..is usperchan--number of microseonds per
> sample, held as a single precision floating point number --need to write
> a routine byte2single ---check the web for this format...Can you helpme
> with it too?

byte2single = @(b) typecast(b, 'single');

However, this depends upon the order of the bytes being the same between
the master and slave. If you cannot be certain of that, then you will
need to order the bytes into some known order for transmission, with the
master or slave (or both) re-ordering the bytes to or from the common
order. The standard common transmission format is most significant byte
first, but Intel-type processors use a different internal ordering;
swapbytes() might help.

Re-ordering for transmission is not needed if the master and slave use
the same processor family.

Subject: trying to understand uint32, single or int16

From: Jose

Date: 23 Jul, 2010 16:38:04

Message: 12 of 14

sorry, I forgot:

function [longint]=byte2long (d);
data=double(d);

longint=data(1)+data(2)*(2^8)+data(3)*(2^16)+data(4)*(2^24);

function [strout]=byte2string (data);
data
i=find(data==0);

if length(i)==0
    strout=[];
else
'que fuerte'
    strout=char(data(1:i)')
end


"Jose " <jose.l.vega@gmail.com> wrote in message <i2cfv7$d6u$1@fred.mathworks.com>...
> Walter, this is the data send from the master to the slave:
>
> case sm_adc
>
> %sending 4 bytes;
> --------
> %sending 40 bytes:
>
> data=data(1:40)
> ptsperchan=byte2long(data(1:4));
> %data(5:8)..my boss says...jose..is usperchan--number of microseonds per sample, held as a single precision floating point number --need to write a routine byte2single ---check the web for this format...Can you helpme with it too?
>
> stimname=byte2string(data(9:29)))
> stimtype=byte2long(data(33:36));
> datasize=byte2long(data(37:40));
>
> And this is the information receive in my slave (the original code of my first message):
>
> case sm_adc
>
> % receiving 44 bytes
> sdata.ptsperchan=typecast(data(5:8),'uint32');
> sdata.usperadc=typecast(data(9:12),'single');
> sdata.stimname=byte2string(data(13:33));
> sdata.stimtype=typecast(data(37:40),'uint32');
> sdata.datasize=typecast(data(41:44),'uint32');
> adc=msrecvraw (sock,sdata.datasize);
>
> it has sense now?...please can you explain to me it better....I think He wrote single in usperadc, just to supply the subrutine byte2single that he was looking for.
>
> Walter Roberson <roberson@hushmail.com> wrote in message <y_i2o.22765$lS1.4988@newsfe12.iad>...
> > Jose wrote:
> > > Fred, but I can´t understand why my bosss in his code use 'single'
> > > instead of 'uint32'..in this line
> > > sdata.usperadc=typecast(data(9:12),'single');...and I want to understand
> > > it.
> >
> > It's a byte array derived from some binary interface (or a simulation of
> > a binary interface). data(9:12) extracts four bytes from the data and
> > re-interprets that as a single precision floating point number, because
> > the binary interface happens to send a single precision floating point
> > number at that point in the array.

Subject: trying to understand uint32, single or int16

From: Jose

Date: 23 Jul, 2010 16:52:04

Message: 13 of 14

Ok, guys and walter thank you for your time and your information...i'll try to sort out it...cheers.

"Jose " <jose.l.vega@gmail.com> wrote in message <i2cghc$jir$1@fred.mathworks.com>...
> sorry, I forgot:
>
> function [longint]=byte2long (d);
> data=double(d);
>
> longint=data(1)+data(2)*(2^8)+data(3)*(2^16)+data(4)*(2^24);
>
> function [strout]=byte2string (data);
> data
> i=find(data==0);
>
> if length(i)==0
> strout=[];
> else
> 'que fuerte'
> strout=char(data(1:i)')
> end
>
>
> "Jose " <jose.l.vega@gmail.com> wrote in message <i2cfv7$d6u$1@fred.mathworks.com>...
> > Walter, this is the data send from the master to the slave:
> >
> > case sm_adc
> >
> > %sending 4 bytes;
> > --------
> > %sending 40 bytes:
> >
> > data=data(1:40)
> > ptsperchan=byte2long(data(1:4));
> > %data(5:8)..my boss says...jose..is usperchan--number of microseonds per sample, held as a single precision floating point number --need to write a routine byte2single ---check the web for this format...Can you helpme with it too?
> >
> > stimname=byte2string(data(9:29)))
> > stimtype=byte2long(data(33:36));
> > datasize=byte2long(data(37:40));
> >
> > And this is the information receive in my slave (the original code of my first message):
> >
> > case sm_adc
> >
> > % receiving 44 bytes
> > sdata.ptsperchan=typecast(data(5:8),'uint32');
> > sdata.usperadc=typecast(data(9:12),'single');
> > sdata.stimname=byte2string(data(13:33));
> > sdata.stimtype=typecast(data(37:40),'uint32');
> > sdata.datasize=typecast(data(41:44),'uint32');
> > adc=msrecvraw (sock,sdata.datasize);
> >
> > it has sense now?...please can you explain to me it better....I think He wrote single in usperadc, just to supply the subrutine byte2single that he was looking for.
> >
> > Walter Roberson <roberson@hushmail.com> wrote in message <y_i2o.22765$lS1.4988@newsfe12.iad>...
> > > Jose wrote:
> > > > Fred, but I can´t understand why my bosss in his code use 'single'
> > > > instead of 'uint32'..in this line
> > > > sdata.usperadc=typecast(data(9:12),'single');...and I want to understand
> > > > it.
> > >
> > > It's a byte array derived from some binary interface (or a simulation of
> > > a binary interface). data(9:12) extracts four bytes from the data and
> > > re-interprets that as a single precision floating point number, because
> > > the binary interface happens to send a single precision floating point
> > > number at that point in the array.

Subject: trying to understand uint32, single or int16

From: Walter Roberson

Date: 23 Jul, 2010 18:47:28

Message: 14 of 14

Jose wrote:
> sorry, I forgot:
>
> function [longint]=byte2long (d);
> data=double(d);
>
> longint=data(1)+data(2)*(2^8)+data(3)*(2^16)+data(4)*(2^24);

That code is not able to create signed values as is suggested by its name
(since the name does not indicate "unsigned").

The code also produces a double rather than an integer data type such as is
suggested by its name.

The code appears to be assuming the data is in "little-endian" byte order,
such as is used on Intel processors, but does not assume that the processor it
is operating on is little endian. That is unusual, in that binary data
transfer interfaces are most often written to use big-endian order (google
"network byte order"), but there are some instances in which it would be
reasonable.


> function [strout]=byte2string (data);
> data
> i=find(data==0);
>
> if length(i)==0
> strout=[];
> else
> 'que fuerte'
> strout=char(data(1:i)')
> end

That code is peculiar. I gather that it must be assuming that the string
should be NUL terminated and is producing the empty array if it is not. If,
though, the data happens to contain multiple NUL (decimal 0's) values then the
routine will bomb.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us