Main Content

extract

Extract time-domain, frequency-domain, or time-frequency-domain features

Since R2021b

    Description

    features = extract(sFE,src) returns a matrix, table, or cell array containing features extracted from input data source src. Specify src as a vector, matrix, MATLAB® timetable, or datastore object. The function output depends on the settings of the feature extractor object sFE.

    example

    [features,infoFeatures] = extract(sFE,src) also returns a structure infoFeatures that maps a specific feature to its column location in the output feature matrix features. This syntax is valid only when you set the FeatureFormat property of the feature extractor object to "matrix".

    example

    [features,infoFeatures,frameLimits] = extract(sFE,src) returns a matrix frameLimits whose ith row contains the beginning and end limits of the ith frame. This syntax is valid only when you set the FeatureFormat property of the feature extractor object to "matrix".

    [___] = extract(sFE,src,UseParallel=usePar) enables you to accelerate code by automatically running computations in parallel. (since R2024b)

    You must have a Parallel Computing Toolbox™ license, specify a datastore object in src, and specify UseParallel=true in the syntax to use this functionality.

    Examples

    collapse all

    Load a set of temperature readings in degrees Celsius taken every hour at Boston Logan International Airport for 31 days. Plot the data.

    load bostemp
    t = (1:31*24)/24;
    plot(t,tempC)
    axis tight
    ylabel("Temperature (\circC)")
    xlabel("Time elapsed from Jan 1, 2011 (days)")
    title({"Dry Bulb Temperature" ...
            "at Boston Logan International Airport (source: NOAA)"})

    Figure contains an axes object. The axes object with title Dry Bulb Temperature at Boston Logan International Airport (source: NOAA), xlabel Time elapsed from Jan 1, 2011 (days), ylabel Temperature ( degree C) contains an object of type line.

    Create a signalTimeFeatureExtractor object and enable the PeakValue feature. To obtain the maximum absolute temperature reading per day, set the frame size to 24 samples and the frame overlap to 0 samples.

    fl = 24;
    sFE = signalTimeFeatureExtractor(FrameSize=fl, ...
        FrameOverlapLength=0,PeakValue=true);

    Call the extract function on the object to extract the absolute value of the daily maximum temperatures in the data set. Plot the extracted peak values along with the hourly dry-bulb temperature readings.

    peaktemps = extract(sFE,tempC);
    hold on
    stairs(0:numel(peaktemps)-1,peaktemps*[-1 1],":k")
    hold off

    Figure contains an axes object. The axes object with title Dry Bulb Temperature at Boston Logan International Airport (source: NOAA), xlabel Time elapsed from Jan 1, 2011 (days), ylabel Temperature ( degree C) contains 3 objects of type line, stair.

    Confirm the extracted peak values. Divide the signal into 24-sample segments representing temperature readings per day and compute the maximum absolute value of each segment. Compare the resulting vector to peaktemps.

    y = framesig(tempC,fl);
    [mx,idx] = max(abs(y));
    
    tf = isequal(peaktemps,mx')
    tf = logical
       1
    
    

    Load a set of temperature readings in degrees Celsius taken every hour at Boston Logan International Airport for 31 days. Plot the data.

    load bostemp
    t = (1:31*24)/24;
    plot(t,tempC)
    axis tight
    ylabel("Temperature (\circC)")
    xlabel("Time elapsed from Jan 1, 2011 (days)")
    title({"Dry Bulb Temperature" ...
            "at Boston Logan International Airport (source: NOAA)"})

    Figure contains an axes object. The axes object with title Dry Bulb Temperature at Boston Logan International Airport (source: NOAA), xlabel Time elapsed from Jan 1, 2011 (days), ylabel Temperature ( degree C) contains an object of type line.

    Convert the array of temperature readings to a timetable. The first temperature reading was taken on January 1, 2011.

    tt = array2timetable(tempC, ...
        TimeStep=hours(1),StartTime=datetime(2011,01,01));

    Create a signalTimeFeatureExtractor object and enable the Mean feature to obtain the weekly average temperatures. Specify the sample rate and frame size (1 week = 168 hours) of the extractor. Set the output format of the extracted values to a table.

    fs = 1/3600;
    sFE = signalTimeFeatureExtractor(SampleRate=fs, ...
        FrameSize=168,Mean=true,FeatureFormat="table");

    Call the extract function to extract the weekly average temperatures from the data set. The function does not include the readings taken in the last three days (72 hours) since they do not span a full week.

    meantemps = extract(sFE,tt)
    meantemps=4×3 table
        FrameStartTime    FrameEndTime     Mean  
        ______________    ____________    _______
    
               1              168         -1.8357
             169              336         -4.3095
             337              504          1.7976
             505              672          2.0911
    
    

    Consider a quadratic chirp sampled at 1 kHz for 2 seconds. The chirp has an initial frequency of 100 Hz that increases to 200 Hz at t = 1 second. Compute and display the spectrogram.

    fs = 1e3;
    t = 0:1/fs:2;
    y = chirp(t,100,1,200,"quadratic");
    pspectrum(y,fs,"spectrogram")

    Figure contains an axes object. The axes object with title Fres = 10.2261 Hz, Tres = 251 ms, xlabel Time (s), ylabel Frequency (Hz) contains an object of type image.

    Create a signalFrequencyFeatureExtractor object to obtain the mean and median frequencies from the signal. Specify the sample rate.

    sFE = signalFrequencyFeatureExtractor(SampleRate=fs, ...
        MeanFrequency=true,MedianFrequency=true);

    Extract the features. info returns the column index in features of each extracted feature.

    [features1,info1] = extract(sFE,y)
    features1 = 1×2
    
      226.0160  199.7034
    
    
    info1 = struct with fields:
          MeanFrequency: 1
        MedianFrequency: 2
    
    

    Set the FrameSize and FrameRate properties of the feature extractor object to divide the signal into two frames. The first frame represents the chirp oscillating at the initial frequency of 100 Hz and the second frame represents the chirp oscillating at 200 Hz. Extract the mean and median frequencies for each frame and include the frame limits in the output.

    sFE.FrameSize = round(length(y)/2);
    sFE.FrameRate = 1000;
    [features2,info2,frameLimits] = extract(sFE,y)
    features2 = 2×2
    
      131.4921  124.9820
      331.2664  324.6992
    
    
    info2 = struct with fields:
          MeanFrequency: 1
        MedianFrequency: 2
    
    
    frameLimits = 2×2
    
               1        1001
            1001        2001
    
    

    Since R2025a

    Extract time-domain, frequency-domain, and time-frequency features from healthy bearing vibration signals and faulty bearing vibration signals. While a healthy bearing vibration signal does not have outstanding defects, a faulty bearing vibration signal results from wear-and-tear defects, such as spalls on the gear teeth, eccentricity or gear misalignment, and cracks at the races.

    For more information on bearing signal generation and analysis, see Vibration Analysis of Rotating Machinery. To learn more about the feature extraction and model training workflow to identify faulty bearing signals in mechanical systems, see Machine Learning and Deep Learning Classification Using Signal Feature Extraction Objects.

    Generate Healthy Bearing Signal

    Generate a healthy bearing vibration signal as a sum of three cosine pulses with amplitudes of 0.4 V, 0.2 V, and 1 V, respectively, and frequencies of 22.5 Hz, 8.36 Hz, and 292.5 Hz, respectively, for three seconds and with a sample rate of 20 kHz. Generate Gaussian noise and add it to the signal.

    rng("default")
    Fs = 20e3;
    t = (0:1/Fs:3-1/Fs)';
    
    a = [0.4 0.2 1];
    f = [22.5 8.36 292.5];
    sClean = cos(2*pi*f.*t)*a';
    sHealthy = sClean + 0.2*randn(size(t));

    Generate Faulty Bearing Signal

    Generate a faulty bearing vibration signal by adding a bearing impact signal to the healthy bearing signal. Model each impact as a 3 kHz sinusoid windowed by a Kaiser window. The defect causes a series of 10-millisecond impacts on the bearing.

    tImpact = t(t<10e-3)';
    xImpact = sin(2*pi*3000*tImpact).*kaiser(length(tImpact),40)';
    
    xImpactBper = 0.33*pulstran(t,0:1/104.5:t(end),xImpact,Fs);

    Generate a faulty bearing vibration signal using the healthy bearing signal and the bearing impact signal.

    sFaulty = sHealthy + xImpactBper;

    Consolidate and Visualize Signals

    Bundle the healthy bearing and faulty bearing signals in a signalDatastore object in single precision.

    sds = signalDatastore({sHealthy,sFaulty},OutputDataType="single");

    Plot the power spectrum of the healthy and faulty vibration signals. Observe the peaks that correspond to the bearing impact.

    [P,F] = pspectrum([sHealthy sFaulty],Fs);
    p = plot(F/1000,pow2db(P));
    p(1).Marker = ".";
    xlabel("Frequency (kHz)")
    ylabel("Power Spectrum (dB)")
    legend(["Healthy" "Faulty"])

    Figure contains an axes object. The axes object with xlabel Frequency (kHz), ylabel Power Spectrum (dB) contains 2 objects of type line. These objects represent Healthy, Faulty.

    Set Up Feature Extraction Pipeline

    Create a signalTimeFeatureExtractor object for time-domain feature extraction.

    timeFE = signalTimeFeatureExtractor(SampleRate=Fs,...
        RMS=true,ImpulseFactor=true,StandardDeviation=true);

    Create a signalFrequencyFeatureExtractor object for frequency-domain feature extraction.

    freqFE = signalFrequencyFeatureExtractor(SampleRate=Fs, ...
        MedianFrequency=true,BandPower=true,PeakAmplitude=true);

    Create a signalTimeFrequencyFeatureExtractor object to extract time-frequency features from a spectrogram. Set the leakage parameter for the spectrogram to 90%.

    timeFreqFE = signalTimeFrequencyFeatureExtractor(SampleRate=Fs, ...
        SpectralKurtosis=true,SpectralSkewness=true,TFRidges=true);
    
    setExtractorParameters(timeFreqFE,"spectrogram",Leakage=0.9);

    Extract Multidomain Features

    Extract signal features using all three feature extractors for the signals in the signalDatastore object sds. Concatenate the multidomain features. Display the first four columns in a feature table.

    featureCell = cellfun(@(a,b,c) [real(a) real(b) real(c)], ...
        extract(timeFE,sds),extract(freqFE,sds),extract(timeFreqFE,sds), ...
        UniformOutput=false);
    
    featureMatrix = cell2mat(featureCell);
    featureTable = array2table(featureMatrix);
    head(featureTable(:,1:4))
        featureMatrix1    featureMatrix2    featureMatrix3    featureMatrix4
        ______________    ______________    ______________    ______________
    
           0.80115           0.80116            3.2635            292.39    
           0.80538           0.80539            3.1501            292.41    
    

    Input Arguments

    collapse all

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

    Input data source, specified as one of these:

    • Vector — extract assumes that src is a single-channel signal.

    • Matrix — extract assumes that src is an N-channel signal, where N is the number of columns in src.

    • Timetable — extract assumes that src.Variables is an N-channel signal, where N is the number of columns in src.Variables.

      If src is a timetable, you must specify the SampleRate property of the feature extractor object sFE. The sample rate in timetable src must equal the sample rate specified in sFE.

    • signalDatastore object or audioDatastore (Audio Toolbox) object — extract computes the features for each member of the datastore object and returns them as a cell array of feature matrices or feature tables depending on the output feature format. (since R2024b)

      Tip

      As a best practice, make sure that the datastore object src points to data of the same format at every read.

    Data Types: single | double

    Since R2024b

    Option to extract features in parallel, specified as either false or true.

    • By default, extract does not extract features in parallel.

    • If you set usePar to true, then extract extracts features using a parallel pool of workers only if:

      • You have installed Parallel Computing Toolbox.

      • You have enabled automatic pool creation in parallel settings or an open parallel pool exists. For more information on how to control your parallel settings, see Specify Your Parallel Settings (Parallel Computing Toolbox).

      • You have specified a datastore object in src.

      Otherwise, extract does not extract features and errors out.

    Data Types: logical

    Output Arguments

    collapse all

    Extracted features, returned as a 3-D array, table, or cell array. The format of the features features output depends on the value of the FeatureFormat property in the feature extractor object sFE and on whether you are extracting features from an input data source src.

    • If you set the FeatureFormat property of the input feature extractor object to "matrix", and you specify an input signal src, then extract returns features as an L-by-M-by-N array:

      • L is the number of frames.

      • M is the number of features extracted per frame.

      • N is the number of channels.

    • If you set the FeatureFormat property of the input feature extractor object to "table", and you specify an input signal src, then extract returns the extracted features in a table with the frame limits listed in the first two table variables.

    • If you specify an input datastore object src with k members, the function returns a k-by-1 cell array. The kth element of the array contains the features extracted from the kth signal in src in the specified format FeatureFormat. (since R2024b)

      • If you set FeatureFormat to "matrix", extract returns each element of the cell array as a matrix.

      • If you set FeatureFormat to "table", extract returns each element of the cell array as a table.

    Feature information, returned as a structure. The function maps each feature to its column location in the output matrix features. This argument applies only when you set the FeatureFormat property of the input feature extractor object to "matrix".

    If you specify an input datastore object src with k members, the function returns a k-by1 cell array where each element is a feature information structure corresponding to a signal from src. (since R2024b)

    Frame limits, returned as a matrix. The ith row in frameLimits contains the beginning and end limits of the ith frame. This argument applies only when you set the FeatureFormat property of the input feature extractor object to "matrix".

    If you specify an input datastore object src with k members, the function returns a k-by1 cell array where each element is a matrix containing the frame limits corresponding to a signal from src. (since R2024b)

    Extended Capabilities

    expand all

    Version History

    Introduced in R2021b

    expand all