## Documentation Center |

On this page… |
---|

Build the Fixed-Point Butterworth Filter Define the Model Callback Function |

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.

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

At the MATLAB

^{®}prompt, type`sfnew`to create a new model with an empty chart.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.Add the following data to your chart:

Data Name Scope Type `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)`Save your model.

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.

Open the Model Properties dialog box by selecting

**File**>**Model Properties**>**Model Properties**in the model window.In the

**Callbacks**tab, select**PreLoadFcn**.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.

Click

**OK**to close the dialog box.Save your model.

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

Open the Simulink Library Browser.

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

Parameter Setting **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.

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

Parameter Setting **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.

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

Connect and label the blocks as follows:

Close the Library Browser and save your model.

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

In the Stateflow Editor, select

**Simulation**>**Model Configuration Parameters**.In the

**Solver**pane, set the following parameters:Parameter Setting **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 in the

*Simulink Graphical User Interface*documentation.In the

**Diagnostics > Data Validity**pane, set the following parameters:Parameter Setting **Signals > Signal resolution**`Explicit and warn implicit`**Parameters > Detect precision loss**`none`By setting the diagnostic settings for data validity, you control what types of warnings or errors appear during simulation. For more information, see Diagnostics Pane: Data Validity in the

*Simulink Graphical User Interface*documentation.Click

**OK**to close the dialog box.Save and close your 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.

Was this topic helpful?