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