% Calculate the skew from microphone 1 --> microphone 2 and from
% microphone 3 --> microphone 4.
%
% INPUTS:
%   p12    - the recordings from microphones 1 and 2
%   p34    - the recordings from microphones 3 and 4
%   sr     - the samping rate
%   time   - the time (in seconds) of each frame
%   figNum - the plot figure number
%
% OUTPUTS:
%   win  - the frame size in samples
%   corr - the correlation peaks
%
% Christine Smit Mar-28-2007

function [win,corr] = problem(p12,p34,sr,time,figNum)

win = sr*time;

num = floor(length(p12)/win);

corr = zeros(num,2);

disp(sprintf('Total windows: %i',num))
for i=1:num
    [b e] = calc_index(i,win);    
    corr(i,1) = xcorrpeak(p12(b:e,1),p12(b:e,2));
    corr(i,2) = xcorrpeak(p34(b:e,1),p12(b:e,2));
    if mod(i,100) == 0
        disp(i);
    end
end

figure(figNum);
plot(corr(:,1),corr(:,2),'x');
title(sprintf('Skew Scatter: window size %f seconds (%d elements)',time,win));
xlabel('channel 1 -> channel 2');
ylabel('channel 3 -> channel 4');

function [beg, en] = calc_index(win_num,win)

% convert to a zero-index window
win_num = win_num-1;

beg = win*win_num + 1;
en = win*(win_num+1);