From: Jose on 23 Jul 2010 11:55 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(a)rochester.rr.com> wrote in message <i2ccv0$rci$1(a)fred.mathworks.com>... > "Jose " <jose.l.vega(a)gmail.com> wrote in message <i2cb57$su7$1(a)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
From: Walter Roberson on 23 Jul 2010 11:58 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.
From: Steven_Lord on 23 Jul 2010 12:08 "Jose " <jose.l.vega(a)gmail.com> wrote in message news:i2cd6g$d41$1(a)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. The easiest way to understand that is to ask your boss why he chose to do it that way. -- Steve Lord slord(a)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
From: Jose on 23 Jul 2010 12:28 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(a)hushmail.com> wrote in message <y_i2o.22765$lS1.4988(a)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.
From: Frédérick Cyr on 23 Jul 2010 12:35 "Jose " <jose.l.vega(a)gmail.com> wrote in message <i2cd6g$d41$1(a)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.
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: not a valid symbolic expression Next: remove horizontal shifts in image |