1## Copyright (C) 2007 Muthiah Annamalai <muthiah.annamalai@uta.edu>
2##
3## This program is free software: you can redistribute it and/or modify
4## it under the terms of the GNU General Public License as published by
5## the Free Software Foundation, either version 3 of the License, or
6## (at your option) any later version.
7##
8## This program is distributed in the hope that it will be useful,
9## but WITHOUT ANY WARRANTY; without even the implied warranty of
10## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11## GNU General Public License for more details.
12##
13## You should have received a copy of the GNU General Public License
14## along with this program; see the file COPYING. If not, see
15## <https://www.gnu.org/licenses/>.
16
17## -*- texinfo -*-
18## @deftypefn  {Function File} {} blackmannuttall (@var{m})
19## @deftypefnx {Function File} {} blackmannuttall (@var{m}, "periodic")
20## @deftypefnx {Function File} {} blackmannuttall (@var{m}, "symmetric")
21## Return the filter coefficients of a Blackman-Nuttall window of length
22## @var{m}.
23##
24## If the optional argument @code{"periodic"} is given, the periodic form
25## of the window is returned.  This is equivalent to the window of length
26## @var{m}+1 with the last coefficient removed.  The optional argument
27## @code{"symmetric"} is equivalent to not specifying a second argument.
28##
29## @seealso{nuttallwin, kaiser}
30## @end deftypefn
31
32function w = blackmannuttall (m, opt)
33
34  if (nargin < 1 || nargin > 2)
35    print_usage ();
36  elseif (! (isscalar (m) && (m == fix (m)) && (m > 0)))
37    error ("blackmannuttall: M must be a positive integer");
38  endif
39
40  N = m - 1;
41  if (nargin == 2)
42    switch (opt)
43      case "periodic"
44        N = m;
45      case "symmetric"
46        N = m - 1;
47      otherwise
48        error ("blackmannuttall: window type must be either \"periodic\" or \"symmetric\"");
49    endswitch
50  endif
51
52  if (m == 1)
53    w = 1;
54  else
55    a0 = 0.3635819;
56    a1 = 0.4891775;
57    a2 = 0.1365995;
58    a3 = 0.0106411;
59    n = [0:m-1]';
60    w = a0 - a1.*cos(2.*pi.*n./N) + a2.*cos(4.*pi.*n./N) - a3.*cos(6.*pi.*n./N);
61  endif
62
63endfunction
64
65%!assert (blackmannuttall (1), 1)
66%!assert (blackmannuttall (2), 0.0003628 * ones (2, 1), eps)
67%!assert (blackmannuttall (15), flipud (blackmannuttall (15)), 10*eps);
68%!assert (blackmannuttall (16), flipud (blackmannuttall (16)), 10*eps);
69%!assert (blackmannuttall (15), blackmannuttall (15, "symmetric"));
70%!assert (blackmannuttall (16)(1:15), blackmannuttall (15, "periodic"));
71
72%% Test input validation
73%!error blackmannuttall ()
74%!error blackmannuttall (0.5)
75%!error blackmannuttall (-1)
76%!error blackmannuttall (ones (1, 4))
77%!error blackmannuttall (1, 2)
78%!error blackmannuttall (1, "invalid")
79
80