Skip to content
Snippets Groups Projects
xcf.m 1.5 KiB
Newer Older
marwan's avatar
marwan committed
function [c_out,s_out]=xcf(x,y,t,flag)
%XCF   Computes and plots crosscorrelation.
%    [C,S]=XCF(X,Y [,T,FLAG]) computes crosscorrelation C
%    between the data in the vectors X and Y with the maximal 
%    lag T (optional). If FLAG is set, the plot is suppressed. 
%    The optional output S stores the 5% significance level.
%
%    See also COR, COV, XCORR, ACF

% Copyright (c) 2001-2002 by AMRON
% Norbert Marwan, Potsdam University, Germany
% http://www.agnld.uni-potsdam.de
%
% $Date$
% $Revision$
%
% $Log$
%
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License
% as published by the Free Software Foundation; either version 2
% of the License, or any later version.

error(nargchk(1,4,nargin));

% check the input
if nargin<2
  y=x;
end
if length(x)~=length(y)
  error('The lengths of x and y must match.')
end
if nargin<3
  t=[];
end

if nargin<4
  flag=0;
end

% check if xcorr exist
if isempty(which('xcorr'))
  disp('Sorry, signal processing toolbox needed.')
  if nargout==1
    c_out(1:t)=NaN;
  elseif nargout==2
    c_out(1:t)=NaN;
    s_out(1:t)=NaN;  
  end

else

% compute xcf
x=trafo(x);
y=trafo(y);
[c, s]=xcorr(x,y,t,'coeff');
c=flipud(c);
s=s';
L=length(x)-abs(s);
si=2./sqrt(L+2);

% output
if flag==0
bar(s,c,'k')
hold on
plot(s,si,'r-.')
plot(s,-si,'r-.')
line([0 0],[-1 1],'linestyle',':')
ha=axis;
line([ha(1) ha(2)],[0 0],'linestyle',':')
hold off
end

if nargout==1
  c_out=c;
elseif nargout==2
  c_out=c;
  s_out=si;  
end

end