function process_vowels(remote_path)
%function process_vowels(remote_path)
%
% PROCESS_VOWELS  --  This function will gather the data produced by "collect_vowels"
%                     and generate confusion matrices comparing the performance of the 
%                     newly collected mean feature vectors with the Lab #8 and #9 
%                     mean feature vectors.  This version uses the newly collected
%                     vowels as the testing set.
%

fs = 8192;

if nargin < 1
    remote_path = 'k:\perm\eecs206\lab10\';
end

if isempty(dir(remote_path))
    error(['Invalid path name: "' remote_path '".']);
end

vowels = {'oo','oh','ah','ae','ee'};

directory = dir([remote_path '*.mat']);
    
allpole = cell(5,1);
formant = cell(5,1);
fbank = cell(5,1);

for j=1:length(directory)
    data = load([remote_path directory(j).name]);
    for i=1:5
        allpole{i} = [allpole{i}; data.allpole{i}];
        formant{i} = [formant{i}; data.formant{i}];
        fbank{i} = [fbank{i}; data.fbank{i}];
    end
end

M_allpole = [];
M_formant = [];
M_fbank = [];
for i=1:5
    M_allpole(i,:) = mean(allpole{i});
    M_formant(i,:) = mean(formant{i});
    M_fbank(i,:) = mean(fbank{i});
end

data = load('lab10_data');
for i=1:5
    M_my_allpole(i,:) = mean(data.allpole{i});
    M_my_formant(i,:) = mean(data.formant{i});
    M_my_fbank(i,:) = mean(data.fbank{i});
end

figure(1);

K_formant_old = confusion_matrix_demo(M_my_formant,formant{:});
K_formant_new = confusion_matrix_demo(M_formant,formant{:});
disp('Performance on NEW data using FORMANT features:');
disp('   Mean feature vectors from Lab #8 and #9        Newly recorded mean feature vectors');
disp(' ');
disp([display_confusion_matrix(K_formant_old) display_confusion_matrix(K_formant_new)]);
disp(' ');
disp(' ');
disp(' ');

subplot(321);
imagesc(K_formant_old); colorbar;
set(gca,'XTickLabel',vowels); set(gca,'YTickLabel',vowels);
ylabel('Formant');
title('Old class reps');
subplot(322);
imagesc(K_formant_new); colorbar;
set(gca,'XTickLabel',vowels); set(gca,'YTickLabel',vowels);
title('New class reps');


K_fbank_old = confusion_matrix_demo(M_my_fbank,fbank{:});
K_fbank_new = confusion_matrix_demo(M_fbank,fbank{:});
disp('Performance on NEW data using FILTER-BANK features:');
disp('   Mean feature vectors from Lab #8 and #9        Newly recorded mean feature vectors');
disp(' ');
disp([display_confusion_matrix(K_fbank_old) display_confusion_matrix(K_fbank_new)]);
disp(' ');
disp(' ');
disp(' ');

subplot(323);
imagesc(K_fbank_old); colorbar
ylabel('Filter-bank');
set(gca,'XTickLabel',vowels); set(gca,'YTickLabel',vowels);
subplot(324);
imagesc(K_fbank_new); colorbar
set(gca,'XTickLabel',vowels); set(gca,'YTickLabel',vowels);


K_allpole_old = confusion_matrix_demo(M_my_allpole,allpole{:});
K_allpole_new = confusion_matrix_demo(M_allpole,allpole{:});
disp('Performance on NEW data using ALL-POLE features:');
disp('   Mean feature vectors from Lab #8 and #9        Newly recorded mean feature vectors');
disp(' ');
disp([display_confusion_matrix(K_allpole_old) display_confusion_matrix(K_allpole_new)]);
disp(' ');
disp(' ');
disp(' ');

subplot(325);
imagesc(K_fbank_old); colorbar
set(gca,'XTickLabel',vowels); set(gca,'YTickLabel',vowels);
ylabel('All-pole');
subplot(326);
imagesc(K_fbank_new); colorbar
set(gca,'XTickLabel',vowels); set(gca,'YTickLabel',vowels);



function out = display_confusion_matrix(K)

vowels = {'oo','oh','ah','ae','ee'};
out(1,:) = [' ' sprintf('%8s',vowels{:}),'       ']; i = i+1;
for i=2:6
    out(i,:) = [vowels{i-1}, ':  ' num2str(K(i-1,:),'  %6.4f'),'     '];
end





    
    