comparison scripts/signal/hamming.m @ 19826:26fb4bfa4193

blackman, hamming, hanning: Add periodic window option (bug #43305) * blackman.m, hamming.m, hanning.m: Add Matlab compatible option to return the periodic form of the window. Add %!tests for new behavior.
author Mike Miller <mtmiller@ieee.org>
date Sun, 22 Feb 2015 17:39:29 -0500
parents 4197fc428c7d
children d209fbae38ae
comparison
equal deleted inserted replaced
19825:bc3e6e96da81 19826:26fb4bfa4193
15 ## You should have received a copy of the GNU General Public License 15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see 16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>. 17 ## <http://www.gnu.org/licenses/>.
18 18
19 ## -*- texinfo -*- 19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} hamming (@var{m}) 20 ## @deftypefn {Function File} {} hamming (@var{m})
21 ## @deftypefnx {Function File} {} hamming (@var{m}, "periodic")
22 ## @deftypefnx {Function File} {} hamming (@var{m}, "symmetric")
21 ## Return the filter coefficients of a Hamming window of length @var{m}. 23 ## Return the filter coefficients of a Hamming window of length @var{m}.
24 ##
25 ## If the optional argument @qcode{"periodic"} is given, the periodic form
26 ## of the window is returned. This is equivalent to the window of length
27 ## @var{m}+1 with the last coefficient removed. The optional argument
28 ## @qcode{"symmetric"} is equivalent to not specifying a second argument.
22 ## 29 ##
23 ## For a definition of the Hamming window, see e.g., 30 ## For a definition of the Hamming window, see e.g.,
24 ## @nospell{A.V. Oppenheim & R. W. Schafer}, 31 ## @nospell{A.V. Oppenheim & R. W. Schafer},
25 ## @cite{Discrete-Time Signal Processing}. 32 ## @cite{Discrete-Time Signal Processing}.
26 ## @end deftypefn 33 ## @end deftypefn
27 34
28 ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at> 35 ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at>
29 ## Description: Coefficients of the Hamming window 36 ## Description: Coefficients of the Hamming window
30 37
31 function c = hamming (m) 38 function c = hamming (m, opt)
32 39
33 if (nargin != 1) 40 if (nargin < 1 || nargin > 2)
34 print_usage (); 41 print_usage ();
35 endif 42 endif
36 43
37 if (! (isscalar (m) && (m == fix (m)) && (m > 0))) 44 if (! (isscalar (m) && (m == fix (m)) && (m > 0)))
38 error ("hamming: M has to be an integer > 0"); 45 error ("hamming: M must be a positive integer");
46 endif
47
48 periodic = false;
49 if (nargin == 2)
50 switch (opt)
51 case "periodic"
52 periodic = true;
53 case "symmetric"
54 ## Default option, same as no option specified.
55 otherwise
56 error ('hamming: window type must be either "periodic" or "symmetric"');
57 endswitch
39 endif 58 endif
40 59
41 if (m == 1) 60 if (m == 1)
42 c = 1; 61 c = 1;
43 else 62 else
44 m = m - 1; 63 if (! periodic)
64 m = m - 1;
65 endif
45 c = 0.54 - 0.46 * cos (2 * pi * (0:m)' / m); 66 c = 0.54 - 0.46 * cos (2 * pi * (0:m)' / m);
67 endif
68
69 if (periodic)
70 c = c(1:end-1);
46 endif 71 endif
47 72
48 endfunction 73 endfunction
49 74
50 75
55 %!test 80 %!test
56 %! N = 15; 81 %! N = 15;
57 %! A = hamming (N); 82 %! A = hamming (N);
58 %! assert (A (ceil (N/2)), 1); 83 %! assert (A (ceil (N/2)), 1);
59 84
85 %!assert (hamming (15), hamming (15, "symmetric"));
86 %!assert (hamming (16)(1:15), hamming (15, "periodic"));
87 %!test
88 %! N = 16;
89 %! A = hamming (N, "periodic");
90 %! assert (A (N/2 + 1), 1);
91
60 %!error hamming () 92 %!error hamming ()
61 %!error hamming (0.5) 93 %!error hamming (0.5)
62 %!error hamming (-1) 94 %!error hamming (-1)
63 %!error hamming (ones (1,4)) 95 %!error hamming (ones (1,4))
96 %!error hamming (1, "invalid");
64 97