Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab : how to perform fixed-point arithmetic without expanding underlying data type?

Google is silent on this issue. I'm currently implementing a numerical calculator on only 16-bit signed fixed point in Matlab. But arithmetic operation on 16bit fixed point causes expanding data type to the following

>> a = int16(1.5 * 4)
a = 6
>> T = numerictype(1, 16, 2)

T = DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 2
>> dis = reinterpretcast(a, T)

dis = 1.5000

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 2
>> c = dis * dis

c = 2.2500

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 32
        FractionLength: 4

i wish the variable c stays in WordLength 16, FractionLength 2. Is it possible that arithmetic operation on 16bit fixed point is done without expanding underlying data type? I'm going to take the risk of any overflow and underflow. Any help would be awesome.

EDIT : typing fimath into command window cause error. Why does this error occur?

>> F = fimath('OverflowAction','Wrap', 'RoundingMethod', 'Floor', ...
           'ProductWordLength', 16, 'ProductFractionLength', 2);
No public field OverflowAction exists for class embedded.fimath.

Error in fimath (line 72)
  this.(varargin{k}) = varargin{k+1};
like image 287
inherithandle Avatar asked Sep 25 '13 10:09

inherithandle


People also ask

Which is the correct notation for fixed-point Matlab?

Notation. The fixed-point numeric object is called fi because J.H.

How do you do fixed-point addition?

The addition of fixed-point numbers requires that the binary points of the addends be aligned. The addition is then performed using binary arithmetic so that no number other than 0 or 1 is used. The default global fimath has a value of 1 (true) for the CastBeforeSum property.

What is fixed-point data type?

A fixed-point data type is characterized by the word length in bits, the position of the binary point, and whether it is signed or unsigned. The position of the binary point is the means by which fixed-point values are scaled and interpreted.

What is meant by fixed-point arithmetic give example?

In computing, fixed-point refers to a method of representing fractional (non-integer) numbers by storing a fixed number of digits of their fractional part. Dollar amounts, for example, are often stored with exactly two fractional digits, representing the cents (1/100 of dollar).


1 Answers

Local solution: You can use an fimath object to specify the precision of the result of a product:

F = fimath('OverflowMode','Wrap', 'RoundMode', 'Floor', ...
           'ProductMode', 'SpecifyPrecision', ...
           'ProductWordLength', 16, 'ProductFractionLength', 2);
dis.fimath = F;

Then the result will be:

>> dis*dis
ans =
         2.25

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 2

             RoundMode: floor
          OverflowMode: wrap
           ProductMode: SpecifyPrecision
     ProductWordLength: 16
 ProductFractionLength: 2
               SumMode: FullPrecision
      MaxSumWordLength: 128

Global solution: Alternatively, if you want this to apply to all your fixedpoint variables you can use

globalfimath('OverflowMode','Wrap', 'RoundMode', 'Floor', ...
             'ProductMode', 'SpecifyPrecision', ...
             'ProductWordLength', 16, 'ProductFractionLength', 2);

Note that to limit to 16 bit you need to specify the precision for sum as well (using SumMode and SumWordLength properties of fimath) otherwise the sum will have a word length of 17:

>> dis+dis
ans =
     3

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 17
        FractionLength: 2
like image 127
Mohsen Nosratinia Avatar answered Sep 30 '22 10:09

Mohsen Nosratinia