function  trans = PLM_transitions(stim_congr,resp_correct)

%PLM_TRANSITIONS -- Fine-grained PercLearn dynamics at transition points.
%
% trans = PLM_transitions(stim_congr,resp_correct)
%
% STIM_CONGR is a 9600x1 vector: 1="congruent", 2="incongruent".
% RESP_CORRECT is a 9600x1 vector: 0="incorrect, 1="correct", 9="missing".
% The stimulus presentation schedule is assumed to be as in the Perceptual
% Learning Experiment 1 (see PLE_TRANSITIONS, PLEXP1, PLEDATA_BY_SBJ).
% That is, 8 days * 4 blocks/day * 300 trials/block = 9600 trials total.
% There are 31 transitions between blocks, of the following three types:
%  * 5 "context transitions" -- after blocks [1 9 17 25 31]
%  * 7 "night transitions"   -- after blocks [4 8 12 16 20 24 28]
%  *19 "block transitions" -- [2 3 5:7,10 11 13:15,18 19 21:23,26 27 29 30]
%
% For each transition, the stimulus sequence is analyzed in "subblocks"
% of 30 trials each. 10 subblocks are tracked prior to the transition
% point and 10 after. The probability correct is calculated for each
% subblock, separately for congruent and incongruent stimulu. The stimulus
% contrast (or difficulty level) is ignored for the present purposes.
% This produces a 20x2 matrix of probabilities, where the transition
% boundary occurs between subblocks 10 and 11. The matrices of the
% corresponding kind (5 context, 7 night, and 19 block) are then averaged
% for each subject and a z-transformation is applied on the averages.
% TRANS is a structure with the follwing fields:
%  - zP320c -- zP correct by subblock,   congruent stimuli, 320x1
%  - zP320i -- zP correct by subblock, incongruent stimuli, 320x1
%  - contextc -- zP averaged over 5 context-switch transitions,(10+10)x1
%  - contexti -- zP, incongr. stim, context-switch transitions,(10+10)x1
%  - nightc -- zP,   congruent stim, 7 overnight transitions,  (10+10)x1
%  - nighti -- zP, incongruent stim, 7 overnight transitions,  (10+10)x1
%  - blockc -- zP,   congruent stim, remaining 19 transitions, (10+10)x1
%  - blocki -- zP, incongruent stim, remaining 19 transitions, (10+10)x1
%
% Example:
%  o = PLModel1(descr,Mparams) ; resp_correct = (o(:,1)==descr(:,1)) ;
%  stim_congr = 2-(descr(:,1)==descr(:,3)) ;  % 1="congr", 2="incongr"
%  trans=PLM_transitions(stim_congr,resp_correct) ; sbl=[-285:30:+285];
%  n=trans.night;plot(sbl(1:10),n(1:10,:),'s-',sbl(11:20),n(11:20,:),'o-')
%
% See also PLM_STATS, PLETRANS_BY_SBJ, PLMODEL1.

% Original coding by Alexander Petrov, UC Irvine.
% $Revision: 1.0 $  $Date: 2003/03/04 11:00 $

%-- Define constants
block_size = 300 ;
N_blocks = 32 ;
N_trials = N_blocks * block_size ;        % 9600
SUBBLOCK_SIZE = 30 ;
N_subblocks = N_trials / SUBBLOCK_SIZE ;  %  320
b2s = block_size / SUBBLOCK_SIZE ;        % 10 subblocks per block
P_min = 0.5/SUBBLOCK_SIZE ; P_max = 1-P_min ;

PRECONTEXT_BLOCKS = [1 9 17 25 31] ;
PRENIGHT_BLOCKS = [4:4:28] ;
OTHER_BLOCKS = [2 3 5 6 7,10 11 13 14 15,18 19 21 22 23,26 27 29 30] ;

%-- Partition into subblocks and calculate zP(correct|congruence)
subblock = floor([0:N_trials-1]'./SUBBLOCK_SIZE)+1 ;    %[9600x1]
win = [-b2s:+b2s-1]' ;      % sliding window: 10 subblocks pre + 10 post

[N,vals] = xtab2([stim_congr,subblock],resp_correct) ;  % [640x2]
N_correct = reshape(N(:,2),N_subblocks,2) ;             % [320x2]
N_total = reshape(N(:,1)+N(:,2),N_subblocks,2) ;        % [320x2]
%N(:,3) counts the missing responses, if any.
P_correct = N_correct./N_total ;                        % [320x2]
Z = norminv(max(P_min,min(P_correct,P_max))) ;          % [320x2]
Zc = Z(:,1) ;        % congruent stimuli
Zi = Z(:,2) ;        % incongruent stimuli
trans.zP320c = Zc ;  % [320x1]
trans.zP320i = Zi ;  % [320x1]

%-- Context transitions
transition_subblock = PRECONTEXT_BLOCKS.*b2s + 1 ;      % [5x1]
idx = repmat(transition_subblock,2*b2s,1) + ...
      repmat(win,1,length(PRECONTEXT_BLOCKS)) ;         % [20x5]
trans.contextc = mean(Zc(idx),2) ;      % [20x1]
trans.contexti = mean(Zi(idx),2) ;      % [20x1]

%-- Overnight transitions
transition_subblock = PRENIGHT_BLOCKS.*b2s + 1 ;        % [7x1]
idx = repmat(transition_subblock,2*b2s,1) + ...
      repmat(win,1,length(PRENIGHT_BLOCKS)) ;           % [20x7]
trans.nightc = mean(Zc(idx),2) ;        % [20x1]
trans.nighti = mean(Zi(idx),2) ;        % [20x1]

%-- Other ("baseline") transitions
transition_subblock = OTHER_BLOCKS.*b2s + 1 ;           % [19x1]
idx = repmat(transition_subblock,2*b2s,1) + ...
      repmat(win,1,length(OTHER_BLOCKS)) ;              % [20x19]
trans.blockc = mean(Zc(idx),2) ;        % [20x1]
trans.blocki = mean(Zi(idx),2) ;        % [20x1]

%--- Return TRANS
%%%%% End of PLM_TRANSITIONS.M
