%% Extract Features from Dataset
% Create an audio datastore that points to audio samples included with Audio Toolbox®.

folder = fullfile(matlabroot,'toolbox','audio','samples');
ads = audioDatastore(folder);

%
% Find all files that correspond to a sample rate of 44.1 kHz and then subset the datastore.

keepFile = cellfun(@(x)contains(x,'44p1'),ads.Files);
ads = subset(ads,keepFile);

% Convert the data to a tall array. tall arrays are evaluated only when you request them explicitly using gather. MATLAB® automatically optimizes the queued calculations by minimizing the number of passes through the data. If you have Parallel Computing Toolbox™, you can spread the calculations across multiple machines. The audio data is represented as an M-by-1 tall cell array, where M is the number of files in the audio datastore.

adsTall = tall(ads)

%
% Create an audioFeatureExtractor object to extract the mel spectrum, Bark spectrum, ERB spectrum, and linear spectrum from each audio file. Use the default analysis window and overlap length for the spectrum extraction.

aFE = audioFeatureExtractor('SampleRate',44.1e3, ...
    'melSpectrum',true, ...
    'barkSpectrum',true, ...
    'erbSpectrum',true, ...
    'linearSpectrum',true);

%
% Define a cellfun function so that audio features are extracted from each cell of the tall array. Call gather to evaluate the tall array.

specsTall = cellfun(@(x)extract(aFE,x),adsTall,"UniformOutput",false);
specs = gather(specsTall);

%
% The specs variable returned from gather is an numFiles-by-1 cell array, where numFiles is the number of files in the datastore. Each element of the cell array is a numHops-by-numFeatures-by-numChannels array, where the number of hops and number of channels depends on the length and number of channels of the audio file, and the number of features is the requested number of features from the audio data.

numFiles = numel(specs)
[numHops1,numFeaturesFile1,numChanelsFile1] = size(specs{1})
[numHops2,numFeaturesFile2,numChanelsFile2] = size(specs{2})

% Copyright 2019 The MathWorks, Inc.