summaryrefslogtreecommitdiff
path: root/macros/aryule.sci
blob: dd4ac7c6302c1f7a05a1571299a1f3f712296583 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//This function fits an AR (p)-model with Yule-Walker estimates.

//Calling Sequence
//a = aryule (x, p)
//[a, v] = aryule (x, p)
//[a, v, k] = aryule (x, p)

//Parameters
//x: vector of real or complex numbers, length > 2
//p: positive integer value < length(x) - 1
//a: gives the AR coefficients
//v: gives the variance of the white noise,
//k: gives the reflection coefficients to be used in the lattice filter

//Description
//This function fits an AR (p)-model with Yule-Walker estimates.
//The first argument is the data vector which is to be estimated.

//Examples
//aryule([1,2,3,4,5],2)
//ans  =
//    1.  - 0.8140351    0.1192982

//*************************************************************************************
//-------------------version1 (using callOctave / errored)-----------------------------
//*************************************************************************************
//function [a, v, k] = aryule (x, p)
//funcprot(0);
//rhs = argn(2)
//lhs = argn(1)
//
//if(rhs~=2)
//error("Wrong number of input arguments.")
//end
//
//	select(lhs)
//	case 1 then
//	a = callOctave("aryule",x,p)
//	case 2 then
//	[a,v] = callOctave("aryule",x,p)
//	case 3 then
//	[a,v,k] = callOctave("aryule",x,p)
//	end
//
//endfunction

//*************************************************************************************
//-----------------------------version2 (pure scilab code)-----------------------------
//*************************************************************************************

function [a, v, k] = aryule (x, p)

  [nargout,nargin] = argn() ;

  if ( nargin~=2 )
    error('aryule : invalid number of inputs');
  elseif ( ~isvector(x) | length(x)<3 )
    error( 'aryule: arg 1 (x) must be vector of length >2' );
  elseif ( ~isscalar(p) | fix(p)~=p | p > length(x)-2 )
    error( 'aryule: arg 2 (p) must be an integer >0 and <length(x)-1' );
  end

  c = xcorr(x, p+1, 'biased');
  c(1:p+1) = [];     // remove negative autocorrelation lags
  c(1) = real(c(1)); // levinson/toeplitz requires exactly c(1)==conj(c(1))
  if nargout <= 1
    a = levinson(c, p);
  elseif nargout == 2
    [a, v] = levinson(c, p);
  else
    [a, v, k] = levinson(c, p);
  end

endfunction