function [m_dd] = analysis_repos_veto_block(subj,blocks)
% function [m_dd] = analysis_repos_veto_block(subj,blocks)
% outputs median dominance durations for the requested blocks
% called by analyzeMain.m
load('subjects_list.mat'); % list of participant and block order; 1-subj, 2-block, 3-belt_or_gear, 4-dir
curr_index = [];
belt_or_gear = [];
dir = [];
filename = [];
rot = [];
dat = [];
jumps_ind = [];
exp_start_ind = [];
exp_end_ind = [];
switch_points = [];
dom_dur = [];
for k = 1:2 % we have two blocks for each condition
curr_index = find(subjects_list(:, 1) == subj & subjects_list(:, 2) == blocks(k));
belt_or_gear = subjects_list(curr_index,3);
dir = subjects_list(curr_index,4);
filename = sprintf('dat_am1_%02d_%02d_%01d_%01d.mat',subj,blocks(k),belt_or_gear,dir);
load(filename); % variable name: "dat"
rot = diff(dat(:,2));
jumps_ind = find(abs(rot)>200);
for i = 1:length(jumps_ind)
if rot(jumps_ind(i)) < 0
rot(jumps_ind(i)) = 360 + rot(jumps_ind(i));
elseif rot(jumps_ind(i)) > 0
rot(jumps_ind(i)) = rot(jumps_ind(i)) - 360;
end
end
% get start and end indices
exp_start_ind = find(rot~=0,1,'first');
exp_end_ind = find(rot~=0,1,'last');
if exp_end_ind == length(rot)
exp_end_ind = exp_end_ind - 1;
end
% fill "holes" in data resulting from sampling rate difference: where only 1 iteration is 0 and dir. is the same before
% and after, replace with subsequent value:
% "Specifically, manipulandum
% rotation velocity data were segmented into periods of
% rotation in one direction or the other, as well as periods
% with no movement (no change of position for at least
% two subsequent sample points). For each observer and
% condition, perceptual stability was defined as the
% median duration of all nonzero velocity segments."
for i = exp_start_ind+1:exp_end_ind-1
if rot(i) == 0
if rot(i-1)*rot(i+1) > 0 % neither is 0 and they have the same sign
rot(i) = rot(i+1); % replace with next iteration's value
end
end
end
% get switching points (any change between positive, zero, or negative).
% Switching point is defined as the datapoint before the change.
switch_c = 0;
for i = exp_start_ind:exp_end_ind
if rot(i) > 0
if rot(i+1) <= 0
switch_c = switch_c + 1;
switch_points(switch_c) = i;
end
elseif rot(i) < 0
if rot(i+1) >= 0
switch_c = switch_c + 1;
switch_points(switch_c) = i;
end
elseif rot(i) == 0
if rot(i+1) ~= 0
switch_c = switch_c + 1;
switch_points(switch_c) = i;
end
end
end
% get dominance durations (where not 0)
dom_dur_c = 0;
for i = 1:(switch_c-1)
if mean(rot((switch_points(i)+1):switch_points(i+1))) ~= 0
dom_dur_c = dom_dur_c + 1;
dom_dur(dom_dur_c) = dat(switch_points(i+1),1)-dat(switch_points(i),1);
end
end
% get median dominance duration
if k == 1
if dom_dur_c >= 1
dd_1 = dom_dur;
else
dd_1 = NaN;
end
elseif k == 2
if dom_dur_c >= 1
dd_2 = dom_dur;
else
dd_2 = NaN;
end
end
curr_index = [];
belt_or_gear = [];
dir = [];
filename = [];
rot = [];
dat = [];
jumps_ind = [];
exp_start_ind = [];
exp_end_ind = [];
switch_points = [];
dom_dur = [];
end % for k = 1:2
m_dd = nanmedian(horzcat(dd_1,dd_2));
end