1 ## Copyright (C) 1999 Paul Kienzle <pkienzle@users.sf.net>
2 ## Copyright (C) 2006 Peter Lanspeary
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
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
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/>.
17 ## usage: [a, v, k] = aryule (x, p)
19 ## fits an AR (p)-model with Yule-Walker estimates.
20 ## x = data vector to estimate
22 ## v: variance of white noise
23 ## k: reflection coeffients for use in lattice filter
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,...).
29 ## pyulear, power, freqz, impz -- for observing characteristics of the model
30 ## arburg -- for alternative spectral estimators
32 ## Example: Use example from arburg, but substitute aryule for arburg.
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.
39 function [a, v, k] = aryule (x, p)
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' );
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))
54 [a, v] = levinson(c, p);
56 [a, v, k] = levinson(c, p);
61 %! % use demo('pyulear')