Main Content

Export Feature Extraction Function and Simulink Model for Streaming Data

In Diagnostic Feature Designer, you can generate a MATLAB® function that automates your interactive computations for the features you choose. The function that you generate can be formatted in two ways:

  1. Automate feature extraction from multimember data ensembles, as described in Automatic Feature Extraction Using Generated MATLAB Code.

  2. Automate feature extraction for streaming data.

    • Generate a MATLAB function that is MATLAB Coder™ compliant and use codegen (MATLAB Coder) to convert the function to C/C++.

    • Generate a Simulink® model that uses the MATLAB function.

    Spectral features that you create using autoregressive or state-space modeling are not supported for streaming data. To generate code for spectral features, you must use the Welch method to extract them. Features derived from ensemble statistics variables or frame-based processing are also not supported for code generation.

Generate MATLAB Function for Streaming

The following figure shows the ranking table for a set of features. These features include spectral features that were computed using an autoregressive model.

Variables are on the left. Feature ranking bar chart is in the middle. Feature values are on the right.

Click Export to access the export options and select Generate Function for Features.

The Export button is the rightmost button on the top. The Generate Function for Features option is the top item in the middle section of the menu.

The dialog box allows you to choose the number of top ranked features and whether you want to format the function for streaming data.

The Number of top features is the third item from the top and is set to 6. The Format for streaming data check box the fourth item and is selected.

When you click OK, the app generates the streaming-formatted function. The following code is the descriptive header at the top of the function.

%DIAGNOSTICFEATURES recreates results in Diagnostic Feature Designer.
% Input:
%  inputData: A timetable.
% Output:
%  featureTable: A table containing all features.
% This function computes signals:
%  Vibration_tsa/Data
% This function computes features:
%  Vibration_sigstats/Mean
%  Vibration_tsa_rotmac/RMS
%  Vibration_tsa_rotmac/CrestFactor
% Organization of the function:
% 1. Compute signals/spectra/features
% 2. Extract computed features into a table
% Modify the function to add or remove data processing, feature generation
% or ranking operations.

% Auto-generated by MATLAB 
This function accepts input data in the form of a timetable and returns a feature table. The function generates only three features from the app session. This is because the other three features were extracted from a power spectrum that was computed using an autoregressive model, which is not compatible with C/C++.

For comparison, the exported code description from the same session data, but without streaming format, is provided in Automatic Feature Extraction Using Generated MATLAB Code.

Generate Streaming Code

From this new MATLAB function, you can now generate C or C++ code using codegen (MATLAB Coder).

For example, if your function has the file name diagnosticFeatures.m and the representative function inputs are in the timetable tt, then generate C code using the following command.

codegen diagnosticFeatures.m -args {tt}

Export Simulink Block for Streaming Data

In addition to exporting a MATLAB function, you can also export a Simulink block that uses that function.

In the Export menu, select Generate Feature Extraction Simulink Block.

The Generate Feature Extraction Simulink Box option is the last menu item.

The resulting dialog box is similar to the Generate Function for Features dialog box but does not include a checkbox for streaming. This is because the Simulink Block code is always formatted for streaming.

The Generate Feature Extraction Simulink Block contains, from top to bottom, Feature Table, Features sorted by, and Number of top features. Help, OK, and Cancel bottoms are beneath these options.

When you click OK, the app generates a template Simulink model that includes blocks for the vibration and tacho signal inputs, the exported Feature Extraction block, and an output scope.

The input blocks are on the left. The Feature Extraction block is in the center. The output scope block is on the right.

Double-click the Feature Extraction block to open the block parameters and confirm or specify the feature extraction function as well as buffering, independent variable, and sample time and start time information.

Block parameters for feature extraction. Descriptions are in text in the upper half. Parameter settings are in the lower half.

Double click an input block to specify the input parameters. The block includes tabs for main parameters and signal attributes.

Block parameter specification for the Vibration_Data input block. The Main tab is open.

You can incorporate additional blocks as well such as a Classification Learner Predict block.

See Also

| (MATLAB Coder) | (MATLAB Coder) |

Related Topics