]> Creatis software - CreaPhase.git/blob - octave_packages/tsa-4.2.4/lattice.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / tsa-4.2.4 / lattice.m
1  function [MX,PE,arg3] = lattice(Y,lc,Mode);
2 % Estimates AR(p) model parameter with lattice algorithm (Burg 1968) 
3 % for multiple channels. 
4 % If you have the NaN-tools, LATTICE.M can handle missing values (NaN), 
5 %
6 % [...] = lattice(y [,Pmax [,Mode]]);
7 %
8 % [AR,RC,PE] = lattice(...);
9 % [MX,PE] = lattice(...);
10 %
11 %  INPUT:
12 % y     signal (one per row), can contain missing values (encoded as NaN)
13 % Pmax  max. model order (default size(y,2)-1))
14 % Mode  'BURG' (default) Burg algorithm
15 %       'GEOL' geometric lattice
16 %
17 %  OUTPUT
18 % AR    autoregressive model parameter  
19 % RC    reflection coefficients (= -PARCOR coefficients)
20 % PE    remaining error variance
21 % MX    transformation matrix between ARP and RC (Attention: needs O(p^2) memory)
22 %        AR(:,K) = MX(:, K*(K-1)/2+(1:K)); = MX(:,sum(1:K-1)+(1:K)); 
23 %        RC(:,K) = MX(:,cumsum(1:K));      = MX(:,(1:K).*(2:K+1)/2);
24 %
25 % All input and output parameters are organized in rows, one row 
26 % corresponds to the parameters of one channel
27 %
28 % see also ACOVF ACORF AR2RC RC2AR DURLEV SUMSKIPNAN 
29
30 % REFERENCE(S):
31 %  J.P. Burg, "Maximum Entropy Spectral Analysis" Proc. 37th Meeting of the Society of Exp. Geophysiscists, Oklahoma City, OK 1967
32 %  J.P. Burg, "Maximum Entropy Spectral Analysis" PhD-thesis, Dept. of Geophysics, Stanford University, Stanford, CA. 1975.
33 %  P.J. Brockwell and R. A. Davis "Time Series: Theory and Methods", 2nd ed. Springer, 1991.
34 %  S.   Haykin "Adaptive Filter Theory" 3rd ed. Prentice Hall, 1996.
35 %  M.B. Priestley "Spectral Analysis and Time Series" Academic Press, 1981. 
36 %  W.S. Wei "Time Series Analysis" Addison Wesley, 1990.
37
38 %       $Id: lattice.m 7687 2010-09-08 18:39:23Z schloegl $ 
39 %       Copyright (C) 1996-2002,2008,2010 by Alois Schloegl <a.schloegl@ieee.org>
40 %       This is part of the TSA-toolbox. See also 
41 %       http://biosig-consulting.com/matlab/tsa/
42 %
43 %    This program is free software: you can redistribute it and/or modify
44 %    it under the terms of the GNU General Public License as published by
45 %    the Free Software Foundation, either version 3 of the License, or
46 %    (at your option) any later version.
47 %
48 %    This program is distributed in the hope that it will be useful,
49 %    but WITHOUT ANY WARRANTY; without even the implied warranty of
50 %    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
51 %    GNU General Public License for more details.
52 %
53 %    You should have received a copy of the GNU General Public License
54 %    along with this program.  If not, see <http://www.gnu.org/licenses/>.
55
56
57 if nargin<3, Mode='BURG'; 
58 else Mode=upper(Mode(1:4));end;
59 BURG=~strcmp(Mode,'GEOL');
60
61 % Inititialization
62 [lr,N]=size(Y);
63 if nargin<2, lc=N-1; end;
64 F=Y;
65 B=Y;
66 [DEN,nn] = sumskipnan((Y.*Y),2);
67 PE = [DEN./nn,zeros(lr,lc)];
68
69 if nargout<3         % needs O(p^2) memory 
70         MX = zeros(lr,lc*(lc+1)/2);   
71         idx= 0;
72         
73         % Durbin-Levinson Algorithm
74         for K=1:lc,
75                 [TMP,nn] = sumskipnan(F(:,K+1:N).*B(:,1:N-K),2);
76                 MX(:,idx+K) = TMP./DEN; %Burg
77                 if K>1,   %for compatibility with OCTAVE 2.0.13
78                         MX(:,idx+(1:K-1))=MX(:,(K-2)*(K-1)/2+(1:K-1))-MX(:,(idx+K)*ones(K-1,1)).*MX(:,(K-2)*(K-1)/2+(K-1:-1:1));
79                 end;   
80                 
81                 tmp = F(:,K+1:N) - MX(:,(idx+K)*ones(1,N-K)).*B(:,1:N-K);
82                 B(:,1:N-K) = B(:,1:N-K) - MX(:,(idx+K)*ones(1,N-K)).*F(:,K+1:N);
83                 F(:,K+1:N) = tmp;
84                 
85                 [PE(:,K+1),nn] = sumskipnan([F(:,K+1:N).^2,B(:,1:N-K).^2],2);        
86                 if ~BURG,
87                         [f,nf] = sumskipnan(F(:,K+1:N).^2,2);
88                         [b,nb] = sumskipnan(B(:,1:N-K).^2,2); 
89                         DEN = sqrt(b.*f); 
90                 else
91                         DEN = PE(:,K+1);
92                 end;
93                 idx=idx+K;
94                 PE(:,K+1) = PE(:,K+1)./nn;      % estimate of covariance
95         end;
96 else            % needs O(p) memory 
97         arp=zeros(lr,lc-1);
98         rc=zeros(lr,lc-1);
99         % Durbin-Levinson Algorithm
100         for K=1:lc,
101                 [TMP,nn] = sumskipnan(F(:,K+1:N).*B(:,1:N-K),2);
102                 arp(:,K) = TMP./DEN; %Burg
103                 rc(:,K)  = arp(:,K);
104                 if K>1, % for compatibility with OCTAVE 2.0.13
105                         arp(:,1:K-1) = arp(:,1:K-1) - arp(:,K*ones(K-1,1)).*arp(:,K-1:-1:1);
106                 end;
107                 
108                 tmp = F(:,K+1:N) - rc(:,K*ones(1,N-K)).*B(:,1:N-K);
109                 B(:,1:N-K) = B(:,1:N-K) - rc(:,K*ones(1,N-K)).*F(:,K+1:N);
110                 F(:,K+1:N) = tmp;
111                 
112                 [PE(:,K+1),nn] = sumskipnan([F(:,K+1:N).^2,B(:,1:N-K).^2],2);        
113                 if ~BURG,
114                         [f,nf] = sumskipnan(F(:,K+1:N).^2,2);
115                         [b,nb] = sumskipnan(B(:,1:N-K).^2,2); 
116                         DEN = sqrt(b.*f); 
117                 else
118                         DEN = PE(:,K+1);
119                 end;
120                 PE(:,K+1) = PE(:,K+1)./nn;      % estimate of covariance
121         end;
122 % assign output arguments
123         arg3=PE;
124         PE=rc;
125         MX=arp;
126 end; %if
127