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.

  3. Add the following data to your chart:

    Data NameScopeType
    xInputInherit:Same as Simulink
    yOutputfixdt(1,16,10)
    x_n1Localfixdt(1,16,12)
    y_n1Localfixdt(1,16,10)
    b0Parameterfixdt(1,16,15)
    b1Parameterfixdt(1,16,15)
    a1Parameterfixdt(1,16,15)

  4. Save your model.

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.

  5. Save your model.

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 typeTime based
    TimeUse simulation time
    Amplitude1
    Bias0
    Frequency2*pi*Fc
    Phase0
    Sample time1/Fs
    Interpret vector parameters as 1-DOn

    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 typefixdt(1,16,14)
    Lock output data type setting against changes by the fixed-point toolsOff
    Input and output to have equalReal World Value (RWV)
    Integer rounding modeFloor
    Saturate on integer overflowOff
    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 time0.1
    TypeFixed-step
    Solverdiscrete (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.

  3. In the Diagnostics > Data Validity pane, set the following parameters:

    ParameterSetting
    Signals > Signal resolutionExplicit and warn implicit
    Parameters > Detect precision lossnone

    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.

  4. Click OK to close the dialog box.

  5. 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.

Was this topic helpful?