comparison scripts/signal/spectral_xdf.m @ 17230:86c45a04990f

spectral_xdf.m: Add input validation, add %!tests, and fix docstring * spectral_xdf.m: Add input validation, add %!tests, and fix docstring.
author Mike Miller <mtmiller@ieee.org>
date Tue, 13 Aug 2013 08:00:33 -0400
parents 2736bc7bf8d9
children bc924baa2c4e
comparison
equal deleted inserted replaced
17229:2736bc7bf8d9 17230:86c45a04990f
15 ## You should have received a copy of the GNU General Public License 15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see 16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>. 17 ## <http://www.gnu.org/licenses/>.
18 18
19 ## -*- texinfo -*- 19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} spectral_xdf (@var{x}, @var{win}, @var{b}) 20 ## @deftypefn {Function File} {} spectral_xdf (@var{x})
21 ## @deftypefnx {Function File} {} spectral_xdf (@var{x}, @var{win})
22 ## @deftypefnx {Function File} {} spectral_xdf (@var{x}, @var{win}, @var{b})
21 ## Return the spectral density estimator given a data vector @var{x}, 23 ## Return the spectral density estimator given a data vector @var{x},
22 ## window name @var{win}, and bandwidth, @var{b}. 24 ## window name @var{win}, and bandwidth, @var{b}.
23 ## 25 ##
24 ## The window name, e.g., @code{"triangle"} or @code{"rectangle"} is 26 ## The window name, e.g., @code{"triangle"} or @code{"rectangle"} is
25 ## used to search for a function called @code{@var{win}_sw}. 27 ## used to search for a function called @code{@var{win}_sw}.
26 ## 28 ##
27 ## If @var{win} is omitted, the triangle window is used. If @var{b} is 29 ## If @var{win} is omitted, the triangle window is used. If @var{b} is
28 ## omitted, @code{1 / sqrt (length (@var{x}))} is used. 30 ## omitted, @code{1 / sqrt (length (@var{x}))} is used.
31 ## @seealso{spectral_adf}
29 ## @end deftypefn 32 ## @end deftypefn
30 33
31 ## Author: FL <Friedrich.Leisch@ci.tuwien.ac.at> 34 ## Author: FL <Friedrich.Leisch@ci.tuwien.ac.at>
32 ## Description: Spectral density estimation 35 ## Description: Spectral density estimation
33 36
34 function retval = spectral_xdf (x, win, b) 37 function retval = spectral_xdf (x, win, b)
38
39 if (nargin < 1 || nargin > 3)
40 print_usage ();
41 endif
35 42
36 xr = length (x); 43 xr = length (x);
37 44
38 if (columns (x) > 1) 45 if (columns (x) > 1)
39 x = x'; 46 x = x';
43 b = 1 / ceil (sqrt (xr)); 50 b = 1 / ceil (sqrt (xr));
44 endif 51 endif
45 52
46 if (nargin == 1) 53 if (nargin == 1)
47 w = triangle_sw (xr, b); 54 w = triangle_sw (xr, b);
55 elseif (! ischar (win))
56 error ("spectral_xdf: WIN must be a string");
48 else 57 else
49 win = str2func ([win "_sw"]); 58 win = str2func ([win "_sw"]);
50 w = feval (win, xr, b); 59 w = feval (win, xr, b);
51 endif 60 endif
52 61
58 retval = [(zeros (xr, 1)), retval]; 67 retval = [(zeros (xr, 1)), retval];
59 retval(:, 1) = (0 : xr-1)' / xr; 68 retval(:, 1) = (0 : xr-1)' / xr;
60 69
61 endfunction 70 endfunction
62 71
72 %% Test input validation
73 %!error spectral_xdf ();
74 %!error spectral_xdf (1, 2, 3, 4);
75 %!error spectral_xdf (1, 2);
76 %!error spectral_xdf (1, "invalid");