]> Creatis software - CreaPhase.git/blob - octave_packages/m/signal/bartlett.m
update packages
[CreaPhase.git] / octave_packages / m / signal / bartlett.m
1 ## Copyright (C) 1995-2012 Andreas Weingessel
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} {} bartlett (@var{m})
21 ## Return the filter coefficients of a Bartlett (triangular) window of
22 ## length @var{m}.
23 ##
24 ## For a definition of the Bartlett window, see e.g., A. V. Oppenheim &
25 ## R. W. Schafer, @cite{Discrete-Time Signal Processing}.
26 ## @end deftypefn
27
28 ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at>
29 ## Description: Coefficients of the Bartlett (triangular) window
30
31 function c = bartlett (m)
32
33   if (nargin != 1)
34     print_usage ();
35   endif
36
37   if (! (isscalar (m) && (m == fix (m)) && (m > 0)))
38     error ("bartlett: M has to be an integer > 0");
39   endif
40
41   if (m == 1)
42     c = 1;
43   else
44     m = m - 1;
45     n = fix (m / 2);
46     c = [2*(0:n)/m, 2-2*(n+1:m)/m]';
47   endif
48
49 endfunction
50
51 %!assert (bartlett (1), 1);
52 %!assert (bartlett (2), zeros (2,1));
53 %!assert (bartlett (16), fliplr (bartlett (16)));
54 %!assert (bartlett (15), fliplr (bartlett (15)));
55 %!test
56 %! N = 9;
57 %! A = bartlett (N);
58 %! assert (A (ceil (N/2)), 1);
59
60 %!error bartlett ();
61 %!error bartlett (0.5);
62 %!error bartlett (-1);
63 %!error bartlett (ones(1,4));