]> Creatis software - CreaPhase.git/blob - octave_packages/signal-1.1.3/aryule.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / signal-1.1.3 / aryule.m
1 ## Copyright (C) 1999 Paul Kienzle <pkienzle@users.sf.net>
2 ## Copyright (C) 2006 Peter Lanspeary
3 ##
4 ## This program is free software; you can redistribute it and/or modify it under
5 ## the terms of the GNU General Public License as published by the Free Software
6 ## Foundation; either version 3 of the License, or (at your option) any later
7 ## version.
8 ##
9 ## This program is distributed in the hope that it will be useful, but WITHOUT
10 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 ## details.
13 ##
14 ## You should have received a copy of the GNU General Public License along with
15 ## this program; if not, see <http://www.gnu.org/licenses/>.
16
17 ## usage:  [a, v, k] = aryule (x, p)
18 ## 
19 ## fits an AR (p)-model with Yule-Walker estimates.
20 ## x = data vector to estimate
21 ## a: AR coefficients
22 ## v: variance of white noise
23 ## k: reflection coeffients for use in lattice filter 
24 ##
25 ## The power spectrum of the resulting filter can be plotted with
26 ## pyulear(x, p), or you can plot it directly with ar_psd(a,v,...).
27 ##
28 ## See also:
29 ## pyulear, power, freqz, impz -- for observing characteristics of the model
30 ## arburg -- for alternative spectral estimators
31 ##
32 ## Example: Use example from arburg, but substitute aryule for arburg.
33 ##
34 ## Note: Orphanidis '85 claims lattice filters are more tolerant of 
35 ## truncation errors, which is why you might want to use them.  However,
36 ## lacking a lattice filter processor, I haven't tested that the lattice
37 ## filter coefficients are reasonable.
38
39 function [a, v, k] = aryule (x, p)
40   if ( nargin~=2 )
41     print_usage;
42   elseif ( ~isvector(x) || length(x)<3 )
43     error( 'aryule: arg 1 (x) must be vector of length >2' );
44   elseif ( ~isscalar(p) || fix(p)~=p || p > length(x)-2 )
45     error( 'aryule: arg 2 (p) must be an integer >0 and <length(x)-1' );
46   endif
47
48   c = xcorr(x, p+1, 'biased');
49   c(1:p+1) = [];     # remove negative autocorrelation lags
50   c(1) = real(c(1)); # levinson/toeplitz requires exactly c(1)==conj(c(1))
51   if nargout <= 1
52     a = levinson(c, p);
53   elseif nargout == 2
54     [a, v] = levinson(c, p);
55   else
56     [a, v, k] = levinson(c, p);
57   endif
58 endfunction
59
60 %!demo
61 %! % use demo('pyulear')