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.
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.
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.
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.
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.
For example, to select only features that are computed directly from the TSA signal, select Input > Vibration_tsa/Data, as the figure shows.
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.
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.
The selection list includes the T-Test and Bhattacharyya ranking tables.
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.
To clear all the filters, click Reset Filters.
Each filtered view displays a subset of available outputs. To display all your selections together, click Sort by Selection.
The items that you selected in the filtered views appear in one group at the top of the selection list.
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)
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.
In the Options section, the following options are available under the following conditions:
Append Data to File Ensemble — Available if your data source is a
fileEnsembleDatastore
or asimulationEnsembleDatastore
Use Parallel Computing — Available if you have a license for Parallel Computing Toolbox™
See Also
Related Topics
- Generate a MATLAB Function in Diagnostic Feature Designer
- Apply Generated MATLAB Function to Expanded Data Set
- Export Feature Extraction Function and Simulink Model for Streaming Data
- Anatomy of App-Generated MATLAB Code
- Data Ensembles for Condition Monitoring and Predictive Maintenance
- Import Data into Diagnostic Feature Designer