From: José María on 2 Aug 2010 09:03 Does Anyone knows how to convert a binary number to fixed point number specifying integer and fraction bits ?? for example , I have the binary number 11101 and I need to convert to fixed point with 1 integer bit and 4 fraction bits: 11101 = 1.825; Now, using the same binary number I need to convert to fixed point with 3 integer bits and 2 fraction bits: 11101 = 7.25 Help me please !!!! Thanks !!!
From: Steve Conahan on 2 Aug 2010 09:32 "José María " <chema15(a)hotmail.com> wrote in message news:i36fmc$sdp$1(a)fred.mathworks.com... > Does Anyone knows how to convert a binary number to fixed point number > specifying integer and fraction bits ?? > > for example , I have the binary number 11101 and I need to convert to > fixed point with 1 integer bit and 4 fraction bits: 11101 = 1.825; Now, > using the same binary number I need to convert to fixed point with 3 > integer bits and 2 fraction bits: 11101 = 7.25 > > Help me please !!!! > > Thanks !!! Hello, I am not sure what you mean by "convert" to fixed-point, as the binary representations that you are mentioning are already representing fixed-point data. It is just a binary representation of the values, with a certain fraction length association (also known as a "binary point"). This conversion could be done fairly easily on paper, or in MATLAB, by considering the equivalent data word length (total number of bits) and the notional fraction length (number of fractional bits) that you want to use. Here are some examples in MATLAB that might help explain this a little more, using your example values above: % Define your word length and fraction length >> num_integer_bits = 1; >> num_fractional_bits = 4; >> word_length = num_integer_bits + num_fractional_bits word_length = 5 % Put your real-world value into an unsigned fixed-point type >> a = ufi(1.825, word_length, num_fractional_bits) a = 1.8125 DataTypeMode: Fixed-point: binary point scaling Signedness: Unsigned WordLength: 5 FractionLength: 4 % View its binary representation >> bin(a) ans = 11101 % View its integer representation >> int(a) ans = 29 % View its least significant bit (LSB) value >> eps(a) ans = 0.0625 % Now let's do the same thing using 7.25, but this time with 3 integer bits and 2 fraction bits: >> b = ufi(7.25, 5, 2) b = 7.2500 DataTypeMode: Fixed-point: binary point scaling Signedness: Unsigned WordLength: 5 FractionLength: 2 >> bin(b) ans = 11101 >> int(b) ans = 29 >> eps(b) ans = 0.2500 You may also want to visit the Fixed-Point Toolbox online documentation for some more information and tutorials on fixed-point binary point scaling representation: http://www.mathworks.com/access/helpdesk/help/toolbox/fixedpoint/ HTH, Steve Conahan MathWorks Development
From: Steve Conahan on 2 Aug 2010 09:47 "Steve Conahan" <sconahan(a)mathworks.com> wrote in message news:i36hdm$ki9$1(a)fred.mathworks.com... > "José María " <chema15(a)hotmail.com> wrote in message > news:i36fmc$sdp$1(a)fred.mathworks.com... >> Does Anyone knows how to convert a binary number to fixed point number >> specifying integer and fraction bits ?? >> >> for example , I have the binary number 11101 and I need to convert to >> fixed point with 1 integer bit and 4 fraction bits: 11101 = 1.825; Now, >> using the same binary number I need to convert to fixed point with 3 >> integer bits and 2 fraction bits: 11101 = 7.25 >> >> Help me please !!!! >> >> Thanks !!! > > Hello, > > I am not sure what you mean by "convert" to fixed-point, as the binary > representations that you are mentioning are already representing > fixed-point data. It is just a binary representation of the values, with a > certain fraction length association (also known as a "binary point"). > This conversion could be done fairly easily on paper, or in MATLAB, by > considering the equivalent data word length (total number of bits) and the > notional fraction length (number of fractional bits) that you want to use. > > Here are some examples in MATLAB that might help explain this a little > more, using your example values above: > > % Define your word length and fraction length >>> num_integer_bits = 1; >>> num_fractional_bits = 4; >>> word_length = num_integer_bits + num_fractional_bits > word_length = > 5 > > % Put your real-world value into an unsigned fixed-point type >>> a = ufi(1.825, word_length, num_fractional_bits) > a = > 1.8125 > > DataTypeMode: Fixed-point: binary point scaling > Signedness: Unsigned > WordLength: 5 > FractionLength: 4 > > % View its binary representation >>> bin(a) > ans = > 11101 > > % View its integer representation >>> int(a) > ans = > 29 > > % View its least significant bit (LSB) value >>> eps(a) > ans = > 0.0625 > > > % Now let's do the same thing using 7.25, but this time with 3 integer > bits and 2 fraction bits: > >>> b = ufi(7.25, 5, 2) > b = > 7.2500 > > DataTypeMode: Fixed-point: binary point scaling > Signedness: Unsigned > WordLength: 5 > FractionLength: 2 > >>> bin(b) > ans = > 11101 > >>> int(b) > ans = > 29 > >>> eps(b) > ans = > 0.2500 > > > You may also want to visit the Fixed-Point Toolbox online documentation > for some more information and tutorials on fixed-point binary point > scaling representation: > > http://www.mathworks.com/access/helpdesk/help/toolbox/fixedpoint/ > > HTH, > Steve Conahan > MathWorks Development > Hi again - here are two more examples for you (these may be more direct): >> ufi(bin2dec('11101')*eps(ufi(0,5,4)), 5, 4) ans = 1.8125 DataTypeMode: Fixed-point: binary point scaling Signedness: Unsigned WordLength: 5 FractionLength: 4 >> ufi(bin2dec('11101')*eps(ufi(0,5,2)), 5, 2) ans = 7.2500 DataTypeMode: Fixed-point: binary point scaling Signedness: Unsigned WordLength: 5 FractionLength: 2 Cheers, Steve
|
Pages: 1 Prev: create and parse mdl file programatically Next: .bash_profile |