% Data analysis across multiple subjects
% 
% EE 362 Project
% Steven Backer
% Winter 2006

clear all

% === Load Data ===

% load data for subject 1 (pre-training)
load -ascii ./responses/sub1_pre/seq_p2.txt  % input
load -ascii ./responses/sub1_pre/resp_p2.txt % responses
sub1_seq_pre = seq_p2;
sub1_resp_pre = resp_p2;
clear seq_p2
clear resp_p2

% load data for subject 1 (post-training)
load -ascii ./responses/sub1_post/seq_p2.txt  % input
load -ascii ./responses/sub1_post/resp_p2.txt % responses
sub1_seq_post = seq_p2;
sub1_resp_post = resp_p2;
clear seq_p2
clear resp_p2

% load data for subject 2 (pre-training)
load -ascii ./responses/sub2_pre/seq_p2.txt  % input
load -ascii ./responses/sub2_pre/resp_p2.txt % responses
sub2_seq_pre = seq_p2;
sub2_resp_pre = resp_p2;
clear seq_p2
clear resp_p2

% load data for subject 2 (post-training)
load -ascii ./responses/sub2_post/seq_p2.txt  % input
load -ascii ./responses/sub2_post/resp_p2.txt % responses
sub2_seq_post = seq_p2;
sub2_resp_post = resp_p2;
clear seq_p2
clear resp_p2

% =================


% === Call Analysis Function ===

[sub1_perc_dif_pre,sub1_perc_dif_post,sub1_dif] = ... 
    analyze_data(sub1_seq_pre,sub1_resp_pre,sub1_seq_post,sub1_resp_post);

[sub2_perc_dif_pre,sub2_perc_dif_post,sub2_dif] = ... 
    analyze_data(sub2_seq_pre,sub2_resp_pre,sub2_seq_post,sub2_resp_post);

% =================


% === Sort ===

sub1_perc_dif_pre_sort = ...
  [sub1_perc_dif_pre(4:8); sub1_perc_dif_pre(2); sub1_perc_dif_pre(9:end)];
sub1_perc_dif_post_sort = ...
  [sub1_perc_dif_post(4:8); sub1_perc_dif_post(2); sub1_perc_dif_post(9:end)];

sub2_perc_dif_pre_sort = ...
  [sub2_perc_dif_pre(4:8); sub2_perc_dif_pre(2); sub2_perc_dif_pre(9:end)];
sub2_perc_dif_post_sort = ...
  [sub2_perc_dif_post(4:8); sub2_perc_dif_post(2); sub2_perc_dif_post(9:end)];

% =================


% ===Plot "% Different" Functions ===

% frequencies
freqs_dif = [3:.2:5.4];
freqs_same = [3, 4, 5.4];
mSize = 8; % marker size

figure(1)
clf
plot(freqs_dif,sub1_perc_dif_pre_sort ,'--o','MarkerSize',mSize) %different
hold on
plot(freqs_dif,sub1_perc_dif_post_sort,'-s','MarkerSize',mSize) %different
plot(freqs_same,sub1_perc_dif_pre(1:3),'o','MarkerSize',mSize,'MarkerFaceColor','b') %same
plot(freqs_same,sub1_perc_dif_post(1:3),'s','MarkerSize',mSize,'MarkerFaceColor','b') %same
hold off
xlabel('Auditory Temporal Rate (Hz)')
ylabel('"% different"')
title('Subject #1')
legend('pre-train, Vis = 4Hz','post-train, Vis = 4Hz','pre-train, Vis = Aud','post-train, Vis = Aud','Location','North')
grid on;
axis([2.9 5.5 0 100])

figure(2)
clf
plot(freqs_dif,sub2_perc_dif_pre_sort ,'--o','MarkerSize',mSize) %different
hold on
plot(freqs_dif,sub2_perc_dif_post_sort,'-s','MarkerSize',mSize) %different
plot(freqs_same,sub2_perc_dif_pre(1:3),'o','MarkerSize',mSize,'MarkerFaceColor','b') %same
plot(freqs_same,sub2_perc_dif_post(1:3),'s','MarkerSize',mSize,'MarkerFaceColor','b') %same
hold off
xlabel('Auditory Temporal Rate (Hz)')
ylabel('"% different"')
title('Subject #2')
legend('pre-train, Vis = 4Hz','post-train, Vis = 4Hz','pre-train, Vis = Aud','post-train, Vis = Aud','Location','North')
grid on;
axis([2.9 5.5 0 100])

% =================


% ===Plot Post-Pre Functions ===

figure(3)
clf
plot(freqs_dif,sub1_dif,'--o')
xlabel('Auditory Temporal Rate (Hz)')
ylabel('Post - Pre (%)')
title('Subject #1')
grid on;
axis([3 5.4 -50 50])

% fit a curve
P = polyfit([3:.2:5.4]',sub1_dif,3);
hold on
points = [3:.01:5.4];
curve = polyval(P,points);
plot(points,curve)
hold off

figure(4)
clf
plot(freqs_dif,sub2_dif,'--o')
xlabel('Auditory Temporal Rate (Hz)')
ylabel('Post - Pre (%)')
title('Subject #2')
grid on;
axis([3 5.4 -50 50])

% fit a curve
P = polyfit([3:.2:5.4]',sub2_dif,3);
hold on
curve = polyval(P,points);
plot(points,curve)
hold off

% =================


% ===Compute Means over Subjects ===

perc_dif_pre_avg = (sub1_perc_dif_pre + sub2_perc_dif_pre) / 2;
perc_dif_post_avg = (sub1_perc_dif_post + sub2_perc_dif_post) / 2;
dif_avg = (sub1_dif + sub2_dif) / 2;

% sort
avg_perc_dif_pre_sort = ...
  [perc_dif_pre_avg(4:8); perc_dif_pre_avg(2); perc_dif_pre_avg(9:end)];
avg_perc_dif_post_sort = ...
  [perc_dif_post_avg(4:8); perc_dif_post_avg(2); perc_dif_post_avg(9:end)];

% =================


% ===Plot Means over Subjects Functions ===

figure(5)
clf
plot(freqs_dif,avg_perc_dif_pre_sort ,'--o','MarkerSize',mSize) %different
hold on
plot(freqs_dif,avg_perc_dif_post_sort,'-s','MarkerSize',mSize) %different
plot(freqs_same,perc_dif_pre_avg(1:3),'o','MarkerSize',mSize,'MarkerFaceColor','b') %same
plot(freqs_same,perc_dif_post_avg(1:3),'s','MarkerSize',mSize,'MarkerFaceColor','b') %same
hold off
xlabel('Auditory Temporal Rate (Hz)')
ylabel('"% different"')
title('Average Across Subjects')
legend('pre-train, Vis = 4Hz','post-train, Vis = 4Hz','pre-train, Vis = Aud','post-train, Vis = Aud','Location','North')
grid on;
axis([2.9 5.5 0 100])

figure(6)
clf
plot(freqs_dif,dif_avg,'--o')
xlabel('Auditory Temporal Rate (Hz)')
ylabel('Post - Pre (%)')
title('Average Across Subjects')
grid on;
axis([3 5.4 -30 30])

% fit a curve
P = polyfit([3:.2:5.4]',dif_avg,3);
hold on
curve = polyval(P,points);
plot(points,curve)
hold off

% =================


