%% Extract Features from Audio Data Set
% Create an <docid:audio_ref#mw_6315b106-9a7b-4a11-a7c6-322c073e343a
% |audioDatastore|> object that points to audio samples included with Audio
% Toolbox(TM).

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);
%% 
% Read one file from the data set.

[audioIn,adsInfo] = read(ads);
fs = adsInfo.SampleRate;
%% 
% Extract audio features from |audioIn| using the *Extract Audio Features*
% live task.
% 
% <<../EAF2.png>>
% 
% The *Extract Audio Features* live task returns an array containing the
% extracted features. The dimensions of the array are
% |numHops| by |numFeatures| by |numChannels|, where |numHops| is the
% number of windows analyzed, |numFeatures| is the number of features you
% extracted, and |numChannels| is the number of channels in the original
% audio.

[numHops,numFeatures,numChannels] = size(features)
%% 
% You can use the output column mapping to determine which columns of features 
% correspond to which features you requested.

plot(features(:,40,:))
title('Spectral Centroid')
xlabel('Hop')
ylabel('Frequency (Hz)')
%% 
% The *Extract Audio Features* task also returns a configured
% <docid:audio_ref#mw_b56cd7dc-af31-4da4-a43e-b13debc30322
% |audioFeatureExtractor|> object. The object is configured by the
% parameters you set in the task.

extractor
%% 
% You can use the configured |audioFeatureExtractor| object to extract
% features from the entire data set. For example, while the
% |audioDatastore| object has unread data, read a file from the datastore
% and then extract the features. First, reset the |audioDatastore| object
% so that you read from the beginning.

reset(ads)
while hasdata(ads)
    audioIn = read(ads);
    features = extract(extractor,audioIn);
end
%% 
% _Copyright 2019 The MathWorks, Inc._
