Main Content

Automatic Feature Extraction Using Generated MATLAB Code

In Diagnostic Feature Designer, you explore features interactively, using tools for signal processing, feature generation, and ranking. Once you determine which features perform best, you can generate code that reproduces your interactive computations and allows you to automate feature extraction on similar input data. Select among your features, computed variables, and ranking tables to specify what the code includes.

With the generated code, you can:

  • Apply the code directly to a larger set of measurement data that includes more members and, therefore, increase the number of members in your feature set. Using more members improves model training in Classification Learner.

  • Modify the function to suit your application. For example, you might add signal processing or features that are not available in Diagnostic Feature Designer.

  • Incorporate portions of the function into another set of code that you are developing.

Generate a Function for Features

The simplest way to generate code for automatic feature extraction is to use the Export button in the Feature Designer tab and select Generate Function for Features.

Export button in Diagnostic Feature Designer showing menu for feature and data export and for code generation

Your selection opens a set of options that allow you to specify the features to include from the feature table that you select. Code generation is possible for only one feature table at a time.

Options on Diagnostic Feature Designer for specifying features to include in code generation

You can generate code for all your features or, if you have performed ranking, you can choose the number of top-ranked features to include. If you have performed ranking, you can also generate the function using the Export button in the Feature Ranking tab. The generated code includes the calculations for any computed signals or spectra that the feature requires. The code includes a preamble that summarizes the computations that the code performs.

%DIAGNOSTICFEATURES recreates results in Diagnostic Feature Designer.
%
% Input:
%  inputData: A table or a cell array of tables/matrices containing the
%  data as those imported into the app.
%
% Output:
%  featureTable: A table containing all features and condition variables.
%  outputTable: A table containing the computation results.
%
% This function computes signals:
%  Vibration_tsa/Data
%
% This function computes features:
%  Vibration_stats/Data_ImpulseFactor
%  Vibration_stats/Data_Mean
%  Vibration_stats/Data_ShapeFactor
%  Vibration_stats/Data_Std
%  Vibration_tsa_rotmac/Data_RMS
%  Vibration_tsa_rotmac/Data_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.

Generate a Function for Specific Variables, Features, and Ranking Tables

If you want to customize your selections for code generation, use the Export > Generate Function for... option. With this option you can:

  • Select code generation for any outputs that the app computes, including computed signals and spectra, ensemble statistics, and ranking tables.

  • Filter your outputs so that you can choose among features with specific characteristics, such as input signal or variable-name text.

You can generate a function for features from one feature table at a time. If you are using frame-based processing, each feature table is associated with one frame policy, or combination of frame size and frame rate. Therefore, if you want to generate code for features computed with two different frame policies, or with both full-signal mode and a frame-based mode, you must generate a separate function for each feature table.

When you generate code for frame-based signals that depend on derived full signals, computations for the full signals appear also in the generated code.

Function generation option with selections for full signal mode and for a frame-based mode

When you select Export > Generate Function for... and choose a feature source, a window containing candidate selection outputs opens. Each output row includes additional information on how the output was computed.

Window in Diagnostic Feature Designer for selecting code generation outputs

In the figure, the skewness and RMS features and the power spectrum are selected. The Details pane displays the output and input for the most recently selected item. The Code Will Be Generated For pane contains your selections.

Along with the selection window, selecting Export > Generate Function for... opens the Code Generation tab, which contains filtering options.

Code Generation tab in Diagnostic Feature Designer for selecting filtering options

For example, to select only features that are computed directly from the TSA signal, select Input > Vibration_tsa/Data, as the figure shows.

Selection for code generation input from TSA signal data only

The filtered selection window displays the three features that are based directly on the TSA signal, along with the power spectrum, which is also based directly on the TSA signal.

Filtered selection window of features and spectrum

When you specify a specific input signal, the Input filter returns only the items which use that signal directly. For example, the list filtered on the TSA input signal contains no features that are derived from the TSA signal spectrum. If you want to filter for text that occurs anywhere in the computation chain, type the text into the filter. For example, if you type tsa into the Input filter, as the following figures show, the filtered list now includes the TSA spectral features. Text matching is case insensitive.

Input filter set for all signals containing the tsa string

Filtered list of all features and spectra that contain the tsa string

You can also filter on output, method, and analysis type. For example, if you want to generate code for a ranking table, select Analysis Type > ranking.

Code Generation tab in Diagnostic Feature Designer with Analysis Type filter set to the ranking option

The selection list includes the T-Test and Bhattacharyya ranking tables.

Filtered list of ranking tables for code generation

To clear all the filters, click Reset Filters.

Reset Filters button

Each filtered view displays a subset of available outputs. To display all your selections together, click Sort by Selection.

Sort by Selection button

The items that you selected in the filtered views appear in one group at the top of the selection list.

List of all possible outputs with selections grouped at the top

To generate code for these items, click Generate Function.

Save and Use Generated Code

The app assigns the default name of diagnosticFeatures to the function. You can save the function as a file with this name, or rename the function and file name. To run the function, follow the syntax in the function line of the code. For example, suppose the function line is:

function [featureTable,outputTable] = diagnosticFeatures(inputData)
The two outputs of this function are a feature table that contains the features and condition variables, and an output table that contains all of the variables in the ensemble. The second output argument is optional. You can use this function on any input data that has the same input variables as the data that you originally imported into the app. For instance, suppose that your data is in fullDataTable and you need only a feature table features. Use:
features = diagnosticFeatures(fullDataTable)

If you originally imported individual tables using Import Single-Member Datasets and want to validate the code against the original data, you must combine the tables. For example, if you imported tables t1, t2, and t3, where each table represents a different ensemble member, first combine the tables into a single input table, and then run the generated function.

inputData = {t1,t2,t3};
features = diagnosticFeatures(inputData);

For an example of generating code and validating the code with the original data, see Generate a MATLAB Function in Diagnostic Feature Designer. For an example that applies code to a new dataset, see Apply Generated MATLAB Function to Expanded Data Set. For a description of generated code itself and how it performs its computations, see Anatomy of App-Generated MATLAB Code.

Change Computation Options for Generated Code

The code that you generate depends in part on your selections in Computation Options. The figure below shows the Computation Options choices when you import an ensemble datastore.

Computation options in Diagnostic Feature Designer with a green box around the options that you can change just before generating code.

In some cases, you may want the generated code to use a different option than the option you used to compute your features in the app. For instance, you might want to invoke parallel computing in the code when you did not use that option originally. If you originally imported data from an ensemble datastore into local memory, you might want your code to interact directly with the external files instead.

The green box in the Computation Options figure indicates the options that you can change immediately before generating code.

  • Store all results in a local dataset or Write results to the same folder as the original data — You can switch between options for code generation. These options appear only when you import an ensemble datastore.

  • Use parallel computing — You can change this option for code generation. You can use parallel computing only if you have Parallel Computing Toolbox™ installed.

You cannot change the data handling mode and frame policy with which specific features were calculated when you generate code.

See Also

Related Topics