Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

convert LSW and MSW to floating point

266 views
Skip to first unread message

Vihang Patil

unread,
Apr 13, 2012, 10:28:18 AM4/13/12
to
Hello
I am getting an array of values from an encoder which is attached to an Data Acquisition Device. The values that I get are in 2 arrays of LSW's amd MSW's
I want that the data to be converted in to floating point representation so that I can use it later for plotting data
I have also made a function to achieve this but the problem is that the function contains bin2dec() which is very slow and due to this my Data Acquisition device throws me an error of Buffer OverRun

function angle = encoder_angle(val)
%Function to calculate Angle from encoder
MSB = val(:,1);
LSB = val(:,2);

LSB = dec2bin(LSB,16);
MSB = dec2bin(MSB,16);
quad_angle = bin2dec([MSB LSB]); % this is slowing my DAQ device
[quad_angle] = TwosComplement(quad_angle, 32);
angle = quad_angle*360/144000; % encoder PPR is 36000, my DAQ sends (36000* 4)
angle = angle';

example:
Q1 =[0,65497;0,65503;0,65509;0,65517;0,65523;0,65529;1,1;1,7;1,13;1,21;1,27;]
angle = encoder_angle(Q1)
angle =

163.7425 163.7575 163.7725 163.7925 163.8075 163.8225 163.8425 163.8575 163.8725 163.8925 163.9075


I need the above function to operate much faster..
Thanks

Vihang Patil

Roger Stafford

unread,
Apr 13, 2012, 1:02:32 PM4/13/12
to
"Vihang Patil" <vihang...@yahoo.com> wrote in message <jm9d62$hg1$1...@newscl01ah.mathworks.com>...
> LSB = dec2bin(LSB,16);
> MSB = dec2bin(MSB,16);
> quad_angle = bin2dec([MSB LSB]); % this is slowing my DAQ device
- - - - - - - - -
Instead of converting to and from binary strings, why not do this:

quad_angle = 2^16*val(:,1)+val(:,2);

Roger Stafford

James Tursa

unread,
Apr 13, 2012, 1:02:32 PM4/13/12
to
What is class(val) ?

Vihang Patil

unread,
Apr 14, 2012, 7:01:22 AM4/14/12
to
"James Tursa" wrote in message <jm9m78$rnt$1...@newscl01ah.mathworks.com>...
> What is class(val) ?

val is of class --> double

Thanks

Vihang

Vihang Patil

unread,
Apr 14, 2012, 7:56:14 AM4/14/12
to

> Instead of converting to and from binary strings, why not do this:
>
> quad_angle = 2^16*val(:,1)+val(:,2);
>
> Roger Stafford

Thanks Roger..
That helped

Vihang

James Tursa

unread,
Apr 14, 2012, 2:15:30 PM4/14/12
to
"Vihang Patil" <vihang...@yahoo.com> wrote in message <jmble2$bq2$1...@newscl01ah.mathworks.com>...
Why is it double if you are importing it from a device and you are attempting to use the "bits"? I would have expected it to be uint16. Did you convert it to double? And why are the MSB and LSB separated in memory? I would have expected them to be next to each other.

James Tursa
0 new messages