function [] = figure4;

%
% function [] = figure4;
%
% plots raw version of figure 4 of Einhäuser, W., Methfessel, P., & Bendixen, A. (2017). Newly acquired audio-visual associations bias perception in binocular rivalry. Vision Research, 133, 121-129. doi: 10.1016/j.visres.2017.02.001
%
%

% 

% load data file
load eyeTraces oknGain 

% load files containing the conditions per observer and per block and trial
load toneByBlockAndTrail toneByBlockAndTrial
load conditionsByObserver ...
	lowPitchColorByObserver ...
	highPitchColorByObserver ...
	lowPitchDirectionByObserver ...
	highPitchDirectionByObserver


% sampling rate was 500Hz
sampRate = 500;

% variables in which per subject data shall be saved
meanDomDurSame   = nan(1,16);
meanDomDurOther  = nan(1,16);
meanDomDurNoTone = nan(1,16);

for obsNum = 1 : 16


  % colelct all dominance durations for all the 3 conditions over all trials
  domDurNoTone  = [];
  domDurSame    = [];
  domDurOther   = [];

  for block = 1 : 6
	
	for trial = 1 : 3
	  
	  % tone in current trial (1-low,2-high,3-none)
	  currentTone      =  toneByBlockAndTrial(obsNum,block,trial);

	  % drift direction associated with the tone in this observer
	  if (currentTone==3)
		% no tone
		currentDir = 0; 
	  elseif (currentTone==1)
		% low-pitch tone
		currentDir = lowPitchDirectionByObserver(obsNum);
	  elseif (currentTone==2)
		% high-pitch tone
		currentDir = highPitchDirectionByObserver(obsNum);
	  else
		error('incorrect condition');
	  end
	  
	  
	  % process the current trial
	  currentGain = oknGain{obsNum,block,trial};
	  
	  % find the valid data (no blinks, no fast phases)
	  ix = find(isfinite(currentGain));
	  
	  % find signchanges in the gain (ignoring blinks and fast phases)
	  D            = diff(sign(currentGain(ix)));
	  allSwitches  = ix(find(D~=0));
	  
	  % all dominance durations
	  domDurAll = diff(allSwitches)/sampRate;
	  
	  % split dominance durations for positive and negative gain
	  domDurPos = domDurAll(find(currentGain(allSwitches(1:end-1))<0));
	  domDurNeg = domDurAll(find(currentGain(allSwitches(1:end-1))>0));

	  % assign the dominance durations to the grating associated with
	  % the current tone or not associated with the current tone
	  if (currentDir==0)

		% no tone was played, all dominance durations are
		domDurNoTone = [domDurNoTone,domDurPos,domDurNeg];

	  elseif (currentDir==-1)
		
		% leftward drift is associated with current tone
		domDurSame = [domDurSame,domDurNeg];
		domDurOther = [domDurOther,domDurPos];
		
	  elseif (currentDir==1)
			
		% rightward drift is associated with current tone
		domDurSame = [domDurSame,domDurPos];
		domDurOther = [domDurOther,domDurNeg];
		
	  else
		error('something is wrong');
	  end
	  
	end % trial
  
  end % block

  % compute average dominance duration for the current observer
  meanDomDurSame(obsNum)   = mean(domDurSame);
  meanDomDurOther(obsNum)  = mean(domDurOther);
  meanDomDurNoTone(obsNum) = mean(domDurNoTone);
  
end % obsNum

figure(4);
clf;
% compute mean and sem
M = mean([meanDomDurSame;meanDomDurNoTone;meanDomDurOther]');
S = std([meanDomDurSame;meanDomDurNoTone;meanDomDurOther]')/sqrt(16);

bar([1:3],M);
hold on;
errorbar([1:3],M,S,'k.');
set(gca,'XTick',[1:3]);
set(gca,'XTickLabel',{'grating assoc. with tone','no tone','other grating'});
ylabel('dominance duration [s]');