% Solution to the Big Exercise.

% The file is divided in three parts (cells). First, for one subject, one
% trial, then one subject, all trials, then all subjects, all trials. Each
% cell can be executed independently, but you must execute the first cell
% that generates the data in the first place.


%%
% Get data from the function GetData.
[inp,js]    = GetData;


%% First part: single trial, single subject
% From the full data, extract the data for the first trial, first subject.

js1  = js(1,:,1);%Joystick movements for first subject, first trial
inp1 = inp(1,:); % Visual input for first subject, first trial

% Find the number of time points from the data
T = size(js1,2);

% Using the plot command, plot the data for both, together
figure(1)
plot(1:T,js1,1:T,inp1);


% Set up a threshold for the decision
thres   = 0.3;

% Find the decision time (DT) and re-decision time (RDT)
DT      = find(js1<(-1+thres),1);
RDT     = find(js1>(1-thres),1);

% Find the times in which the input actually switched
RST     = find(inp1==1,1);

% Reaction time equals decision time
rDT     = DT;
% Calculate redecision reaction time
rRDT    = RDT - RST;


% Print the conclusion for this subject.
yay = ['For this subject, the re-decision time was larger than the',...
    ' decision time'];
nay = ['For this subject, the re-decision time was smaller than the',...
    ' decision time'];

if rRDT>DT
    display(yay)
else
    display(nay)
end

%% Second part: multi-trial, single subject
% Everything in the previous cell was copied into a function called
% SingleAll. See the function for more info.

% Find out the number of trials
nT      = size(js,1);

% Call Single(js2,inp2) once per trial

for i = 1:nT
    js2 = js(i,:,1);
    inp2= inp(i,:);
    [rDT(i),rRDT(i)] = SingleAll(js2,inp2);
end

% Calculate the average rDT and rRDT for this subject
avrDT   = mean(rDT);
avrRDT  = mean(rRDT);


%% Third part: multi-trial, multi-subject
% Find the number of trials and subjects, and time steps, why not.
[nT,T,nS]   =   size(js);

% Initialize matrices rDT2 and rRDT2. This speeds up execution.
rDT2        =   zeros(nT,nS);
rRDT2       =   zeros(nT,nS);


for s = 1:nS
    for tr = 1:nT
        js3     = js(tr,:,s);
        inp3    = inp(tr,:);
        [rDT2(tr,s),rRDT2(tr,s)] = SingleAll(js3,inp3);
    end
end

% Calculate average per subject
for s = 1:nS
    AvPerSubDT(s)   = mean(rDT2(:,s));
    AvPerSubRDT(s)  = mean(rRDT2(:,s));
end


% Plots of histograms from reaction times.
fig2 = figure(2);
set(fig2,'Name','Histogram of average reaction time over subjects');
hist(AvPerSubDT)
title('Histogram of the average reaction times over subjects')
xlabel('Reaction times (ms)')
ylabel('Number of subjects with this reaction time')

fig3 = figure(3);
set(fig3,'Name','Histogram of average re-decision reaction time over subjects');
hist(AvPerSubRDT)
title('Histogram of the average re-decision reaction times over subjects')
xlabel('Re-decision reaction times (ms)')
ylabel('Number of subjects with this reaction time')






