# Documentation

### This is machine translation

Translated by
Mouseover text to see original. Click the button below to return to the English verison of the page.

## Use Fixed-Point Parameters and Local Data

### Goal of the Tutorial

In the sections that follow, you build a model that uses fixed-point parameters and local data in a Stateflow® chart. In this model, the chart acts as a low-pass Butterworth filter:

Building this model requires a Signal Processing Toolbox™ license.

### Build the Fixed-Point Butterworth Filter

In this section, you create a stateless flow chart that accepts one input and provides one output.

1. At the MATLAB® prompt, type `sfnew` to create a new model with an empty chart.

2. In your chart, add a flow chart with a single branch:

The values `b0`, `b1`, and `a1` are the coefficients of the low-pass Butterworth filter. For more information about the filter coefficients, see Define the Model Callback Function.

Data NameScopeType
`x``Input``Inherit:Same as Simulink`
`y``Output``fixdt(1,16,10)`
`x_n1``Local``fixdt(1,16,12)`
`y_n1``Local``fixdt(1,16,10)`
`b0``Parameter``fixdt(1,16,15)`
`b1``Parameter``fixdt(1,16,15)`
`a1``Parameter``fixdt(1,16,15)`

### Define the Model Callback Function

In this section, you define a preload callback for the model. This callback function computes the values for `b0`, `b1`, and `a1` in the chart.

1. Open the Model Properties dialog box by selecting File > Model Properties > Model Properties in the model window.

2. In the Callbacks tab, select PreLoadFcn.

3. Enter the following MATLAB code for the preload function:

```Fs = 1000; Fc = 50; [B,A] = butter(1,2*pi*Fc/(Fs/2)); b0 = B(1); b1 = B(2); a1 = A(2);```

In the code:

• The sampling frequency `Fs` is 1000 Hz.

• The cutoff frequency `Fc` is 50 Hz.

• The `butter` function constructs a first-order low-pass Butterworth filter with a normalized cutoff frequency of `(2*pi*Fc/(Fs/2))` radians per second. The function output `B` contains the numerator coefficients of the filter in descending powers of z. The function output `A` contains the denominator coefficients of the filter in descending powers of z.

4. Click OK to close the dialog box.

### Add Other Blocks to the Model

In this section, you add the remaining blocks to the model.

1. Open the Simulink Library Browser.

2. From the Simulink/Sources library, add a Sine Wave block with the following parameter settings to the model:

ParameterSetting
Sine type`Time based`
Time`Use simulation time`
Amplitude`1`
Bias`0`
Frequency`2*pi*Fc`
Phase`0`
Sample time`1/Fs`
Interpret vector parameters as 1-D`On`

The Sine Wave block provides the signal that you want to filter using the Stateflow chart. This block outputs a floating-point signal.

3. From the Simulink/Signal Attributes library, add a Data Type Conversion block with the following parameter settings to the model:

ParameterSetting
Output minimum`[]`
Output maximum`[]`
Output data type`fixdt(1,16,14)`
Lock output data type setting against changes by the fixed-point tools`Off`
Input and output to have equal`Real World Value (RWV)`
Integer rounding mode`Floor`
Saturate on integer overflow`Off`
Sample time`-1`

The Data Type Conversion block converts the floating-point signal from the Sine Wave block to a fixed-point signal. By converting the signal to a fixed-point type, the model can simulate using less memory.

4. From the Simulink/Sinks library, add a Scope block to the model.

5. Connect and label the blocks as follows:

6. Close the Library Browser and save your model.

### Set Model Configuration Parameters

In this section, you specify solver and diagnostic options for simulation.

1. In the Stateflow Editor, select Simulation > Model Configuration Parameters.

2. In the Solver pane, set the following parameters:

ParameterSetting
Stop time`0.1`
Type`Fixed-step`
Solver`discrete (no continuous states)`
Fixed-step size (fundamental sample time)`1/Fs`

Because none of the blocks in your model have a continuous sample time, a discrete solver is appropriate. For more information, see Solver Pane (Simulink).

3. Click OK to close the dialog box.

4. Save and close your model.

### Run the Model

When you reopen and simulate the model, you see these results in the scope:

The top signal shows the fixed-point version of the sine wave input to the chart. The bottom signal corresponds to the filtered output from the chart. The filter removes high-frequency values from the signal but allows low-frequency values to pass through the chart unchanged.