]> Creatis software - CreaPhase.git/blob - octave_packages/signal-1.1.3/pyulear.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / signal-1.1.3 / pyulear.m
1 %% Copyright (C) 2006 Peter V. Lanspeary <pvl@mecheng.adelaide.edu.au>
2 %%
3 %% This program is free software; you can redistribute it and/or modify it under
4 %% the terms of the GNU General Public License as published by the Free Software
5 %% Foundation; either version 3 of the License, or (at your option) any later
6 %% version.
7 %%
8 %% This program is distributed in the hope that it will be useful, but WITHOUT
9 %% ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 %% FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 %% details.
12 %%
13 %% You should have received a copy of the GNU General Public License along with
14 %% this program; if not, see <http://www.gnu.org/licenses/>.
15
16 %% usage:
17 %%    [psd,f_out] = pyulear(x,poles,freq,Fs,range,method,plot_type)
18 %%
19 %% Calculates a Yule-Walker autoregressive (all-pole) model of the data "x"
20 %% and computes the power spectrum of the model.  This is a wrapper for
21 %% functions "aryule" and "ar_psd" which perform the argument checking.
22 %% See "help aryule" and "help ar_psd" for further details.
23 %%
24 %% ARGUMENTS:
25 %%     All but the first two arguments are optional and may be empty.
26 %%   x       %% [vector] sampled data
27 %%
28 %%   poles   %% [integer scalar] required number of poles of the AR model
29 %%
30 %%   freq    %% [real vector] frequencies at which power spectral density
31 %%           %%               is calculated
32 %%           %% [integer scalar] number of uniformly distributed frequency
33 %%           %%          values at which spectral density is calculated.
34 %%           %%          [default=256]
35 %%
36 %%   Fs      %% [real scalar] sampling frequency (Hertz) [default=1]
37 %%
38 %%
39 %% CONTROL-STRING ARGUMENTS -- each of these arguments is a character string.
40 %%   Control-string arguments can be in any order after the other arguments.
41 %%
42 %%
43 %%   range   %% 'half',  'onesided' : frequency range of the spectrum is
44 %%           %%       from zero up to but not including sample_f/2.  Power
45 %%           %%       from negative frequencies is added to the positive
46 %%           %%       side of the spectrum.
47 %%           %% 'whole', 'twosided' : frequency range of the spectrum is
48 %%           %%       -sample_f/2 to sample_f/2, with negative frequencies
49 %%           %%       stored in "wrap around" order after the positive
50 %%           %%       frequencies; e.g. frequencies for a 10-point 'twosided'
51 %%           %%       spectrum are 0 0.1 0.2 0.3 0.4 0.5 -0.4 -0.3 -0.2 -0.1
52 %%           %% 'shift', 'centerdc' : same as 'whole' but with the first half
53 %%           %%       of the spectrum swapped with second half to put the
54 %%           %%       zero-frequency value in the middle. (See "help
55 %%           %%       fftshift". If "freq" is vector, 'shift' is ignored.
56 %%           %% If model coefficients "ar_coeffs" are real, the default
57 %%           %% range is 'half', otherwise default range is 'whole'.
58 %%
59 %%   method  %% 'fft':  use FFT to calculate power spectrum.
60 %%           %% 'poly': calculate power spectrum as a polynomial of 1/z
61 %%           %% N.B. this argument is ignored if the "freq" argument is a
62 %%           %%      vector.  The default is 'poly' unless the "freq"
63 %%           %%      argument is an integer power of 2.
64 %%   
65 %% plot_type %% 'plot', 'semilogx', 'semilogy', 'loglog', 'squared' or 'db':
66 %%           %% specifies the type of plot.  The default is 'plot', which
67 %%           %% means linear-linear axes. 'squared' is the same as 'plot'.
68 %%           %% 'dB' plots "10*log10(psd)".  This argument is ignored and a
69 %%           %% spectrum is not plotted if the caller requires a returned
70 %%           %% value.
71 %%
72 %% RETURNED VALUES:
73 %%     If return values are not required by the caller, the spectrum
74 %%     is plotted and nothing is returned.
75 %%   psd     %% [real vector] power-spectrum estimate 
76 %%   f_out   %% [real vector] frequency values 
77 %%
78 %% HINTS
79 %%   This function is a wrapper for aryule and ar_psd.
80 %%   See "help aryule", "help ar_psd".
81
82 function [psd,f_out]=pyulear(x,poles,varargin)
83   %%
84   if ( nargin<2 )
85     error( 'pburg: need at least 2 args. Use "help pburg"' );
86   end
87   %%
88   [ar_coeffs,residual,k]=aryule(x,poles);
89   if ( nargout==0 )
90     ar_psd(ar_coeffs,residual,varargin{:});
91   elseif ( nargout==1 )
92     psd = ar_psd(ar_coeffs,residual,varargin{:});
93   elseif ( nargout>=2 )
94     [psd,f_out] = ar_psd(ar_coeffs,residual,varargin{:});
95   end
96 end
97
98 %!demo
99 %! fflush(stdout);
100 %! rand('seed',2038014164);
101 %! a = [ 1.0 -1.6216505 1.1102795 -0.4621741 0.2075552 -0.018756746 ];
102 %! signal = detrend(filter(0.70181,a,rand(1,16384)));
103 %! % frequency shift by modulating with exp(j.omega.t) 
104 %! skewed = signal.*exp(2*pi*i*2/25*[1:16384]);
105 %! Fs = 25;
106 %! hold on
107 %! pyulear(signal,3,[],Fs);
108 %! disp( 'Results from this demo should be nearly the same as pburg demo' );
109 %! input('Onesided 3-pole spectrum. Press ENTER', 's' );
110 %! pyulear(signal,4,[],Fs,'whole');
111 %! input('Twosided 4-pole spectrum of same data. Press ENTER', 's' );
112 %! pyulear(signal,5,128,Fs,'shift', 'semilogy');
113 %! input('Twosided, centred zero-frequency, 5-pole. Press ENTER', 's' );
114 %! pyulear(skewed,7,128,Fs,'shift','semilogy');
115 %! input('Complex data, frequency-shifted. Press ENTER', 's' );
116 %! user_freq=[-0.2:0.02:0.2]*Fs;
117 %! pyulear(skewed,7,user_freq,Fs,'semilogy');
118 %! input('User-specified frequency values. Press ENTER', 's' );
119 %! hold off
120 %! clf