1## Author: Paul Kienzle <pkienzle@users.sf.net> (2004)
2## This program is granted to the public domain.
3
4## -*- texinfo -*-
5## @deftypefn  {Function File} {} flattopwin (@var{m})
6## @deftypefnx {Function File} {} flattopwin (@var{m}, "periodic")
7## @deftypefnx {Function File} {} flattopwin (@var{m}, "symmetric")
8##
9## Return the filter coefficients of a Flat Top window of length @var{m}.
10## The Flat Top window is defined by the function f(w):
11##
12## @example
13## @group
14##   f(w) = 1 - 1.93 cos(2 pi w) + 1.29 cos(4 pi w)
15##            - 0.388 cos(6 pi w) + 0.0322cos(8 pi w)
16## @end group
17## @end example
18##
19## where w = i/(m-1) for i=0:m-1 for a symmetric window, or
20## w = i/m for i=0:m-1 for a periodic window.  The default
21## is symmetric.  The returned window is normalized to a peak
22## of 1 at w = 0.5.
23##
24## This window has low pass-band ripple, but high bandwidth.
25##
26## According to [1]:
27##
28##    The main use for the Flat Top window is for calibration, due
29##    to its negligible amplitude errors.
30##
31## [1] Gade, S; Herlufsen, H; (1987) "Use of weighting functions in DFT/FFT
32## analysis (Part I)", Bruel & Kjaer Technical Review No.3.
33## @end deftypefn
34
35function w = flattopwin (m, opt)
36
37  if (nargin < 1 || nargin > 2)
38    print_usage ();
39  elseif (! (isscalar (m) && (m == fix (m)) && (m > 0)))
40    error ("flattopwin: M must be a positive integer");
41  endif
42
43  N = m - 1;
44  if (nargin == 2)
45    switch (opt)
46      case "periodic"
47        N = m;
48      case "symmetric"
49        N = m - 1;
50      otherwise
51        error ("flattopwin: window type must be either \"periodic\" or \"symmetric\"");
52    endswitch
53  endif
54
55  if (m == 1)
56    w = 1;
57  else
58    x = 2*pi*[0:m-1]'/N;
59    w = (1-1.93*cos(x)+1.29*cos(2*x)-0.388*cos(3*x)+0.0322*cos(4*x))/4.6402;
60  endif
61
62endfunction
63
64%!assert (flattopwin (1), 1);
65%!assert (flattopwin (2), 0.0042 / 4.6402 * ones (2, 1), eps);
66%!assert (flattopwin (15), flipud (flattopwin (15)), 10*eps);
67%!assert (flattopwin (16), flipud (flattopwin (16)), 10*eps);
68
69%!assert (flattopwin (15), flattopwin (15, "symmetric"));
70%!assert (flattopwin (16)(1:15), flattopwin (15, "periodic"));
71
72%% Test input validation
73%!error flattopwin ()
74%!error flattopwin (0.5)
75%!error flattopwin (-1)
76%!error flattopwin (ones (1, 4))
77%!error flattopwin (1, 2)
78%!error flattopwin (1, 2, 3)
79%!error flattopwin (1, "invalid")
80
81