function corr_view(signal, to_corr, window_length, thresh)
%function corr_view(signal, to_corr, window_length, thresh)
%
% CORR_VIEW -- A real-time demonstration of runnning correlation
%
% Input Parameters:
%    signal        -- the "noisy" signal in which we're looking for to_corr
%    to_corr       -- the thing we're looking for in signal
%    window_length -- size of the window in samples (optional)
%    threh         -- pause when the correlation exceeds this value, pos or neg (optional)
%
% Output Parmeters:
%    none

% Written by Mark Bartsch, Winter 2002
% Modification History:
%    8/16/02  --  Added modification history (MB)



pre_frac = .5;      % What fraction of the window is devoted to the "past"?
line_width = 1.5;   % How wide should the signal lines be?
skip = 5;           % How many samples to we skip per update
pausetime = .1;     % How much time to we pause between updates?


if ~exist('thresh','var') | isempty(thresh)
    thresh = inf;
end

if ~exist('window_length','var') | isempty(window_length)
    window_length = 2.6*length(to_corr);
end

sig_length = length(signal);

if (1-pre_frac)*window_length < length(to_corr)
    window_length = (1-pre_frac)*window_length + length(to_corr);
end

pre_samples = pre_frac*window_length;
post_samples = window_length - pre_samples - length(to_corr);

if size(to_corr,1) > size(to_corr,2)
    to_corr = to_corr';
end

if size(signal,1) > size(signal,2);
    signal = signal';
end

t = -(pre_samples):(length(to_corr) - 1 + post_samples);
c = [zeros(1,pre_samples) conv(signal,to_corr(end:-1:1))];

signal = [zeros(1,pre_samples+length(to_corr)) signal zeros(1,window_length*(1-pre_frac))];



subplot(211);
ylabel('Incoming Signal','FontSize',18);
set(gca,'XTick',[]);
h = plot(t,signal(1:length(t)),'g',0:(length(to_corr)-1),to_corr,'r');
h_corr = h(2);
h_sig = h(1);
axis([min(t)-1 max(t)+1 min(signal)*1.1 max(signal)*1.1]);
set(h_corr,'erasemode','background');
set(h_corr,'linewidth',line_width);
set(h_sig,'erasemode','background');
set(h_sig,'linewidth',line_width);
set(gca,'XTick',[]);

a = axis;
pl_height = a(4)-a(3);
h = line([0 0 length(to_corr)-1 length(to_corr)-1 ], ...
    [a(4), a(3)-.1*pl_height, a(3)-.1*pl_height a(4)], ...
    'clipping', 'off', 'Color',[0 0 0],'Erasemode','none'); 
h2 = line([length(to_corr)/2-1, length(to_corr)/2-1, 0, 0],...
    [a(3)-.1*pl_height a(3)-.3*pl_height a(3)-.3*pl_height a(3)-pl_height],...
    'clipping', 'off', 'Color',[0 0 0],'Erasemode','none');

subplot(212);
ylabel('Correlation Signal','FontSize',18);
set(gca,'XTick',[]);
h_res = plot(t(1:pre_samples),c(1:pre_samples));
axis([min(t)-1 max(t)+1 min(c)*1.1 max(c)*1.1]);
set(h_res,'erasemode','background');
set(h_res,'linewidth',line_width);
set(gca,'XTick',[]);

a = axis;
h3 = line([0 0],[a(3) a(4)],'Color',[0 0 0],'Erasemode','none');

zoom off;


for i=1:skip:(sig_length + length(to_corr))
    set(h_corr,'ydata',max(signal) + ones(size(to_corr)));
    set(h,'linewidth',.6);
    set(h_sig,'ydata',signal(i + (1:length(t))));
    set(h_corr,'ydata',to_corr);
    set(h,'linewidth',.5);
    
    set(h_res,'ydata',c(i + (1:pre_samples) - 1));
    
    if any(abs(c(i + pre_samples + (-skip:-1))) > thresh)
        pause;
    end
    
    pause(pausetime)
end
