clear
addpath ./mat

tic;
% Initialize Some initial Constants

%CONS = {'C','D','G','J','S','T','Z','b','d','f','g','h','k','L','m','n','p',...
%    'r','s','t','v','w','y','z'};
CONS = {'p','b','t','d','k','g','f','v','T','D','s','z','S','Z','h','C','J',...
    'm','n','G','w','y','r','L'};
conOrd = [17,8,20,9,13,11,10,21,6,2,19,24,5,7,12,1,4,15,16,3,22,23,18,14];
nCONS = length(CONS); %We use 24 constants
%VOWS = {'@','A','E','I','O','R','U','W','Y','a','c','e','i','o','u'};
VOWS = {'i','e','u','o','c','Y','W','O','R','@','A','a','I','E','U'};
vowOrd = [13,12,15,14,11,9,8,5,6,1,2,10,4,3,7];
nVOWS = length(VOWS); %We use 15 vowels

frictivesAffricates = { 'C', 'D', 'h', 'J', 'S', 'T', 'Z', 'f', 's', 'v', 'z'};
iFrictivesAffricates = [ismember(CONS,frictivesAffricates) zeros(1,nVOWS)];
stops = { 'b', 'd', 'g', 'k', 'p', 't' };
iStops = [ismember(CONS,stops)  zeros(1,nVOWS)];
nasalsLiquidsGlides = {'G', 'L', 'm', 'n', 'r', 'w', 'y'};
iNasalsLiquidsGlides = [ismember(CONS,nasalsLiquidsGlides)  zeros(1,nVOWS)];
vowels = VOWS;
iVowels = [zeros(1,nCONS)  ones(1,nVOWS)];
groups = {'frictivesAffricates','stops','nasalsLiquidsGlides','vowels'};

RD = {'Alina','Angela','Edward','Latisha','Laura','Norene',...
    'Shauna','Teddy','Tony'};
RC = {'Anton','Bob','Carly','Evan','Joanna','Miguel'};
SUBJECTS = {RD{:}, RC{:}};
nSUBJECTS = length(SUBJECTS);

% Load in Data for Subjects to Generate Stacked Bar Graphs
for i = 1 : nSUBJECTS
    subject = SUBJECTS(i);
    eval(['load ' subject{:} '.mat'])
%%    
    cm = CMs{1}; % CMs 1-2 Cons, 3-4 Vows (odd Initial, even Final),
    % Cons Initial
    concm(1:24,:) = cm(conOrd,:);
    N = sum(concm,2);  % Sum of the rows total of each occurance
    N = repmat(N,[1 length(CONS)]);
    pm = 100*(concm./N);
    concm(:,1:24) = concm(:,conOrd);
    pm(:,1:24) = pm(:,conOrd);
    figure;bar(concm,'stacked');
    set(gca,'XTick',1:length(CONS),'XTickLabel',CONS,'FontSize',16)
    legend(CONS, 'Location', 'NorthEastOutside','FontSize',11)
    title(['Initial Consonants for ' SUBJECTS{i} ' (Count)'])
    xlabel('Presented Consonants');ylabel('Count of Responses')
    fname = sprintf('stackConInitCount%s.eps',SUBJECTS{i});
    print(gcf, fullfile('./stackedBarFigsReOrder/', fname), '-depsc2');
    
    figure;bar(pm,'stacked'); ylim([0 100]);
    set(gca,'XTick',1:length(CONS),'XTickLabel',CONS,'FontSize',16)
    title(['Initial Consonants for ' SUBJECTS{i} ' (Percentages)'])
    legend(CONS, 'Location', 'NorthEastOutside','FontSize',11)
    xlabel('Presented Consonants');ylabel('Percentages of Responses')
    fname = sprintf('stackConInitPer%s.eps',SUBJECTS{i});
    print(gcf, fullfile('./stackedBarFigsReOrder/', fname), '-depsc2');
    
%%    
    cm = CMs{2}; % CMs 1-2 Cons, 3-4 Vows (odd Initial, even Final),
    % Cons Final
    concm(1:24,:) = cm(conOrd,:);
    N = sum(concm,2);  % Sum of the rows total of each occurance
    N = repmat(N,[1 length(CONS)]);
    pm = 100*(concm./N);
    concm(:,1:24) = concm(:,conOrd);
    pm(:,1:24) = pm(:,conOrd);
    figure;bar(concm,'stacked')
    set(gca,'XTick',1:length(CONS),'XTickLabel',CONS,'FontSize',16)
    legend(CONS, 'Location', 'NorthEastOutside','FontSize',11)
    title(['Final Consonants for ' SUBJECTS{i} ' (Count)'])
    xlabel('Presented Consonants');ylabel('Count of Responses')
    fname = sprintf('stackConFinCount%s.eps',SUBJECTS{i});
    print(gcf, fullfile('./stackedBarFigsReOrder/', fname), '-depsc2');
    
    figure;bar(pm,'stacked'); ylim([0 100]);
    set(gca,'XTick',1:length(CONS),'XTickLabel',CONS,'FontSize',16)
    title(['Final Consonants for ' SUBJECTS{i} ' (Percentages)'])
    legend(CONS, 'Location', 'NorthEastOutside','FontSize',11)
    xlabel('Presented Consonants');ylabel('Percentages of Responses')
    fname = sprintf('stackConFinPer%s.eps',SUBJECTS{i});
    print(gcf, fullfile('./stackedBarFigsReOrder/', fname), '-depsc2');
%%
    cm = CMs{3}; % CMs 1-2 Cons, 3-4 Vows (odd Initial, even Final),
    % Vows Initial
    vowcm(1:15,:) = cm(vowOrd,:);
    N = sum(vowcm,2);  % Sum of the rows total of each occurance
    N = repmat(N,[1 length(VOWS)]);
    pm = 100*(vowcm./N);
    vowcm(:,1:15) = vowcm(:,vowOrd);
    pm(:,1:15) = pm(:,vowOrd);
    figure;bar(vowcm,'stacked')
    set(gca,'XTick',1:length(VOWS),'XTickLabel',VOWS,'FontSize',16)
    legend(VOWS, 'Location', 'NorthEastOutside','FontSize',11)
    title(['Initial Vowels for ' SUBJECTS{i} ' (Count)'])
    xlabel('Presented Vowel');ylabel('Count of Responses')
    fname = sprintf('stackVowInitCount%s.eps',SUBJECTS{i});
    print(gcf, fullfile('./stackedBarFigsReOrder/', fname), '-depsc2');
    
    figure;bar(pm,'stacked'); ylim([0 100]);
    set(gca,'XTick',1:length(VOWS),'XTickLabel',VOWS,'FontSize',16)
    title(['Initial Vowels for ' SUBJECTS{i} ' (Percentages)'])
    legend(VOWS, 'Location', 'NorthEastOutside','FontSize',11)
    xlabel('Presented Vowel');ylabel('Percentages of Responses')
    fname = sprintf('stackVowInitPer%s.eps',SUBJECTS{i});
    print(gcf, fullfile('./stackedBarFigsReOrder/', fname), '-depsc2');

%%
    cm = CMs{4}; % CMs 1-2 Cons, 3-4 Vows (odd Initial, even Final),
    % Vows Final
    vowcm(1:15,:) = cm(vowOrd,:);
    N = sum(vowcm,2);  % Sum of the rows total of each occurance
    N = repmat(N,[1 length(VOWS)]);
    pm = 100*(vowcm./N);
    vowcm(:,1:15) = vowcm(:,vowOrd);
    pm(:,1:15)= pm(:,vowOrd);
    figure;bar(vowcm,'stacked')
    set(gca,'XTick',1:length(VOWS),'XTickLabel',VOWS,'FontSize',16)
    legend(VOWS, 'Location', 'NorthEastOutside','FontSize',11)
    title(['Final Vowels for ' SUBJECTS{i} ' (Count)'])
    xlabel('Presented Vowel');ylabel('Count of Responses')
    fname = sprintf('stackVowFinCount%s.eps',SUBJECTS{i});
    print(gcf, fullfile('./stackedBarFigsReOrder/', fname), '-depsc2');
    
    figure;bar(pm,'stacked'); ylim([0 100]);
    set(gca,'XTick',1:length(VOWS),'XTickLabel',VOWS,'FontSize',16)
    title(['Final Vowels for ' SUBJECTS{i} ' (Percentages)'])
    legend(VOWS, 'Location', 'NorthEastOutside','FontSize',11)
    xlabel('Presented Vowel');ylabel('Percentages of Responses')
    fname = sprintf('stackVowFinPer%s.eps',SUBJECTS{i});
    print(gcf, fullfile('./stackedBarFigsReOrder/', fname), '-depsc2');
end

toc;


