Discussion:
Fixed point number truncation ... Help !!!!!
(too old to reply)
José María
2010-07-27 21:17:19 UTC
Permalink
Hi,

I have a signed fixed point number N1 with 11 bits wordlength 9 fraction bits, and I need to truncate N1 to obtain a signed fixed point number N2 with 8 bits wordlength 6 fraction bits, it is, N2 is N1 but without the 3 LSB bits, here an example:

N1 =

-0.0020

DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 11
FractionLength: 9

RoundMode: round
OverflowMode: saturate
ProductMode: FullPrecision
MaxProductWordLength: 128
SumMode: FullPrecision
MaxSumWordLength: 128
CastBeforeSum: true

K>> bin(N1)
ans =11111111111

What I need is a signed fixed point number bin(N2)= 11111111, Do you now some function ??? it is a truncation but in bits level...

Thanks !!!!!
Ashok Charry
2010-07-27 22:16:16 UTC
Permalink
Hi Jose,

If you set the 'RoundMode' of the fi object N2 to 'Floor' you will get
what you want:

N1 = fi(-0.0020,1,11,9,'RoundMode','floor');
bin(N1)
% 11111111111
N2 = fi(0,1,8,6,'RoundMode','floor');
N2(1) = N1;
bin(N2)
% 11111111

Hope that helps.


Ashok Charry
Fixed-Point Engineer.
MathWorks
Post by José María
Hi,
I have a signed fixed point number N1 with 11 bits wordlength 9 fraction
bits, and I need to truncate N1 to obtain a signed fixed point number N2
with 8 bits wordlength 6 fraction bits, it is, N2 is N1 but without the
N1 =
-0.0020
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 11
FractionLength: 9
RoundMode: round
OverflowMode: saturate
ProductMode: FullPrecision
MaxProductWordLength: 128
SumMode: FullPrecision
MaxSumWordLength: 128
CastBeforeSum: true
K>> bin(N1)
ans =11111111111
What I need is a signed fixed point number bin(N2)= 11111111, Do you now
some function ??? it is a truncation but in bits level...
Thanks !!!!!
José María
2010-07-27 23:17:06 UTC
Permalink
Post by Ashok Charry
Hi Jose,
If you set the 'RoundMode' of the fi object N2 to 'Floor' you will get
N1 = fi(-0.0020,1,11,9,'RoundMode','floor');
bin(N1)
% 11111111111
N2 = fi(0,1,8,6,'RoundMode','floor');
N2(1) = N1;
bin(N2)
% 11111111
Hope that helps.
Ashok Charry
Fixed-Point Engineer.
MathWorks
Post by José María
Hi,
I have a signed fixed point number N1 with 11 bits wordlength 9 fraction
bits, and I need to truncate N1 to obtain a signed fixed point number N2
with 8 bits wordlength 6 fraction bits, it is, N2 is N1 but without the
N1 =
-0.0020
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 11
FractionLength: 9
RoundMode: round
OverflowMode: saturate
ProductMode: FullPrecision
MaxProductWordLength: 128
SumMode: FullPrecision
MaxSumWordLength: 128
CastBeforeSum: true
K>> bin(N1)
ans =11111111111
What I need is a signed fixed point number bin(N2)= 11111111, Do you now
some function ??? it is a truncation but in bits level...
Thanks !!!!!
Ok !!!!! It was the solution, thanks !!!!!!
Dinesh
2013-08-13 11:01:11 UTC
Permalink
Hi,

I have tried the following.

a = fi(6.375,1,8,4,'Roundmode','floor');
b = fi(0,1,6,3,'Roundmode','floor');
b(1) = a
b =

3.8750

DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 6
FractionLength: 3

RoundingMethod: Floor
OverflowAction: Saturate
ProductMode: FullPrecision
SumMode: FullPrecision
a.bin
ans =

01100110
b.bin
ans =

011111

But the answer that I expected was 011011
Can you please tell me if I am missing anything
Tom Bryan
2013-08-13 12:11:07 UTC
Permalink
The range of b is -4 to 3.875. It overflowed and saturated when you assigned a value of 6.375 into it.

Best wishes,
Tom Bryan
Post by Dinesh
Hi,
I have tried the following.
a = fi(6.375,1,8,4,'Roundmode','floor');
b = fi(0,1,6,3,'Roundmode','floor');
b(1) = a
b =
3.8750
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 6
FractionLength: 3
RoundingMethod: Floor
OverflowAction: Saturate
ProductMode: FullPrecision
SumMode: FullPrecision
a.bin
ans =
01100110
b.bin
ans =
011111
But the answer that I expected was 011011
Can you please tell me if I am missing anything
Dinesh
2013-08-14 04:46:15 UTC
Permalink
Hi,

Thank you for the reply.

Can you please let me know which technique does matlab use when we want to decrease the word length and fraction length of a fixed point.

For example, I have a signed fixed point (WordLength = 8, Fraction Length = 4)
Suppose a.bin = 10110110

If I want to convert a to it to (WordLength = 6, Fraction Length = 3), Which method does Matlab follow?
Can you please suggest some documentation on how the algorithm works.

Thanks in advance.
Tom Bryan
2013-08-14 12:34:26 UTC
Permalink
Assigning it to b = fi(0,1,6,3) is one way, but, as you saw, the cast is by "real world value" and overflowed in your last example.

If you want to extract a particular set of bits, then see the documentation for BITSLICEGET

http://www.mathworks.com/help/fixedpoint/ref/bitsliceget.html

Function bitsliceget always returns fraction length 0, so you can follow that with REINTERPRETCAST

http://www.mathworks.com/help/fixedpoint/ref/reinterpretcast.html

You can also do bit-shifts and logical operations if you want to manipulate the word and extract bits like you would in C. See the documentation for BITSRA, BITCMP, BITGET, BITOR, BITSET, BITSHIFT, BITXOR.
Post by Dinesh
Hi,
Thank you for the reply.
Can you please let me know which technique does matlab use when we want to decrease the word length and fraction length of a fixed point.
For example, I have a signed fixed point (WordLength = 8, Fraction Length = 4)
Suppose a.bin = 10110110
If I want to convert a to it to (WordLength = 6, Fraction Length = 3), Which method does Matlab follow?
Can you please suggest some documentation on how the algorithm works.
Thanks in advance.
Loading...