]> Creatis software - CreaPhase.git/blob - octave_packages/signal-1.1.3/xcov.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / signal-1.1.3 / xcov.m
1 ## Copyright (C) 1999, 2001 Paul Kienzle <pkienzle@users.sf.net>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## usage: [c, lag] = xcov (X [, Y] [, maxlag] [, scale])
17 ##
18 ## Compute covariance at various lags [=correlation(x-mean(x),y-mean(y))].
19 ##
20 ## X: input vector
21 ## Y: if specified, compute cross-covariance between X and Y,
22 ## otherwise compute autocovariance of X.
23 ## maxlag: is specified, use lag range [-maxlag:maxlag], 
24 ## otherwise use range [-n+1:n-1].
25 ## Scale:
26 ##    'biased'   for covariance=raw/N, 
27 ##    'unbiased' for covariance=raw/(N-|lag|), 
28 ##    'coeff'    for covariance=raw/(covariance at lag 0),
29 ##    'none'     for covariance=raw
30 ## 'none' is the default.
31 ##
32 ## Returns the covariance for each lag in the range, plus an 
33 ## optional vector of lags.
34
35 function [retval, lags] = xcov (X, Y, maxlag, scale)
36
37   if (nargin < 1 || nargin > 4)
38     print_usage;
39   endif
40
41   if nargin==1
42     Y=[]; maxlag=[]; scale=[];
43   elseif nargin==2
44     maxlag=[]; scale=[];
45     if ischar(Y), scale=Y; Y=[];
46     elseif isscalar(Y), maxlag=Y; Y=[];
47     endif
48   elseif nargin==3
49     scale=[];
50     if ischar(maxlag), scale=maxlag; maxlag=[]; endif
51     if isscalar(Y), maxlag=Y; Y=[]; endif
52   endif
53
54   ## XXX FIXME XXX --- should let center(Y) deal with []
55   ## [retval, lags] = xcorr(center(X), center(Y), maxlag, scale);
56   if (!isempty(Y))
57     [retval, lags] = xcorr(center(X), center(Y), maxlag, scale);
58   else
59     [retval, lags] = xcorr(center(X), maxlag, scale);
60   endif
61   
62 endfunction