주요 콘텐츠

Root Mean Square (RMS Block) Code Replacement

This example shows how to replace the code generated from the RMS (DSP System Toolbox) block with a custom root mean square (RMS) function. Replacing the generated code with a custom function can optimize the generated code for your target execution environment.

Note

Code replacement supports replacement of the RMS (DSP System Toolbox) block from the DSP System Toolbox™. Other root mean square (RMS) blocks are not supported.

RMS Block Configurations

When you define a code replacement entry for the RMS block, you must specify a conceptual representation function prototype that corresponds to these characteristics of the RMS block:

  • The data type of the input, which can be single or double and real or complex.

  • The dimensions of the input, which can be scalar, vector, or a matrix. You can specify the dimensions as either:

    • A range of sizes by using a two-row matrix, for example, [1 1 1; inf inf inf].

    • A specific size by using a vector, for example, [20, 30, 40].

  • The dimension over which the block calculates the RMS value, which is specified by the block parameter Find the RMS value over.

Code replacement does not support RMS blocks if you select the Running RMS parameter.

This table shows the function prototype to use according to the dimension over which the block finds the RMS value.

Find the RMS value overConceptual Function SignatureArgumentsExample Function Prototypes
Entire inputy1 = rms(u1)
  • y1 — output, specified as a single or double

  • u1 — input, specified as a matrix of the data type and dimension of the block input signal

single y1 = rms(single u1[1 1 1; inf inf inf]) matches RMS blocks that calculate the value over the entire input of a three-dimensional matrix.
Each columny1 = rms(u1, u2)
  • y1 — output, specified as a matrix of the data type and dimension that matches the Output (DSP System Toolbox) port of the RMS block

  • u1 — input, specified as a matrix of the data type and dimension of the block input signal

  • u2 — dimension to calculate over, specified as single or double, matching the base type of the input u1

single y1[1 1 1; 1 inf inf] = rms(single u1[1 1 1; inf inf inf]) matches RMS blocks that calculate the value over each column of a three-dimensional matrix.
Each rowsingle y1[1 1 1; inf 1 inf] = rms(single u1[1 1 1; inf inf inf], single u2) matches RMS blocks that calculate the value over each row of a three-dimensional matrix.
Specified dimension
  • single y1[1 1 1; inf 1 inf] = rms(single u1[1 1 1; inf inf inf], single u2) matches RMS blocks that calculate the value over the second dimension (each row) of a three-dimensional matrix.

  • single y1[1 1; inf inf] = rms(single u1[1 1 1; inf inf inf], single u2) matches RMS blocks that calculate the value over the third dimension of a three-dimensional matrix. Because the calculation happens over the third dimension, which is the terminal dimension of the matrix, the output is reduced by one dimension to a two-dimensional matrix.

Replace Code From RMS Blocks

Create and register a code replacement library that replaces code generated from RMS blocks.

  1. Open the programmatic interface from the MATLAB menu by selecting New > Function.

  2. Create a table.

    1. Create a function with the name of your code replacement library table that does not have arguments and returns a table object. You can use this function to call your code replacement library table.

    2. Create a table object by calling RTW.TflTable.

    function hTable = crl_rms_blocks
    % Create a function to call the code replacement library table 
    
    %% Create a table object
    hTable = RTW.TflTable;
    
  3. Create an entry by calling the function createCRLEntry. Specify these arguments:

    • The code replacement table object.

    • A function prototype for the conceptual representation of the RMS blocks to replace.

    • A function prototype for the implementation function that replaces the RMS block code.

    For this example, replace the code for RMS blocks that perform the calculation over the entire input of a three-dimensional matrix with the custom function rms_all.

    function hTable = crl_rms_blocks
    % Create a code replacement library table 
    
    %% Create a table object
    hTable = RTW.TflTable;
    
    %% Create an entry
    hEntry = createCRLEntry(hTable,...
                          "single y1 = rms(single u1[1 1 1; inf inf inf])",...
                          "single y1 = rms_all(single* u1)");
  4. Create entry parameters. Because this example replaces a function, create entry parameters by calling the function setTflCFunctionEntryParameters. For this example, specify the implementation header file rms_all.h.

    function hTable = crl_rms_blocks
    % Create a code replacement library table 
    
    %% Create a table object
    hTable = RTW.TflTable;
    
    %% Create an entry
    hEntry = createCRLEntry(hTable,...
                          "single y1 = rms(single u1[1 1 1; inf inf inf])",...
                          "single y1 = rms_all(single* u1)");
    
    %% Create entry parameters
    hEntry.setTflCFunctionEntryParameters("ImplementationHeaderFile","rms_all.h");
  5. Add the entry to the table by calling addEntry.

    function hTable = crl_rms_blocks
    % Create a code replacement library table 
    
    %% Create a table object
    hTable = RTW.TflTable;
    
    %% Create an entry
    hEntry = createCRLEntry(hTable,...
                          "single y1 = rms(single u1[1 1 1; inf inf inf])",...
                          "single y1 = rms_all(single* u1)");
    
    %% Create entry parameters
    hEntry.setTflCFunctionEntryParameters("ImplementationHeaderFile","rms_all.h");
    
    %% Add entry to the table
    addEntry(hTable,hEntry);
  6. Validate and save the customization file. From the MATLAB menu, save this customization file by selecting File > Save. From the command line, validate the code replacement library table by calling it:

    hTable = crl_rms_blocks
  7. Register the code replacement library. Registration creates a code replacement library by defining the library name, code replacement tables, and other information. Create a registration file (a new function file) with these specifications:

    function rtwTargetInfo(cm)
     
    cm.registerTargetInfo(@loc_register_crl);
    end
     
    function this = loc_register_crl 
     
    this(1) = RTW.TflRegistry; 
    this(1).Name = 'CRLRMSBlocks';
    this(1).TableList = {'crl_rms_blocks.m'}; % table created in this example
    this(1).TargetHWDeviceType = {'*'};
    this(1).Description = 'Example code replacement library';
    
    end

    To use your code replacement library, refresh your current MATLAB session with the command:

    sl_refresh_customizations

  8. Verify the code replacement library. From the MATLAB command line, open the library by using the Code Replacement Viewer and verify that the table and entry are correctly specified. For more information, see Verify Code Replacement Library. Configure your model to use the code replacement library, generate code, and verify that code replacement occurs as expected. If unexpected behavior occurs, examine the hit and miss logs to troubleshoot the issues.

Example RMS Block Code Replacement

This example shows code replacement for a model that contains RMS blocks that use a variety of data types and dimensions.

Model containing eight RMS blocks. Each block uses an input data type that is a different variation of single or double, real or complex, and scalar or three dimensional matrix.

This code replacement library replaces the RMS code with custom RMS functions.

function hLib = crl_rms_blocks
    %   Copyright 2025 The MathWorks, Inc.

    hLib = RTW.TflTable;

    % 3 Dimensional Single Matrix, Calculate Over Entire Input 
    hEntry = hLib.createCRLEntry("single y1 = rms(single u1[1 1 1; inf inf inf])", ...
        "single y1 = rms_s_n_n_n_all(single* u1)");
    setTflCFunctionEntryParameters(hEntry,"ImplementationHeaderFile", "rms_s_n_n_n_all.h");
    addEntry(hLib,hEntry);

    % 3 Dimensional Single Matrix, Calculate Over Dimension 1 (Each Column)
    hEntry = hLib.createCRLEntry("single y1[1 1 1; 1 inf inf] = rms(single u1[1 1 1; inf inf inf], single u2)", ...
        "void rms_s_n_n_n_dim1(single* y1, single* u1, single u2)");
    setTflCFunctionEntryParameters(hEntry,"ImplementationHeaderFile", "rms_dim1.h");
    addEntry(hLib,hEntry);

    % 3 Dimensional Double Matrix, Calculate Over Dimension 2 (Each Row)
    hEntry = hLib.createCRLEntry("double y1[1 1 1; inf 1 inf] = rms(double u1[1 1 1; inf inf inf], double u2)", ...
        "void rms_d_n_n_n_dim2(double* y1, double* u1, double u2)");
    setTflCFunctionEntryParameters(hEntry,"ImplementationHeaderFile", "rms_d_n_n_n_dim2.h");
    addEntry(hLib,hEntry);

    % 3 Dimensional Complex Double Matrix, Calculate Over Dimension 3
    hEntry = hLib.createCRLEntry("double y1[1 1; inf inf] = rms(cdouble u1[1 1 1; inf inf inf], double u2)", ...
        "void rms_cd_n_n_n_dim4(double* y1, cdouble* u1, double u2)");
    setTflCFunctionEntryParameters(hEntry,"ImplementationHeaderFile", "rms_cd_n_n_n_dim3.h");
    addEntry(hLib,hEntry);

    % Real Single Scalar
    hEntry = hLib.createCRLEntry("single y1 = rms(single u1)",...
        "single y1 = rms_real_scalar(single u1)");
    setTflCFunctionEntryParameters(hEntry,"ImplementationHeaderFile","rms_real_scalar.h");
    addEntry(hLib,hEntry);

    % Complex Single Scalar
    hEntry = hLib.createCRLEntry("single y1 = rms(csingle u1)", ...
        "single y1 = rms_complex_scalar(csingle u1)");
    setTflCFunctionEntryParameters(hEntry,"ImplementationHeaderFile", "rms_complex_scalar.h");
    addEntry(hLib,hEntry);

end

This registration file registers the code replacement library.

function rtwTargetInfo(cm)
 
cm.registerTargetInfo(@loc_register_crl);
end
 
function this = loc_register_crl 
 
this(1) = RTW.TflRegistry; 
this(1).Name = 'CRLRMSBlocks';
this(1).TableList = {'crl_rms_blocks.m'};
this(1).TargetHWDeviceType = {'*'};
this(1).Description = 'Example code replacement library';

end

To generate code that uses the custom RMS functions:

  1. Register the code replacement library by entering sl_refresh_customizations at the command line.

    sl_refresh_customizations

  2. Configure the model to use the code replacement library. In the model configuration parameters dialog box, on the Interface pane, set Code Replacement Library by clicking Select and adding CRLRMSBlocks to the Selected code replacement libraries - prioritized list pane.

  3. Generate code from the model.

The generated code contains these calls to custom replacement functions.

void rms_basic_step(void)
{
  /* Outport: '<Root>/Out7' incorporates:
   *  Inport: '<Root>/In1'
   *  S-Function (sdspstatfcns): '<Root>/RMS6'
   */
  rms_s_n_n_n_dim1(&rms_basic_Y.Out7[0], &rms_basic_U.In1[0], 1.0F);

  /* Outport: '<Root>/Out17' incorporates:
   *  Inport: '<Root>/In7'
   *  S-Function (sdspstatfcns): '<Root>/RMS16'
   */
  rms_d_n_n_n_dim2(&rms_basic_Y.Out17[0], &rms_basic_U.In7[0], 2.0);

  /* Outport: '<Root>/Out8' incorporates:
   *  Inport: '<Root>/In3'
   *  S-Function (sdspstatfcns): '<Root>/RMS7'
   */
  rms_cd_n_n_n_dim4(&rms_basic_Y.Out8[0], &rms_basic_U.In3[0], 3.0);

  /* Outport: '<Root>/Out1' incorporates:
   *  Inport: '<Root>/In1'
   *  S-Function (sdspstatfcns): '<Root>/RMS'
   */
  rms_basic_Y.Out1 = rms_s_n_n_n_all(&rms_basic_U.In1[0]);

  /* Outport: '<Root>/Out16' incorporates:
   *  Inport: '<Root>/In6'
   *  S-Function (sdspstatfcns): '<Root>/RMS15'
   */
  rms_basic_Y.Out16 = rms_complex_scalar(rms_basic_U.In6);

  /* Outport: '<Root>/Out15' incorporates:
   *  Inport: '<Root>/In5'
   *  S-Function (sdspstatfcns): '<Root>/RMS14'
   */
  rms_basic_Y.Out15 = rms_real_scalar(rms_basic_U.In5);
}

See Also

(DSP System Toolbox) | |

Topics