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 spectra:
%  Vibration_ps/SpectrumData
%
% This function computes features:
%  Vibration_sigstats/Mean
%  Vibration_tsa_rotmac/RMS
%  Vibration_tsa_rotmac/CrestFactor
%  Vibration_ps_spec/PeakAmp1
%  Vibration_ps_spec/PeakFreq1
%  Vibration_ps_spec/BandPower
%
% 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

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

In addition to the filter lists, you can filter for text within variable names by typing the text into the filter. For example, if you type peak into the Output filter, as the following figures show, the filtered list now includes the spectral features for peak amplitude and peak frequency. Text matching is case insensitive.

Output filter set for all signal names that include the word peak

Filtered list of all features and spectra with names containing the word peak

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. This action produces a function with a preamble that includes the following information.

%
% This function computes signals:
%  Vibration_tsa/Data
%
% This function computes spectra:
%  Vibration_ps/SpectrumData
%
% This function computes features:
%  Vibration_sigstats/Skewness
%  Vibration_tsa_rotmac/RMS
%  Vibration_ps_spec/PeakAmp1
%
% This function ranks computed feautres using algorithms:
%  Bhattacharyya
%

Even though the Vibration_tsa/Data is not selected in the sorted filter view, the function computes this signal because other signals that are selected require it.

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 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 data set, 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 Options for Generated Code

When you develop your features, you have options for whether to use parallel computing and, during the import process, how to handle ensemble datastores. 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 chose to store computation results in local memory when you imported an ensemble datastore, you might want your code to append results directly to the external files instead when you run the code. You can set these options for code generation in the Code Generation tab.

List of all possible outputs with selections grouped at the top

In the Options section, the following options are available under the following conditions:

See Also

Related Topics