]> Creatis software - CreaPhase.git/blob - octave_packages/m/signal/blackman.m
update packages
[CreaPhase.git] / octave_packages / m / signal / blackman.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} {} blackman (@var{m})
21 ## Return the filter coefficients of a Blackman window of length @var{m}.
22 ##
23 ## For a definition of the Blackman 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 Blackman window
29
30 function c = blackman (m)
31
32   if (nargin != 1)
33     print_usage ();
34   endif
35
36   if (! (isscalar (m) && (m == fix (m)) && (m > 0)))
37     error ("blackman: M has to be an integer > 0");
38   endif
39
40   if (m == 1)
41     c = 1;
42   else
43     m = m - 1;
44     k = (0 : m)' / m;
45     c = 0.42 - 0.5 * cos (2 * pi * k) + 0.08 * cos (4 * pi * k);
46   endif
47
48 endfunction
49
50 %!assert (blackman (1), 1);
51 %!assert (blackman (2), zeros(2,1), 1e-6);
52 %!assert (blackman (16), fliplr (blackman (16)));
53 %!assert (blackman (15), fliplr (blackman (15)));
54 %!test
55 %! N = 9;
56 %! A = blackman (N);
57 %! assert (A (ceil (N/2)), 1, 1e-6);
58 %! assert ([A(1), A(length (A))], zeros (1, 2), 1e-6);
59
60 %!error blackman ();
61 %!error blackman (0.5);
62 %!error blackman (-1);
63 %!error blackman (ones(1,4));