annotate extra/tsa/inst/acovf.m @ 12580:b6eace8bc216 octave-forge

[tsa] update contact email address
author schloegl
date Thu, 02 Apr 2015 10:00:34 +0000
parents 65a4be217166
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2414
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
1 function [ACF,NN] = acovf(Z,KMAX,Mode,Mode2);
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
2 % ACOVF estimates autocovariance function (not normalized)
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
3 % NaN's are interpreted as missing values.
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
4 %
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
5 % [ACF,NN] = acovf(Z,MAXLAG,Mode);
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
6 %
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
7 % Input:
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
8 % Z Signal (one channel per row);
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
9 % MAXLAG maximum lag
7501
65a4be217166 prepare next release
schloegl
parents: 4907
diff changeset
10 % Mode 'biased' : normalizes with N [default]
2414
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
11 % 'unbiased': normalizes with N-lag
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
12 % 'coeff' : normalizes such that lag 0 is 1
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
13 % others : no normalization
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
14 %
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
15 % Output:
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
16 % ACF autocovariance function
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
17 % NN number of valid elements
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
18 %
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
19 % REFERENCES:
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
20 % A.V. Oppenheim and R.W. Schafer, Digital Signal Processing, Prentice-Hall, 1975.
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
21 % S. Haykin "Adaptive Filter Theory" 3ed. Prentice Hall, 1996.
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
22 % M.B. Priestley "Spectral Analysis and Time Series" Academic Press, 1981.
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
23 % W.S. Wei "Time Series Analysis" Addison Wesley, 1990.
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
24 % J.S. Bendat and A.G.Persol "Random Data: Analysis and Measurement procedures", Wiley, 1986.
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
25
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
26 % $Id$
12580
b6eace8bc216 [tsa] update contact email address
schloegl
parents: 7501
diff changeset
27 % Copyright (C) 1998-2003,2008,2010 by Alois Schloegl <alois.schloegl@gmail.com>
7501
65a4be217166 prepare next release
schloegl
parents: 4907
diff changeset
28 % This is part of the TSA-toolbox. See also
65a4be217166 prepare next release
schloegl
parents: 4907
diff changeset
29 % http://biosig-consulting.com/matlab/tsa/
4907
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
30 %
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
31 % This program is free software: you can redistribute it and/or modify
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
32 % it under the terms of the GNU General Public License as published by
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
33 % the Free Software Foundation, either version 3 of the License, or
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
34 % (at your option) any later version.
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
35 %
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
36 % This program is distributed in the hope that it will be useful,
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
37 % but WITHOUT ANY WARRANTY; without even the implied warranty of
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
38 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
39 % GNU General Public License for more details.
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
40 %
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
41 % You should have received a copy of the GNU General Public License
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
42 % along with this program. If not, see <http://www.gnu.org/licenses/>.
2414
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
43
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
44
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
45
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
46 if nargin<3, Mode='biased'; end;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
47
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
48 [lr,lc] = size(Z);
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
49
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
50 MISSES = sum(isnan(Z)')';
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
51 if any(MISSES); % missing values
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
52 M = real(~isnan(Z));
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
53 Z(isnan(Z))=0;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
54 end;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
55
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
56 if (nargin == 1)
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
57 KMAX = lc-1;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
58 elseif (KMAX >= lc-1)
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
59 KMAX = lc-1;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
60 end;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
61
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
62 ACF = zeros(lr,KMAX+1);
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
63
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
64 if nargin>3, % for testing, use arg4 for comparing the methods,
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
65
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
66 elseif (KMAX*KMAX > lc*log2(lc)) % & isempty(MISSES);
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
67 Mode2 = 1;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
68 elseif (10*KMAX > lc);
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
69 Mode2 = 3;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
70 else
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
71 Mode2 = 4;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
72 end;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
73
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
74
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
75 %%%%% ESTIMATION of non-normalized ACF %%%%%
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
76
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
77 % the following algorithms gve equivalent results, however, the computational effort is different,
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
78 % depending on lr,lc and KMAX, a different algorithm is most efficient.
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
79 if Mode2==1; % KMAX*KMAX > lc*log(lc); % O(n.logn)+O(K�)
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
80 tmp = fft(Z',2^nextpow2(size(Z,2))*2);
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
81 tmp = ifft(tmp.*conj(tmp));
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
82 ACF = tmp(1:KMAX+1,:)';
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
83 if ~any(any(imag(Z))), ACF=real(ACF); end; % should not be neccessary, unfortunately it is.
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
84 elseif Mode2==3; % (10*KMAX > lc) % O(n*K) % use fast Built-in filter function
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
85 for L = 1:lr,
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
86 acf = filter(Z(L,lc:-1:1),1,Z(L,:));
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
87 ACF(L,:)= acf(lc:-1:lc-KMAX);
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
88 end;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
89 else Mode2==4; % O(n*K)
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
90 for L = 1:lr,
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
91 for K = 0:KMAX,
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
92 ACF(L,K+1) = Z(L,1:lc-K) * Z(L,1+K:lc)';
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
93 end;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
94 end;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
95 end;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
96
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
97
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
98 %%%%% GET number of elements used for estimating ACF - is needed for normalizing ACF %%%%%
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
99
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
100 if any(MISSES),
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
101 % the following algorithms gve equivalent results, however, the computational effort is different,
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
102 % depending on lr,lc and KMAX, a different algorithm is most efficient.
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
103 if Mode2==1; % KMAX*KMAX > lc*log(lc); % O(n.logn)+O(K�)
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
104 tmp = fft(M',2^nextpow2(size(M,2))*2);
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
105 tmp = ifft(tmp.*conj(tmp));
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
106 NN = tmp(1:KMAX+1,:)';
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
107 if ~any(any(imag(M))), NN=real(NN); end; % should not be neccessary, unfortunately it is.
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
108 elseif Mode2==3; % (10*KMAX > lc) % O(n*K) % use fast Built-in filter function
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
109 for L = 1:lr,
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
110 acf = filter(M(L,lc:-1:1),1,M(L,:));
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
111 NN(L,:)= acf(lc:-1:lc-KMAX);
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
112 end;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
113 else Mode2==4; % O(n*K)
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
114 for L = 1:lr,
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
115 for K = 0:KMAX,
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
116 NN(L,K+1) = M(L,1:lc-K) * M(L,1+K:lc)';
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
117 end;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
118 end;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
119 end;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
120 else
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
121 NN = (ones(lr,1)*(lc:-1:lc-KMAX));
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
122 end;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
123
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
124
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
125 if strcmp(Mode,'biased')
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
126 if ~any(MISSES),
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
127 ACF=ACF/lc;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
128 else
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
129 %ACF=ACF./((lc-MISSES)*ones(1,KMAX+1));
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
130 ACF=ACF./max(NN + ones(lr,1)*(0:KMAX),0);
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
131 end;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
132
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
133 elseif strcmp(Mode,'unbiased')
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
134 ACF=ACF./NN;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
135 %if ~any(MISSES),
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
136 % ACF=ACF./(ones(lr,1)*(lc:-1:lc-KMAX));
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
137 %else
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
138 % ACF=ACF./((lc-MISSES)*ones(1,KMAX+1) - ones(lr,1)*(0:KMAX));
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
139 %end;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
140
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
141 elseif strcmp(Mode,'coeff')
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
142 %ACF = ACF ./ ACF(:,ones(1,KMAX+1)) .* ((lc-MISSES)*ones(1,KMAX+1));
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
143 ACF = ACF./NN;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
144 ACF = ACF./(ACF(:,1)*ones(1,size(ACF,2)));
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
145 else
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
146
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
147 end;