% function [inp,js] = GetData()
% Function to generate the inputs for the exercises of the seventh session.
%
% It uses random number generators to generate fake behavioral data for the
% bee experiment from Hübner and Park.
% 
% The number of time steps, each representing a millisecond in the
% experiments, is set by T. nS is the number of subjects in the study and
% nT is the number of trials per subject. It is assumed that all trials for
% all subjects were valid (didn't meet the exclusion criteria). Moreover,
% all subjects made the right decision within the time limits (no timeouts
% or wrong decisions). This is made for simplicity in creating data, but in
% the actual experiments this was not the case.
%
% The joystick position is given noise over time through a Wiener process
% to simulate shaky hands.
%
% The transitions (from 0 to -1, and from -1 to 1) are smoothened
% throughout 100 and 200 milliseconds, respectively, with a linear
% function, to simulate the joystick movement.
%
% Input:
%   The function takes no inputs.
%
% Output:
%   inp(nT,T)           A vector (size = (nT,T)) with the visual input
%                       given to the subjects. All subjects receive exactly
%                       the same visual input for adequate data comparison.
%                       A row represents a trial, and each column
%                       represents a time point (milliseconds). A 0 (zero)
%                       means that the bee was flying around the left
%                       flower. A 1 means that the bee was flying around
%                       the right flower.
%   js(nT,T,nS)         Is the position of the joystick at every
%                       millisecond, for every trial, every subject. a 0
%                       (zero) means the central position, where no
%                       decision has been made. A -1 means the left
%                       position, meaning that the subject has decided that
%                       the bee is flying around the left flower. A 1
%                       indicates the right flower.
function [inp,js] = GetData()
% Set the parameters
T = 10000;
nS = 100;
nT = 10;


%% 
%Generate the Input to the subject. That is, which flower the bee is flying
%around for every time step in the task. 

% The inputs are saved in a matrix: Column is time, row is trial.

% The input-switch-times will follow a constant distribution from 4 to 6
% seconds.

swtimes = round((4+2*rand(1,nT))*1000);
inp = zeros(nT,T);

for trial = 1:nT
    inp(trial,swtimes(trial):end) = 1;
end



%%
% Generate data for subjects. The decision times (DT) and re-decision times
% (RDT) will follow gamma distributions of different parameters.


% Metaparameters for each subject:
% Average reaction time for each subject:
aDT = gamrnd(6,50*ones(nS,1));

% Generate DTs and RDTs for each subject, each trial. Each row is
% a subject, each column a trial.
DT = bsxfun(@plus,aDT, randn(nS,nT));
RDT = bsxfun(@plus,aDT, randn(nS,nT));
RDT = bsxfun(@plus,RDT,swtimes)+50;


% Generate the time series of the joystick movement. When the sensory input
% is at 0, the right decision is -1. When the sensory input is at 1, the
% right decision is 1. joystick position at 0 means no decision and is the
% starting point. Each page is a subject, each row a trial and each column
% the time.

js = zeros(nT,T,nS);

for s = 1:nS
    for trial = 1:nT
        js(trial,round(DT(s,trial)):end, s) = -1;
        js(trial,round(RDT(s,trial)):end,s) = 1;
    end
end

%%
% Create the transition when changing the joystick
smooth = 50;

for s = 1:nS
    for trial = 1:nT
        changes = find(abs(diff(js(trial,:,s)))>0.7,2);
        js(trial,(changes(1)-smooth+1):(changes(1)+smooth),s) = ...
            linspace(0,-1,2*smooth);
        js(trial,(changes(2)-2*smooth+1):(changes(2)+2*smooth),s) = ...
            linspace(-1,1,4*smooth);        
    end
end
        


%%
% Put noise into js using a Wiener process
wien = 0.01*(cumsum(randn(1,T))*0.1);

js = bsxfun(@plus,wien,js);

js(js<-1) = -1;
js(js>1) = 1;




%%
% save('DataBeeExperiment.mat','inp','js');


end
