]> Creatis software - CreaPhase.git/blob - octave_packages/signal-1.1.3/xcorr2.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / signal-1.1.3 / xcorr2.m
1 ## Copyright (C) 2000 Dave Cogdell <cogdelld@asme.org>
2 ## Copyright (C) 2000 Paul Kienzle <pkienzle@users.sf.net>
3 ##
4 ## This program is free software; you can redistribute it and/or modify it under
5 ## the terms of the GNU General Public License as published by the Free Software
6 ## Foundation; either version 3 of the License, or (at your option) any later
7 ## version.
8 ##
9 ## This program is distributed in the hope that it will be useful, but WITHOUT
10 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 ## details.
13 ##
14 ## You should have received a copy of the GNU General Public License along with
15 ## this program; if not, see <http://www.gnu.org/licenses/>.
16
17 ## -*- texinfo -*-
18 ## @deftypefn {Function File} {@var{c} =} xcorr2 (@var{a})
19 ## @deftypefnx {Function File} {@var{c} =} xcorr2 (@var{a}, @var{b})
20 ## @deftypefnx {Function File} {@var{c} =} xcorr2 (@dots{}, @var{scale})
21 ## Compute the 2D cross-correlation of matrices @var{a} and @var{b}.
22 ##
23 ## If @var{b} is not specified, it defaults to the same matrix as @var{a}, i.e.,
24 ## it's the same as @code{xcorr(@var{a}, @var{a})}.
25 ##
26 ## The optional argument @var{scale}, defines the type of scaling applied to the
27 ## cross-correlation matrix (defaults to "none"). Possible values are:
28 ## @itemize @bullet
29 ## @item "biased"
30 ##
31 ## Scales the raw cross-correlation by the maximum number of elements of @var{a}
32 ## and @var{b} involved in the generation of any element of @var{c}.
33 ##
34 ## @item "unbiased"
35 ##
36 ## Scales the raw correlation by dividing each element in the cross-correlation
37 ## matrix by the number of products @var{a} and @var{b} used to generate that
38 ## element 
39 ##
40 ## @item "coeff"
41 ##
42 ## Normalizes the sequence so that the largest cross-correlation element is
43 ## identically 1.0.
44 ##
45 ## @item "none"
46 ##
47 ## No scaling (this is the default).
48 ## @end itemize
49 ## @seealso{conv2, corr2, xcorr}
50 ## @end deftypefn
51
52 function c = xcorr2 (a, b = a, biasflag = "none")
53
54   if (nargin < 1 || nargin > 3)
55     print_usage;
56   elseif (nargin == 2 && ischar (b))
57     biasflag = b;
58     b        = a;
59   endif
60
61   ## compute correlation
62   [ma,na] = size(a);
63   [mb,nb] = size(b);
64   c = conv2 (a, conj (b (mb:-1:1, nb:-1:1)));
65
66   ## bias routines by Dave Cogdell (cogdelld@asme.org)
67   ## optimized by Paul Kienzle (pkienzle@users.sf.net)
68   if strcmp(lower(biasflag), 'biased'),
69     c = c / ( min ([ma, mb]) * min ([na, nb]) );
70   elseif strcmp(lower(biasflag), 'unbiased'), 
71     lo   = min ([na,nb]);
72     hi   = max ([na, nb]);
73     row  = [ 1:(lo-1), lo*ones(1,hi-lo+1), (lo-1):-1:1 ];
74
75     lo   = min ([ma,mb]);
76     hi   = max ([ma, mb]);
77     col  = [ 1:(lo-1), lo*ones(1,hi-lo+1), (lo-1):-1:1 ]';
78
79     bias = col*row;
80     c    = c./bias;
81
82   elseif strcmp(lower(biasflag),'coeff'),
83     c = c/max(c(:))';
84   else
85     error ("invalid type of scale %s", biasflag);
86   endif
87 endfunction