Documentation

ConvertMATLABFunction Block to Fixed Point

This example shows how to use the Fixed-Point Tool to convert a model containing a MATLAB®Function block to fixed point.

Best Practices for Working with theMATLABFunction Block in the Fixed-Point Tool

  • Do not edit the fixed-point variant of your MATLAB Function block algorithm. Use the code view to edit the floating-point variant of your MATLAB code and repropose and apply data types.

  • For a successful conversion, only use modeling constructs supported by the Fixed-Point Converter app. For a list of the supported modeling constructs, seeMATLAB Language Features Supported for Automated Fixed-Point Conversion.

  • While collecting range information, do not edit the MATLAB code in the MATLAB Function block. Editing the code will cause problems if you try to merge results.

  • During the fixed-point conversion process using the Fixed-Point Tool, do not use the “Save as” option to save the MATLAB Function block with a different name. If you do, you might lose existing results for the original block.

Open the Model

Change directories to the folder where the model is located. At the MATLAB command line, enter:

cd(fullfile(docroot,'toolbox','fixpoint','examples'))

Copy theex_symmetric_fir.slxfile to a local writable folder and open the model.

Theex_symmetric_firmodel uses a symmetric FIR filter. Simulate the model and inspect the model output. Inspect the symmetric FIR filter algorithm by double-clicking the MATLAB Function block.

  1. To open the Fixed-Point Tool, from the model menu, selectAnalysis>Data Type Design>Fixed-Point Tool.

  2. In thePrepare System for Conversion部分toolstrip, underSystem under design, select thesymmetric_firsubsystem, which contains the MATLAB Function block, as the system to convert.

Collect Range Information

Collect idealized ranges to use for data type proposal.

  1. In theCollect Ranges部分toolstrip, selectSimulation Rangesas the method of range collection. This button configures the model to collect ranges using idealized floating-point data types.

  2. Click theCollect Rangesbutton to start the simulation

    The Fixed-Point Tool stores the simulation data in a run titledRanges (Double). Examine the range information of the MATLAB variables in the spreadsheet.

Propose Data Types

Configure the proposal settings and propose fixed-point data types for the model.

  1. In theConvert Data Types部分toolstrip you can configure the data type proposal settings for the MATLAB Function block variables.

    In this example, use the default proposal settings.

  2. ClickPropose Data Types.

    The data type proposals appear in theProposedDTcolumn of the spreadsheet.

    Note

    TheSpecifiedDTcolumn is always blank for MATLAB Function block variables.

Inspect Code Using the Code View

To launch the code view, click theMATLAB Functionsbutton.

Using the code view you can:

  • View detailed variable and expression information.

  • Adjust proposal settings, such asfimathsettings.

  • Edit proposed data types.

  • Manage function replacements.

    For examples showing how to replace MATLAB functions with a lookup table, seeReplace Functions in a MATLAB Function Block with a Lookup Table.

  • Edit your code.

  • Propose fixed-point data types.

  • Apply proposed data types to your code.

To view the current proposal settings, clickSettings. Here you can edit thefimathproperties for the function. For this example, the defaultfimathproperties are sufficient.

Apply Proposed Data Types

When you have finished examining the proposed types, editing proposal settings, and implementing any function replacements, apply the proposed data types to the model. You can apply the data types either from the code view, or from the Fixed-Point Tool.

In the code view window, clickApply. The left pane displays both the original floating-point MATLAB Function block, as well as a newly generated fixed-point variant MATLAB Function block.

Right-click on the MATLAB Function block node in the left pane. SelectGo to Blockto navigate to the MATLAB Function block in the model.

A variant subsystem is now in the place of the MATLAB Function block. The variant subsystem contains both floating-point and fixed-point versions of the MATLAB Function block. The active version is automatically controlled by the Fixed-Point Tool based on the data type override settings of the model. Data Type Override is not currently active on the model, so the fixed-point version is active.

Verify Results

Return to the Fixed-Point Tool to verify the results of the conversion.

In theVerify部分toolstrip, click theSimulate with Embedded Typesbutton to simulate the model using the newly applied fixed-point data types. The model simulates with the fixed-point variant as the active variant.

Related Topics

Was this topic helpful?