Contents

Propose Data Types Based on Derived Ranges

This example shows how to propose fixed-point data types based on static ranges that you specify. The advantage of proposing data types based on derived ranges is that you do not have to provide test files that exercise your algorithm over its full operating range. Running such test files often takes a very long time so you can save time by deriving ranges instead.

Prerequisites

To complete this example, you must install the following products:

  • MATLAB®

  • Fixed-Point Designer™

  • C compiler (for most platforms, a default C compiler is supplied with MATLAB) For a list of supported compilers, see Supported Compilers

    You can use mex -setup to change the default compiler. See Changing Default Compiler.

Create a New Folder and Copy Relevant Files

  1. Create a local working folder, for example, c:\dti.

  2. Change to the docroot\toolbox\fixpoint\examples folder. At the MATLAB command line, enter:

    cd(fullfile(docroot, 'toolbox', 'fixpoint', 'examples')) 
  3. Copy the dti.m and dti_test.m files to your local working folder.

    It is best practice to create a separate test script to do all the pre- and post-processing such as loading inputs, setting up input values, calling the function under test, and outputting test results.

    TypeNameDescription
    Function codedti.mEntry-point MATLAB function
    Test filedti_test.mMATLAB script that tests dti.m

 The dti Function

 The dti_test Function

Open the Fixed-Point Converter App

  1. At the MATLAB command line, enter

    fixedPointConverter
  2. In the Fixed-Point Converter app, select the Select Source Files step.

  3. Click Select Source Files. Browse to the file dti.m and then click OK to add the file to the project and check the file for potential code generation issues.

    By default, the app saves information and settings for this project in the current folder in a file named dti.prj. It detects no potential issues with the dti.m file.

Define Input Types

  1. Click the arrow on the right side of the app to navigate to the next step in the workflow.

  2. Browse to add dti_test as a test file and then click .

    The test file runs and displays the outputs of the filter for each of the input signals.

    The app determines from the test file that the input type of u_in is double(1x1).

Fixed-Point Conversion

  1. Click the arrow on the right side of the app to navigate to the next step in the workflow.

    The Fixed-Point Converter app generates an instrumented MEX function for your entry-point MATLAB function. After generating the MEX function, the app displays compiled information — type, size, and complexity — for variables in your code. For more information, see View and Modify Variable Information.

    If the MEX function generation fails, the app provides error message links to help you navigate to the code that caused the build issues. If your code contains functions that are not supported for fixed-point conversion, the app displays these on the Function Replacements tab.

  2. In the Fixed-Point Conversion window, on the Variables tab, for input u_in, select Static Min and set it to -1. Then set Static Max to 1.

    To compute derived range information, at a minimum you must specify static minimum and maximum values or proposed data types for all input variables.

      Note:   If you manually enter static ranges, these manually-entered ranges take precedence over simulation ranges and the app uses them to propose data types. In addition, you can also modify and lock the proposed type.

  3. Click Compute Derived Ranges.

    Range analysis computes the derived ranges and displays them in the Variables tab. Using these derived ranges, the analysis proposes fixed-point types for each variable based on the default type proposal settings, and displays them in the Proposed Type column.

    In the dti function, the clip_status output has a minimum value of -2 and a maximum of 2.

    % Compute Output
    if (u_state > limit_upper)
        y = limit_upper;
        clip_status = -2;
    elseif (u_state >= limit_upper)
        y = limit_upper;
        clip_status = -1;
    elseif (u_state < limit_lower)
    	y = limit_lower;
        clip_status = 2;
    elseif (u_state <= limit_lower)
    	y = limit_lower;
        clip_status = 1;   
    else        
        y = u_state;
        clip_status = 0;
    end
    

    When you derive ranges, the Fixed-Point Converter app analyses the function and computes these minimum and maximum values for clip_status.

    The app provides a Quick derived range analysis option and the option to specify a timeout in case the analysis takes a very long time. For more information, see Computing Derived Ranges.

  4. To convert the floating-point algorithm to fixed point, click Convert.

    The software validates the proposed types, generates a fixed-point version of the entry-point function, dti_fixpt.m, in the codegen\dti\fixpt folder in your local working folder.

    If the errors or warnings occur during validation, they are displayed on the Type Validation Output tab. For more information, see Validating Types.

  5. To convert the floating-point algorithm to fixed point, click Convert.

    During the fixed-point conversion process, the software validates the proposed types and generates the following files in the codegen\dti\fixpt folder in your local working folder.

    • dti_fixpt.m — the fixed-point version of dti.m.

    • dti_wrapper_fixpt.m — this file converts the floating-point data values supplied by the test file to the fixed-point types determined for the inputs during conversion. These fixed-point values are fed into the converted fixed-point design, dti_fixpt.m.

    • dti_fixpt_report.html — this report shows the generated fixed-point code and the fixed-point instrumentation results.

    • dti_report.html — this report shows the original algorithm and the fixed-point instrumentation results.

    • dti_fixpt_exVal.mat — MAT-file containing a structure for the input arguments, a structure for the output arguments and the name of the fixed-point file.

    If the errors or warnings occur during validation, they are displayed on the Type Validation Output tab. For more information, see Validating Types.

  6. In the Output files list, select dti_fixpt.m. The app displays the generated fixed-point code.

  7. Use the Simulation Data Inspector to plot the floating-point and fixed-point results.

    1. Click

    2. Expand the Plotting and Reporting settings and set Plot with Simulation Data Inspector to Yes.

  8. Click the arrow to the right of Test, select Log inputs and outputs for comparison plots, and then click Test.

    The app runs the test file that you used to define input types to test the fixed-point MATLAB code. Optionally, you can add test files and select to run more than one test file to test numerics. The software runs both a floating-point and a fixed-point simulation and then calculates the errors for the output variable y. Because you selected to log inputs and outputs for comparison plots and to use the Simulation Data Inspector for these plots, the Simulation Data Inspector opens.

    On the Verification Output tab, the app provides a link to a type proposal report.

    Click the dti_fixpt_report.html link to open the report.

  9. You can use the Simulation Data Inspector to view floating-point and fixed-point run information and compare results. For example, to compare the floating-point and fixed-point values for the output y, on the Compare tab, select y, and then click Compare Runs.

    The Simulation Data Inspector displays a plot of the baseline floating-point run against the fixed-point run and the difference between them.

Integrate Fixed-Point Code

To integrate the fixed-point version of the code to perform system-level simulations, generate a MEX function to accelerate the fixed-point algorithm and call this MEX function instead of the original MATLAB algorithm.

  1. Copy dti_fixpt.m to your local working folder.

  2. Generate a MEX function for dti_fixpt.m. Use the type proposal report to get the fimath, fm, and fixed-point data type for input u_in.

    fm = fimath('RoundingMethod', 'Floor', 'OverflowAction', 'Wrap', 'ProductMode', 'FullPrecision', 'MaxProductWordLength', 128, 'SumMode', 'FullPrecision', 'MaxSumWordLength', 128);
    fiaccel dti_fixpt -args {fi( 0, 1, 16, 14, fm )}

    fiaccel generates a MEX function, dti_fixpt_mex, in the current folder.

  3. You can now call this MEX function in place of the original MATLAB algorithm.

Was this topic helpful?