]> Creatis software - CreaPhase.git/blob - octave_packages/m/signal/spectral_xdf.m
update packages
[CreaPhase.git] / octave_packages / m / signal / spectral_xdf.m
1 ## Copyright (C) 1995-2012 Friedrich Leisch
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} spectral_xdf (@var{x}, @var{win}, @var{b})
21 ## Return the spectral density estimator given a data vector @var{x},
22 ## window name @var{win}, and bandwidth, @var{b}.
23 ##
24 ## The window name, e.g., @code{"triangle"} or @code{"rectangle"} is
25 ## used to search for a function called @code{@var{win}_sw}.
26 ##
27 ## If @var{win} is omitted, the triangle window is used.  If @var{b} is
28 ## omitted, @code{1 / sqrt (length (@var{x}))} is used.
29 ## @end deftypefn
30
31 ## Author: FL <Friedrich.Leisch@ci.tuwien.ac.at>
32 ## Description: Spectral density estimation
33
34 function retval = spectral_xdf (x, win, b)
35
36   xr = length (x);
37
38   if (columns (x) > 1)
39     x = x';
40   endif
41
42   if (nargin < 3)
43     b = 1 / ceil (sqrt (xr));
44   endif
45
46   if (nargin == 1)
47     w = triangle_sw (xr, b);
48   else
49     win = str2func (cstrcat (win, "_sw"));
50     w = feval (win, xr, b);
51   endif
52
53   x = x - sum (x) / xr;
54
55   retval = (abs (fft (x)) / xr).^2;
56   retval = real (ifft (fft(retval) .* fft(w)));
57
58   retval = [(zeros (xr, 1)), retval];
59   retval(:, 1) = (0 : xr-1)' / xr;
60
61 endfunction
62