]> Creatis software - CreaPhase.git/blob - octave_packages/tsa-4.2.4/rmle.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / tsa-4.2.4 / rmle.m
1 function [a,VAR,S,a_aux,b_aux,e_aux,MLE,pos] = rmle(arg1,arg2); 
2 % RMLE estimates AR Parameters using the Recursive Maximum Likelihood 
3 % Estimator according to [1]
4
5 % Use: [a,VAR]=rmle(x,p)
6     % Input: 
7                 % x is a column vector of data
8                 % p is the model order
9     % Output:
10                 % a is a vector with the AR parameters of the recursive MLE
11                 % VAR is the excitation white noise variance estimate
12 %
13 % Reference(s):
14 % [1] Kay S.M., Modern Spectral Analysis - Theory and Applications. 
15 %       Prentice Hall, p. 232-233, 1988. 
16 %
17                 
18 %       $Id: rmle.m 9609 2012-02-10 10:18:00Z schloegl $
19 %       Copyright (C) 2004 by  Jose Luis Gutierrez <jlg@gmx.at>
20 %       Grupo GENESIS - UTN - Argentina
21 %
22 %    This program is free software: you can redistribute it and/or modify
23 %    it under the terms of the GNU General Public License as published by
24 %    the Free Software Foundation, either version 3 of the License, or
25 %    (at your option) any later version.
26 %
27 %    This program is distributed in the hope that it will be useful,
28 %    but WITHOUT ANY WARRANTY; without even the implied warranty of
29 %    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30 %    GNU General Public License for more details.
31 %
32 %    You should have received a copy of the GNU General Public License
33 %    along with this program.  If not, see <http://www.gnu.org/licenses/>.
34
35
36 x=arg1*1e-6;
37 p=arg2;
38
39 N=length(x);
40 S=zeros(p+1,p+1);
41 a_aux=zeros(p+1,p);, a_aux(1,:)=1;
42 b_aux=ones(p+1,p);
43 e_aux=zeros(p,1);, p_aux=zeros(p,1);
44 MLE=zeros(3,1);
45 pos=1;
46
47 for i=0:p
48     for j=0:p      
49         for n=0:N-1-i-j
50             S(i+1,j+1)=S(i+1,j+1)+x(n+1+i)*x(n+1+j);
51         end
52     end
53 end
54
55 e0=S(1,1);
56 c1=S(1,2);
57 d1=S(2,2);
58 coef3=1;
59 coef2=((N-2)*c1)/((N-1)*d1);
60 coef1=-(e0+N*d1)/((N-1)*d1);
61 ti=-(N*c1)/((N-1)*d1);
62 raices=roots([coef3 coef2 coef1 ti]);
63 for o=1:3
64     if raices(o)>-1 && raices(o)<1
65         a_aux(2,1)=raices(o);    
66         b_aux(p+1,1)=raices(o);
67     end
68 end
69 e_aux(1,1)=S(1,1)+2*a_aux(2,1)*S(1,2)+(a_aux(2,1)^2)*S(2,2);
70 p_aux(1,1)=e_aux(1,1)/N;
71
72 for k=2:p
73     Ck=S(1:k,2:k+1);
74     Dk=S(2:k+1,2:k+1);
75     ck=a_aux(1:k,k-1)'*Ck*b_aux(p+1:-1:p+2-k,k-1);
76     dk=b_aux(p+1:-1:p+2-k,k-1)'*Dk*b_aux(p+1:-1:p+2-k,k-1);
77     coef3re=1;
78     coef2re=((N-2*k)*ck)/((N-k)*dk);
79     coef1re=-(k*e_aux(k-1,1)+N*dk)/((N-k)*dk);
80     tire=-(N*ck)/((N-k)*dk);
81     raices=roots([coef3re coef2re coef1re tire]);
82     for o=1:3
83         if raices(o,1)>-1 && raices(o,1)<1
84             MLE(o,1)=((1-raices(o)^2)^(k/2))/(((e_aux(k-1)+2*ck*raices(o)+dk*(raices(o)^2))/N)^(N/2));
85         end
86     end
87     [C,I]=max(MLE);
88     k_max=raices(I);
89     for i=1:k-1
90         a_aux(i+1,k)=a_aux(i+1,k-1)+k_max*a_aux(k-i+1,k-1);
91     end
92     a_aux(k+1,k)=k_max;
93     b_aux(p+1-k:p+1,k)=a_aux(1:k+1,k);
94     e_aux(k,1)=e_aux(k-1,1)+2*ck*k_max+dk*k_max^2;
95     p_aux(k,1)=e_aux(k,1)/N;
96 end
97
98 a=a_aux(:,p)';
99 VAR=p_aux(p)*1e12;