Compute Slope and Bias

What Is Slope Bias Scaling?

With slope bias scaling, you must specify the slope and bias of a number. The real-world value of a slope bias scaled number can be represented by:

real-world value=(slope×integer)+bias

slope=slope adjustment factor×2fixed exponent

Compute Slope and Bias

Start with the endpoints that you want, signedness, and word length.

lower_bound = 999;
upper_bound = 1000;
is_signed = true;
word_length = 16;

To find the range of a fi object with a specified word length and signedness, use the range function.

[Q_min, Q_max] = range(fi([], is_signed, word_length, 0));

To find the slope and bias, solve the system of equations:

lower_bound = slope * Q_min + bias

upper_bound = slope * Q_max + bias

Rewrite these equations in matrix form.

[lower_boundupper_bound]=[Q_min1Q_max1]×[slopebias]

Solve for the slope and bias.

A = double ([Q_min, 1; Q_max, 1]);
b = double ([lower_bound; upper_bound]);
x = A\b;
format long g

To find the slope, or precision, call the first element of the slope-bias vector, x.

slope = x(1)
slope =

      1.52590218966964e-05

To find the bias, call the second element of vector x.

bias = x(2)
bias =

          999.500007629511

Create a numerictype object with slope bias scaling.

T = numerictype(is_signed, word_length, slope, bias)
T =
 

          DataTypeMode: Fixed-point: slope and bias scaling
            Signedness: Signed
            WordLength: 16
                 Slope: 1.5259021896696368e-5
                  Bias: 999.500007629511

Create a fi object with numerictype T.

a = fi(999.255, T)
a = 

          999.254993514916

          DataTypeMode: Fixed-point: slope and bias scaling
            Signedness: Signed
            WordLength: 16
                 Slope: 1.5259021896696368e-5
                  Bias: 999.500007629511

Verify that the fi object that you created has the correct specifications by finding the range of a.

range(a)
ans = 

         999        1000

          DataTypeMode: Fixed-point: slope and bias scaling
            Signedness: Signed
            WordLength: 16
                 Slope: 1.5259021896696368e-5
                  Bias: 999.500007629511