1function [mu,sd,COV,xc,M,R2]=decovm(XCN,NN)
2% decompose extended covariance matrix into mean (mu),
3% standard deviation, the (pure) Covariance (COV),
4% correlation (xc) matrix and the correlation coefficients R2.
5% NaN's are condsidered as missing values.
6% [mu,sd,COV,xc,N,R2]=decovm(ECM[,NN])
7%
8% ECM 	is the extended covariance matrix
9% NN	is the number of elements, each estimate (in ECM) is based on
10%
11% see also: MDBC, COVM, R2
12
13%	Copyright (c) 1999-2002,2009 by  Alois Schloegl
14%       This function is part of the NaN-toolbox
15%       http://pub.ist.ac.at/~schloegl/matlab/NaN/
16
17% This program is free software; you can redistribute it and/or
18% modify it under the terms of the GNU General Public License
19% as published by the Free Software Foundation; either version 3
20% of the  License, or (at your option) any later version.
21%
22% This program is distributed in the hope that it will be useful,
23% but WITHOUT ANY WARRANTY; without even the implied warranty of
24% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25% GNU General Public License for more details.
26%
27% You should have received a copy of the GNU General Public License
28% along with this program; if not, write to the Free Software
29% Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
30
31[r,c]=size(XCN);
32if r~=c,
33        fprintf(2,'Warning DECOVM: input argument is not a square matrix\n');
34        XCN = ecovm(XCN);
35        c = c + 1;
36else
37	M = XCN(1,1);
38	if nargin<2,
39                XCN = XCN/(XCN(1,1));
40        else %if nargin==2
41                XCN = XCN./(NN);
42	end;
43
44	if any(isnan(XCN(:))),
45		warning('DECOVM: Extended Covariance Matrix should not contain NaN''s');
46	end;
47	if 0, %det(XCN)<0; % check removed for performance reasons
48		warning('DECOVM: Extended Covariance Matrix must be non-negative definite');
49	end;
50end;
51
52mu  = XCN(1,2:c);
53COV = XCN(2:c,2:c) - mu'*mu;
54sd  = sqrt(diag(COV))';
55if nargout<4, return; end;
56xc  = COV./(sd'*sd);
57M   = XCN(1,1);
58if nargout<6, return; end;
59R2  = xc.*xc;
60
61return;
62
63mu=XCN(2:N,1)/XCN(1,1);
64COV=(XCN(2:N,2:N)/XCN(1,1)-XCN(2:N,1)*XCN(1,2:N)/XCN(1,1)^2);
65sd=sqrt(diag(COV));
66xc=COV./(sd*sd');
67
68% function [ECM] = ecovm(signal);
69% Generates extended Covariance matrix,
70% ECM= [l signal]'*[l signal]; % l is a matching column of 1's
71% ECM is additive, i.e. it can be applied to subsequent blocks and summed up afterwards
72% [ECM1] = ecovm(s1);
73% [ECM2] = ecovm(s1);
74% [ECM]  = ecovm([s1;s2]);
75% ECM1+ECM2==ECM;
76%
77% SS=sum(signal); ECM=[[size(signal,1),SS];[SS',signal'*signal]];
78