Example: Converting from Doubles to Fixed Point

About This Example

The purpose of this example is to show you how to simulate a continuous real-world doubles signal using a generalized fixed-point data type. Although simple in design, the model gives you an opportunity to explore many of the important features of the Simulink® Fixed Point™ software, including

The model used in this example is given by the fxpdemo_dbl2fix demo. You can launch this demo by typing its name at the MATLAB® command line:

fxpdemo_dbl2fix

The model is shown in the following figure.

The sections that follow describe the model and its simulation results.

Block Descriptions

For purposes of this documentation example, the Signal Generator block is configured to output a sine wave signal with an amplitude defined on the interval [-5 5]. It always outputs double-precision numbers.

The function of the Data Type Conversion (Dbl-to-FixPt) block is to convert the double-precision numbers from the Signal Generator block into one of the Simulink Fixed Point data types. For simplicity, its output signal is limited to 5 bits in this example.

The function of the Data Type Conversion (FixPt-to-Dbl) block is to convert one of the Simulink Fixed Point data types into a Simulink® data type. In this example, it outputs double-precision numbers.

Simulation Results

The results of two simulation trials are given below. The first trial uses binary-point-only scaling while the second trial uses [Slope Bias] scaling.

Trial 1: Binary-Point-Only Scaling

When using binary-point-only scaling, your goal is to find the optimal power-of-two exponent E, as defined in Scaling. For this scaling mode, the fractional slope F is set to 1 and no bias is required.

The Data Type Conversion (Dbl-to-FixPt) block is configured in this way:

The resulting real-world and fixed-point simulation results are shown in the following figure.

The simulation clearly demonstrates the quantization effects of fixed-point arithmetic. The combination of using a 5-bit word with a precision of (1/2)2 = 0.25 produces a discretized output that does not span the full range of the input signal.

If you want to span the complete range of the input signal with 5 bits using binary-point-only scaling, then your only option is to sacrifice precision. Hence, the output scaling would be given by 2^-1, which puts the binary point one place to the left of the rightmost bit. This scaling gives a maximum value of 0111.1 = 7.5, a minimum value of 1000.0 = -8.0, and a precision of (1/2)1 = 0.5.

Trial 2: [Slope Bias] Scaling

When using [Slope Bias] scaling, your goal is to find the optimal fractional slope F and fixed power-of-two exponent E, as defined in Scaling. No bias is required for this example because the sine wave is defined on the interval [-5 5]. The Data Type Conversion (Dbl-to-FixPt) block configuration is the same as that of the previous trial except for the scaling.

To arrive at a value for the slope, you can begin by assuming a fixed power-of-two exponent of -2. In the previous trial, this value defined the binary-point-only scaling and resulted in a precision of 0.25. To find the fractional slope, you divide the maximum value of the sine wave by the maximum value of the scaled 5-bit number. The result is 5.00/3.75 = 1.3333. The slope (and precision) is 1.3333.(0.25) = 0.3333. You specify the [Slope Bias] scaling as [0.3333 0] by entering the expression fixdt(1,5,0.3333,0) as the value of the Output data type parameter.

Of course, you could have specified a fixed power-of-two exponent of -1 and a corresponding fractional slope of 0.6667. Naturally, the resulting slope is the same since E was reduced by one bit but F was increased by one bit. In this case, the blockset would automatically store F as 1.3332 and E as -2 because of the normalization condition of 1 ≤ F < 2.

The resulting real-world and fixed-point simulation results are shown in the following figure.

This somewhat cumbersome process used to find the slope is not really necessary. All that is required is the range of the data you are simulating and the size of the fixed-point word used in the simulation. In general, you can achieve reasonable simulation results by selecting your scaling based on the formula

where

For this example, the formula produces a slope of 0.32258.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS