%% 1) Clean Conditions
%File Input and preprocessing

clear all
close all

audioPath = strcat(pwd, '/audio/');           % The directory of the WAV files
audioFiles = dir(strcat(audioPath, '*.wav'));  % Get all WAV files in the directory
fs = 8000;                                  % Desired sampling frequency

NumWords = 10;                              % There are 10 unique words
NumRecordings = 5;                          % There are 5 different recordings of each word

ConditionLabels = {'Clean', '30dB Car', '20dB Car', '10dB Car', '5dB Car', '0dB Car', '-5dB Car', ...
                    '30dB Babble', '20dB Babble', '10dB Babble', '5dB Babble', '0dB Babble', '-5dB Babble'};

% Condition 1
FeatureMatrix = cell(NumWords, NumRecordings);          % A cell matrix...
                                                        %   vertical    =   what word it is
                                                        %   horizontal  =   the # of recording for that word
                                                        %   Each cell contains a (# of frames BY 17 band energes) matrix

audioFileIndex = 1;                                     % # of audio file in directory
curWord = 1;                                            % Index of the current word
curRecording = 1;                                       % Index of the current recording of that word
                                                        % (m,n) pairs

while curWord < NumWords                                                % Iterating over the words zero to nine...
    
    while curRecording < NumRecordings                                  % Iterating over each recording of the words...
        curWord = str2double(audioFiles(audioFileIndex).name(1)) + 1;   % Index representing what word this file is for...
                                                                        % i.e. 1 = zero .... 10 = nine
                                                                     
        curRecording = str2double(audioFiles(audioFileIndex).name(2));  % Index representing the recording #...
                                                                        % i.e. 1 = zero1 .... 5 = zero5
        
        [wordAudio, fs0] = audioread([audioPath, audioFiles(audioFileIndex).name]); % Read the wav file
        wordAudio = resample(wordAudio, fs, fs0);                                   % Resample to 8K fs
   
        [energyVec, frameCount] = ExtractFeatures(wordAudio, fs, 0.030, 0.50);      % Extract the critical band
                                                                                    %   energies for the word
        
        FeatureMatrix{curWord,curRecording} = energyVec;                           % Store the energyVec in the P1 matrix
        
        audioFileIndex = audioFileIndex + 1;                            % Move to the next audio file
    end
    
    curRecording = 1;                                                   % Reset the current recording #1 after
                                                                        %   all five versions of the word are processed
end

confMatrix = zeros(NumWords * NumRecordings);       % Zero out the Confusion Matrix
    
bar = 0;
h = waitbar(bar, 'Please wait...');

test_index = 0;    
for curTestWord = 1:NumWords                                    % For each word... zero to nine
                                                                % Use this as the test word

    for curTestTrial = 1:NumRecordings                          % For each word recording number
                                                                % Use this as the test word recording # 

        TestWordFeatureMatrix = FeatureMatrix{curTestWord, curTestTrial};       % Save the outputs to the Ptest cell matrix
        test_index = test_index + 1;

        ref_index = 0;

        for curReferenceWord = 1:NumWords                       % Reference Word

            for curReferenceRecording = 1:NumRecordings         % Reference Word's recording number

                ref_index = ref_index + 1;

                ReferenceWordFeatureMatrix = FeatureMatrix{curReferenceWord, curReferenceRecording};

                E = EuclideanDistanceMatrix(TestWordFeatureMatrix, ReferenceWordFeatureMatrix);

                ADM = AccumulatedDistanceMatrix(E);

                Row = size(ADM,1);
                Col = size(ADM,2);
                confMatrix(test_index,ref_index) = ADM(Row,Col);

            end

        end

        bar = bar+1;
        waitbar(bar/60,h);
    end
end

close(h);                                       % Close the wait bar

fig = figure(2);                                % Open a figure to plot the Confusion Matrix
set(fig, 'Position', [100 100 1000 800]);       % Set the figure position & size
colormap('jet');                                % Set the colormap for the Confusion Matrix
imagesc(confMatrix)                             % Visualize the Confusion Matrix
title('Distance Matrix for All Words Under Clean Conditions')
xlabel('Test Word')
ylabel('Reference Word')