メインコンテンツ

Integrate External C++ Code Using Class with Custom Constructor in Simulink

This example shows how to integrate external C++ code that uses a class with a custom constructor into Simulink®. The classes and custom constructor define an algorithm for use in various engineering and science applications. You can integrate such external C++ code into Simulink® by using a C Function block.

In this example, the CFcnUniquePtrExample model performs signal processing operation on a digital signal. To perform signal processing, the Signal Processing unit of the model uses two blocks:

  • A Rate Transition block performs upsampling. Upsampling increases the sampling rate of the input signal.

  • A C Function block performs signal smoothing using a linear filter defined in the external C++ code. This code uses std::inner_product function to implement a digital signal processing algorithm.

Open the model.

mdl = "CFcnUniquePtrExample.slx";
open(mdl);

The CFcnUniquePtrExample model.

Signal Processing Unit

The Signal Processing unit receives a discrete sine wave. The Rate Transition block and C Function blocks together process the input signal and output a less noisy, smoother signal.

Rate Transition Block

To upsample the input signal, set the Output port sample time parameter value of the Rate Transition block to 0.02 which increases the sampling rate of the input signal by five times. This upsampling operation helps the C Function block perform the signal smoothing.

Rate transition block.

C Function Block

The C++ code in the C Function block uses a header file named LinearFilter.h and a source file named LinearFilter.cpp. The header and source file together implement the linear filter, which performs signal smoothing.

The header file defines a class named LinearFilter that uses:

  • Standard template library container (STL) std::deque to create a data buffer and stores that in a variable named inBuffer.

  • Standard template library container (STL) std::vector to create a variable that represents filter coefficients.

  • A constructor (LinearFilter(const std::vector<double>& coefficients)) of the LinearFilter class.

  • A function named process to perform signal smoothing operation.

Header file LinearFilter.h.

In the source file, the process function uses the template function std::inner_product for convolution between the input sine wave data and the filter coefficients to smooth the signal. This function uses a First-In-First-Out strategy where the pop_front() method removes the signal data from the initial position and the push_back() method adds data at the end of the data buffer named inBuffer.

Source file LinearFilter.c.

To understand how the C++ code is integrated into the C Function block, check these settings in the Simulation tab of the C Function block:

  • The header file LinearFilter.h and a source file LinearFilter.cpp are specified locally in the C Function block in the Headers and Sources fields, respectively.

  • The Start code calls the class constructor for LinearFilter and creates a smart pointer named std::unique_ptr . This pointer is stored in a variable named filter.

  • To instantiate filter, in the Ports and Parameters table, specify its Type as Class:std::unique_ptr<LinearFilter> and Scope as Persistent because it retains its value from one time step to another during simulation.

The Start code and the Ports and Parameters table.

  • The Output code uses the pointer variable filter to call the process function, which is a member of the LinearFilter class. The input signal is passed to the function using the variable in. The function output variable out stores the processed signal data.

C Function block Output code.

Model Simulation

Simulate the model for t = 10s and visualize the results using a Scope block. The yellow signal indicates the input signal, and the blue signal indicates the output signal. The output signal is smoother and less noisy as compared to the input signal.

out = sim(mdl);

See Also

|

Topics