Main Content

generateMATLABFunction

Create MATLAB function compatible with C/C++ code generation

Since R2021b

    Description

    example

    generateMATLABFunction(sFE) generates code based on the input feature extractor object sFE and opens an untitled file containing the function extractSignalFeatures. The function signature depends on how you set the FeatureFormat property of the input feature extractor object.

    • When you specify FeatureFormat as "matrix", the generated MATLAB® function has this signature:

      [features,info,framelimits] = extractSignalFeatures(x)
      The signature is equivalent to:
      [features,info,framelimits] = extract(sFE,x)

    • When you specify FeatureFormat as "table", the generated MATLAB function has this signature:

      features = extractSignalFeatures(x)
      The signature is equivalent to:
      features = extract(sFE,x)

    Examples

    collapse all

    Create a signalTimeFeatureExtractor object to extract the mean, standard deviation, and peak value of a random signal.

    x = randn(1000,1);
    sFE = signalTimeFeatureExtractor(FrameSize=100, ...
          FrameOverlapLength=10,Mean=true,StandardDeviation=true, ...
          PeakValue=true)
    sFE =
       signalTimeFeatureExtractor with properties:
    
        Properties
                   FrameSize: 100
          FrameOverlapLength: 10
                  SampleRate: []
         IncompleteFrameRule: "drop"
               FeatureFormat: "matrix"
    
        Enabled Features
          Mean, StandardDeviation, PeakValue
    
        Disabled Features
          RMS, ShapeFactor, SNR, THD, SINAD, CrestFactor
          ClearanceFactor, ImpulseFactor

    Call generateMATLABFunction on the object. The generated function extractSignalFeatures is equivalent to calling the extract function on sFE. Save the function to your current folder and view the function script.

    generateMATLABFunction(sFE)
    type extractSignalFeatures
    function [features,info,frameLimits] = extractSignalFeatures(x)
    % EXTRACTSIGNALFEATURES Extract signal features
    % [FEATURES,INFO,FRAMELIMITS] = extractSignalFeatures(X) returns a matrix
    % containing features extracted from input X, INFO, a structure that maps
    % a specific feature to its column location in the output feature matrix
    % and FRAMELIMITS, whose i-th row contains the beginning and end limits
    % of the i-th frame.
    %
    % Parameters of the signalTimeFeatureExtractor used to generate this
    % function must be honored when calling this function.
    
    % Generated by MATLAB(R) 9.11 and Signal Processing Toolbox 8.7.
    % Generated on: 10-Jun-2021 08:25:02.
    
    %#codegen
    
    if istimetable(x)
         xInTT = x{:,:};
    else
         xInTT = x;
    end
    if isrow(xInTT)
         xIn = xInTT(:);
    else
         xIn = xInTT;
    end
    dataType = class(xIn);
    signalLength = size(xIn,1);
    numChannels = size(xIn,2);
    frameSize = 100;
    frameOverlapLength = 10;
    frameRate = frameSize - frameOverlapLength;
    featureMatrix = zeros(0,1,dataType);
    numFeatureCols = 0;
    numFeatureRows = 0;
    frameLimits = zeros(0,2,dataType);
    info = struct('Mean',0,'StandardDeviation',0,'PeakValue',0);
    for idx = 1:numChannels
        if numChannels == 1
             xChannel = xIn;
        else
             xChannel = xIn(:,idx);
        end
        startIdx = 1;
        endIdx = frameSize;
        while startIdx <= signalLength
            if endIdx > signalLength
                break;
            end
            featureIndex = 1;
            xFrame = xChannel(startIdx:endIdx,1);
            meanValue = mean(xFrame);
            numCurrentFeature = numel(meanValue);
            info.Mean = featureIndex;
            featureIndex = featureIndex+numCurrentFeature;
    
            standardDeviation = std(xFrame);
            numCurrentFeature = numel(standardDeviation);
            info.StandardDeviation = featureIndex;
            featureIndex = featureIndex+numCurrentFeature;
    
            peakValue = max(abs(xFrame));
            info.PeakValue = featureIndex;
    
            featureVector = [meanValue(:);standardDeviation(:);peakValue(:)];
            featureMatrix = [featureMatrix;featureVector];
            if startIdx == 1
                numFeatureCols = size(featureVector,1);
            end
            if idx == 1
                numFeatureRows = numFeatureRows+1;
                frameLimits = [frameLimits;[startIdx endIdx]];
            end
            startIdx = startIdx+frameRate;
            endIdx = startIdx+frameSize-1;
        end
    end
    tempFeatureMatrix = reshape(featureMatrix,numFeatureCols,numFeatureRows,numChannels);
    features = permute(tempFeatureMatrix,[2,1,3]);
    end

    You can replace calls to extract with calls to the generated function in your code. The outputs are identical.

    features1 = extract(sFE,x)
    features1 = 11x3
          0.0842    1.0690    2.7526
          0.0500    1.0516    2.9491
          0.1901    1.0356    2.7304
          0.1209    0.9171    2.4366
          0.0443    0.9399    2.4247
         -0.1153    1.0490    3.5699
         -0.1001    0.9530    2.4124
          0.0616    0.9959    2.7485
         -0.0263    0.9482    2.4868
         -0.0234    0.9876    3.1585
            .
            .
            .
    
    features2 = extractSignalFeatures(x)
    features2 = 11x3
          0.0842    1.0690    2.7526
          0.0500    1.0516    2.9491
          0.1901    1.0356    2.7304
          0.1209    0.9171    2.4366
          0.0443    0.9399    2.4247
         -0.1153    1.0490    3.5699
         -0.1001    0.9530    2.4124
          0.0616    0.9959    2.7485
         -0.0263    0.9482    2.4868
         -0.0234    0.9876    3.1585
            .
            .
            .
    

    Copyright 2020 The MathWorks, Inc.

    Input Arguments

    collapse all

    Feature extractor object, specified as a signalFrequencyFeatureExtractor object, a signalTimeFeatureExtractor object, or a signalTimeFrequencyFeatureExtractor object.

    Version History

    Introduced in R2021b