1########################################################################
2##
3## Copyright (C) 1995-2021 The Octave Project Developers
4##
5## See the file COPYRIGHT.md in the top-level directory of this
6## distribution or <https://octave.org/copyright/>.
7##
8## This file is part of Octave.
9##
10## Octave is free software: you can redistribute it and/or modify it
11## under the terms of the GNU General Public License as published by
12## the Free Software Foundation, either version 3 of the License, or
13## (at your option) any later version.
14##
15## Octave is distributed in the hope that it will be useful, but
16## WITHOUT ANY WARRANTY; without even the implied warranty of
17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18## GNU General Public License for more details.
19##
20## You should have received a copy of the GNU General Public License
21## along with Octave; see the file COPYING.  If not, see
22## <https://www.gnu.org/licenses/>.
23##
24########################################################################
25
26## -*- texinfo -*-
27## @deftypefn  {} {} hanning (@var{m})
28## @deftypefnx {} {} hanning (@var{m}, "periodic")
29## @deftypefnx {} {} hanning (@var{m}, "symmetric")
30## Return the filter coefficients of a Hanning window of length @var{m}.
31##
32## If the optional argument @qcode{"periodic"} is given, the periodic form
33## of the window is returned.  This is equivalent to the window of length
34## @var{m}+1 with the last coefficient removed.  The optional argument
35## @qcode{"symmetric"} is equivalent to not specifying a second argument.
36##
37## For a definition of the Hanning window see, e.g.,
38## @nospell{A.V. Oppenheim & R. W. Schafer},
39## @cite{Discrete-Time Signal Processing}.
40## @end deftypefn
41
42function c = hanning (m, opt)
43
44  if (nargin < 1 || nargin > 2)
45    print_usage ();
46  endif
47
48  if (! (isscalar (m) && (m == fix (m)) && (m > 0)))
49    error ("hanning: M must be a positive integer");
50  endif
51
52  N = m - 1;
53  if (nargin == 2)
54    switch (opt)
55      case "periodic"
56        N = m;
57      case "symmetric"
58        ## Default option, same as no option specified.
59      otherwise
60        error ('hanning: window type must be either "periodic" or "symmetric"');
61    endswitch
62  endif
63
64  if (m == 1)
65    c = 1;
66  else
67    m -= 1;
68    c = 0.5 - 0.5 * cos (2 * pi * (0 : m)' / N);
69  endif
70
71endfunction
72
73
74%!assert (hanning (1), 1)
75%!assert (hanning (2), zeros (2,1))
76%!assert (hanning (15), flip (hanning (15)), 5*eps)
77%!assert (hanning (16), flip (hanning (16)), 5*eps)
78%!test
79%! N = 15;
80%! A = hanning (N);
81%! assert (A(ceil (N/2)), 1);
82
83%!assert (hanning (15), hanning (15, "symmetric"))
84%!assert (hanning (16)(1:15), hanning (15, "periodic"))
85%!test
86%! N = 16;
87%! A = hanning (N, "periodic");
88%! assert (A(N/2 + 1), 1);
89
90%!error hanning ()
91%!error hanning (0.5)
92%!error hanning (-1)
93%!error hanning (ones (1,4))
94%!error hanning (1, "invalid")
95