1 function [C,N,LAGS] = xcovf(X,Y,MAXLAG,SCALEOPT)
2 % XCOVF generates cross-covariance function.
3 % XCOVF is the same as XCORR except
4 % X and Y can contain missing values encoded with NaN.
5 % NaN's are skipped, NaN do not result in a NaN output.
6 % The output gives NaN only if there are insufficient input data
8 % [C,N,LAGS] = xcovf(X,MAXLAG,SCALEOPT);
9 % calculates the (auto-)correlation function of X
10 % [C,N,LAGS] = xcovf(X,Y,MAXLAG,SCALEOPT);
11 % calculates the crosscorrelation function between X and Y
13 % SCALEOPT [character string] specifies the type of scaling applied
14 % to the correlation vector (or matrix). is one of:
15 % 'none' return the unscaled correlation, R,
16 % 'biased' return the biased average, R/N,
17 % 'unbiased' return the unbiassed average, R(k)/(N-|k|),
18 % 'coeff' return the correlation coefficient, R/(rms(x).rms(y)),
19 % where "k" is the lag, and "N" is the length of X.
20 % If omitted, the default value is "none".
21 % If Y is supplied but does not have the ame length as X,
22 % scale must be "none".
25 % see also: COVM, XCORR
27 % $Id: xcovf.m 9608 2012-02-10 09:56:25Z schloegl $
28 % Copyright (C) 2005,2010,2011 by Alois Schloegl <alois.schloegl@gmail.com>
29 % This function is part of the NaN-toolbox
30 % http://pub.ist.ac.at/~schloegl/matlab/NaN/
32 % This program is free software; you can redistribute it and/or modify
33 % it under the terms of the GNU General Public License as published by
34 % the Free Software Foundation; either version 3 of the License, or
35 % (at your option) any later version.
37 % This program is distributed in the hope that it will be useful,
38 % but WITHOUT ANY WARRANTY; without even the implied warranty of
39 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 % GNU General Public License for more details.
42 % You should have received a copy of the GNU General Public License
43 % along with this program; If not, see <http://www.gnu.org/licenses/>.
53 elseif all(size(Y)==1),
65 elseif isempty(Y) && isempty(MAXLAG)
68 [C,LAGS] = xcorr(X,'none');
69 [N,LAGS] = xcorr(1-NX,'none');
70 elseif ~isempty(Y) && isempty(MAXLAG)
75 [C,LAGS] = xcorr(X,Y,'none');
76 [N,LAGS] = xcorr(1-NX,1-NY,'none');
77 elseif isempty(Y) && ~isempty(MAXLAG)
80 [C,LAGS] = xcorr(X,MAXLAG,'none');
81 [N,LAGS] = xcorr(1-NX,MAXLAG,'none');
82 elseif ~isempty(Y) && ~isempty(MAXLAG)
87 [C,LAGS] = xcorr(X,Y,MAXLAG,'none');
88 [N,LAGS] = xcorr(1-NX,1-NY,MAXLAG,'none');
93 elseif strcmp(SCALEOPT,'none')
96 elseif strcmp(SCALEOPT,'coeff')
98 if ~any(size(X)==1), %% ~isvector(X)
99 c = C(ix,1:size(X,2)+1:end); %% diagonal elements
100 v = c.^-0.5; % sqrt(1./c(:));
102 C = C.*repmat(v(:).',size(C,1),1);
106 C = C/sqrt(sumsq(X)*sumsq(Y));
109 elseif strcmp(SCALEOPT,'biased')
110 C = C./repmat(max(N),size(C,1),1);
112 elseif strcmp(SCALEOPT,'unbiased')
113 C = C./(repmat(max(N),size(C,1),1)-repmat(LAGS,1,size(C,2)));
116 warning('invalid SCALEOPT - not supported');