This example shows how to build a Stateflow® chart that uses fixed-point data to implement a low-pass Butterworth filter. By designing the filter with fixed-point data instead of floating-point data, you can simulate your model using less memory. For more information, see Fixed-Point Data in Stateflow Charts.
The Low-Pass Filter chart is a stateless flow chart that accepts one input and provides one output. The chart contains these data symbols:
x — Scope:
Inherit:Same as Simulink
y — Scope:
x_n1 — Scope:
y_n1 — Scope:
b0 — Scope:
b1 — Scope:
a1 — Scope:
The values of
a1 are the coefficients of the low-pass Butterworth filter.
To build the Low-Pass Filter chart:
Create a Simulink® model with an empty Stateflow chart by entering
sfnew at the MATLAB® command prompt.
In the Stateflow chart, add a flow chart with a single branch that assigns values to
Add input, output, local, and parameter data to the chart, as described in Add Stateflow Data.
Before loading the model, MATLAB calls the
butter (Signal Processing Toolbox) function to compute the values for the parameters
a1. The function constructs a first-order low-pass Butterworth filter with a normalized cutoff frequency of
(2*pi*Fc/(Fs/2)) radians per second, where:
The sampling frequency is
Fs = 1000 Hz.
The cutoff frequency is
Fc = 50 Hz.
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
Fs = 1000; Fc = 50; [B,A] = butter(1,2*pi*Fc/(Fs/2)); b0 = B(1); b1 = B(2); a1 = A(2);
To define the preload callback for the model:
In the Modeling tab, under Setup, select Model Settings > Model Properties.
In the Model Properties dialog box, on the Callbacks tab, select PreLoadFcn.
Enter the MATLAB code for the preload function call.
To load the parameter values to the MATLAB workspace, save, close, and reopen the model.
Sine Wave block
The Sine Wave block outputs a floating-point signal. The block has these settings:
Use simulation time
Interpret vector parameters as 1-D:
Data Type Conversion block
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, you can simulate your model using less memory. The block has these settings:
Output data type:
Lock output data type setting against changes by the fixed-point tools:
Input and output to have equal:
Real World Value (RWV)
Integer rounding mode:
Saturate on integer overflow:
The Scope block has two input ports that connect to the input and output signals for the Low-Pass Filter chart. To display the two signals separately, select a scope layout with two rows and one column.
Because none of the blocks in the model have a continuous sample time, use a discrete solver with these configuration parameters:
discrete (no continuous states)
Fixed-step size (fundamental sample time):
To configure the model:
In the Modeling tab, under Setup, select Model Settings.
In the Solver pane, set the discrete solver parameters.
When you simulate the model, the Scope block displays two signals. 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.