## Documentation Center |

This example shows how to write a fixed-point version of your entry-point function using the data types proposed in Propose Fixed-Point Data Types Using an Instrumented Mex Function.

This capability is not compatible with automatic fixed-point
conversion. If you select `Convert to fixed point at build
time`, you cannot build instrumented MEX functions.

You will learn how to:

Use the proposed data types to create a fixed-point version of your entry-point function.

Update your test file to call the fixed-point entry-point function.

Verify that the fixed-point function is functionally equivalent to the original MATLAB

^{®}algorithm.

On this page… |
---|

Create a New Folder and Copy Relevant Files |

**Prerequisites**

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

MATLAB

MATLAB Coder™

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**

Create a local working folder, for example,

`c:\coder\fun_with_matlab`.Change to the

`docroot\toolbox\coder\examples`folder. At the MATLAB command line, enter:cd(fullfile(docroot, 'toolbox', 'coder', 'examples'))

Copy the following files to your local working folder.

Type Name Description Function code `fun_with_matlab.m`Entry-point MATLAB function Test file `fun_with_matlab_test.m`MATLAB script that tests `fun_with_matlab.m`Function code `fun_with_fi.m`Entry-point MATLAB function — fixed-point version of `fun_with_matlab`that uses data types proposed in Propose Fixed-Point Data Types Using an Instrumented Mex FunctionTest file `fun_with_fi_test.m`MATLAB script that runs both `fun_with_matlab`and`fun_with_fi`and compares the results

**The fun_with_fi Function**

The `fun_with_fi` is a fixed-point version
of the `fun_with_matlab` function that uses the data
types proposed in Propose Fixed-Point Data Types Using an Instrumented Mex Function.

Variable | Proposed Signedness | Proposed Word Length | Proposed Fraction Length |
---|---|---|---|

y | Signed | 16 | 14 |

x | Signed | 16 | 14 |

z | Signed | 16 | 15 |

a | Unsigned | 16 | 18 |

b | Signed | 16 | 14 |

i | Unsigned | 16 | 0 |

For example, in `fun_with_matlab`, variable `y` is
defined as `y = zeros(size(x));`. In `fun_with_fi`,
to specify that it is a signed fixed-point data type with a word length
of 16 and a fraction length of 14:

y = fi(zeros(size(x)),1,16,14,'OverflowAction','Wrap','RoundingMethod','Floor'); |

For more information, see `fi`.

**Create and set up a MATLAB Coder Project**

Navigate to the work folder that contains the file for this tutorial.

On the MATLAB

**Apps**tab, select**MATLAB Coder**and then, in the**MATLAB Coder Project**dialog box, set**Name**to`fun_with_fi_project.prj`.Alternatively, at the MATLAB command line, enter

coder -new fun_with_fi_project.prj

By default, the project opens in the MATLAB workspace.

On the project

**Overview**tab, click the**Add files**link. Browse to the file`fun_with_fi.m`, and then click**OK**to add the file to the project.

**Define Input Types**

On the project

**Overview**tab, click the**Autodefine types**link.In the Autodefine Input Types dialog box, add

`fun_with_fi_test`as a test file, and then click**Run**.The test file runs and plots the outputs of the filter. MATLAB Coder determines the input types from the test file and then displays them.

In the Autodefine Input Types dialog box, click

**Use These Types**to accept the autodefined input type.MATLAB Coder sets the type of

`x`to`double(1x256)`.

**The fun_with_fi_test Script**

The `fun_with_fi_test` script runs the original
floating-point MATLAB algorithm, `fun_with_matlab`,
then runs the fixed-point version of the algorithm, `fun_with_fi`.
The script then plots the outputs for the floating-point and fixed-point
algorithms and the difference in results.

**Run Simulation**

In the project, click the

**Build**tab.On the

**Verification**pane, verify that the test file is set to`fun_with_fi_test`. Clear**Redirect entry-point calls to MEX function**so that the test file calls the MATLAB versions of the original and fixed-point algorithms.On

**Verification**pane, click**Run**.The

`fun_with_fi_test`file runs. The test file runs the original MATLAB algorithm and the fixed-point version, and plots the difference in their outputs.Optionally, zoom in on each plot in turn to view the error (difference between the two versions of the algorithm). In this example, the errors are very small, on the order of

`10`. If the error is unacceptably large, refine the fixed-point data types.^{-3}

Was this topic helpful?