%% Classify Sounds Using YAMNet
% The YAMNet network requires you to preprocess and extract features from audio signals by converting them to the sample rate the network was trained on, and then extracting overlapping log-mel spectrograms.  This example walks through the required preprocessing and feature extraction necessary to match the preprocessing and feature extraction used to train YAMNet.  The classifySound function performs these steps for you.
%
% Read in an audio signal to classify it. Resample the audio signal to 16 kHz and then convert it to single precision.

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

fs = 16e3;
audioIn = resample(audioIn,fs,fs0);

audioIn = single(audioIn);

%
% Define mel spectrogram parameters and then extract features using the melSpectrogram function.

FFTLength = 512;
numBands = 64;
frequencyRange = [125 7500];
windowLength = 0.025*fs;
overlapLength = 0.015*fs;

melSpect = melSpectrogram(audioIn,fs, ...
    'Window',hann(windowLength,'periodic'), ...
    'OverlapLength',overlapLength, ...
    'FFTLength',FFTLength, ...
    'FrequencyRange',frequencyRange, ...
    'NumBands',numBands, ...
    'FilterBankNormalization','none', ...
    'WindowNormalization',false, ...
    'SpectrumType','magnitude', ...
    'FilterBankDesignDomain','warped');

%
% Convert the mel spectrogram to the log scale.

melSpect = log(melSpect + single(0.001));

%
% Reorient the mel spectrogram so that time is along the first dimension as rows.

melSpect = melSpect.';
[numSTFTWindows,numBands] = size(melSpect)

%
% Partition the spectrogram into frames of length 96 with an overlap of 48. Place the frames along the fourth dimension.

frameWindowLength = 96;
frameOverlapLength = 48;

hopLength = frameWindowLength - frameOverlapLength;
numHops = floor((numSTFTWindows - frameWindowLength)/hopLength) + 1;

frames = zeros(frameWindowLength,numBands,1,numHops,'like',melSpect);
for hop = 1:numHops
    range = 1 + hopLength*(hop-1):hopLength*(hop - 1) + frameWindowLength;
    frames(:,:,1,hop) = melSpect(range,:);
end

%
% Create a YAMNet network.

net = yamnet();

%
% Classify the spectrogram images.

classes = classify(net,frames);

%
% Classify the audio signal as the most frequently occurring sound.

  mySound = mode(classes)

% Copyright 2020 The MathWorks, Inc.