%% Apply Principal Component Analysis to VGGish Embeddings
% Read in an audio file, listen to it, and then extract VGGish features from the audio.

[audioIn,fs] = audioread('Counting-16-44p1-mono-15secs.wav');

sound(audioIn,fs)

features = vggishFeatures(audioIn,fs);

%
% Visualize the VGGish features over time. Many of the individual features are zero-valued and contain no useful information.

surf(features,'EdgeColor','none')
view([90,-90])
axis tight
xlabel('Feature Index')
ylabel('Frame Index')
title('VGGish Features')

%
% You can apply principal component analysis (PCA) to map the feature vectors into a space that emphasizes variation between the embeddings. Call the vggishFeatures function again and specify ApplyPCA as true. Visualize the VGGish features after PCA.

features = vggishFeatures(audioIn,fs,'ApplyPCA',true);

surf(features,'EdgeColor','none')
view([90,-90])
axis tight
xlabel('Feature Index')
ylabel('Frame Index')
title('VGGish Features + PCA')

%
% Copyright 2020 The MathWorks, Inc.