[d4a496]: inst / triang.m Maximize Restore History

Download this file

triang.m    71 lines (64 with data), 2.4 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
## Copyright (C) 2000-2002 Paul Kienzle <pkienzle@users.sf.net>
##
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 3 of the License, or (at your option) any later
## version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, see <http://www.gnu.org/licenses/>.
## -*- texinfo -*-
## @deftypefn {Function File} {@var{w} =} triang (@var{m})
##
## Returns the filter coefficients of a triangular window of length @var{m}.
## Unlike the bartlett window, triang does not go to zero at the edges
## of the window. For odd @var{m}, @code{triang(@var{m})} is equal to
## @code{bartlett(@var{m}+2)} except for the zeros at the edges of the window.
## @end deftypefn
function w = triang(m)
if (nargin != 1)
print_usage;
elseif (!isscalar(m) || m != fix (m) || m < 1)
error("triang: M has to be an integer > 0");
endif
w = 1 - abs ([-(m-1):2:(m-1)]' / (m+rem(m,2)));
endfunction
%!error triang
%!error triang(1,2)
%!error triang([1,2]);
%!assert (triang(1), 1)
%!assert (triang(2), [1; 1]/2)
%!assert (triang(3), [1; 2; 1]/2);
%!assert (triang(4), [1; 3; 3; 1]/4);
%!test
%! x = bartlett(5);
%! assert (triang(3), x(2:4));
%!demo
%! subplot(221);
%! n=7; k=(n-1)/2; t=[-k:0.1:k]/(k+1);
%! plot(t,1-abs(t),";continuous;",[-k:k]/(k+1),triang(n),"g*;discrete;");
%! axis([-1, 1, 0, 1.3]); grid("on");
%! title("comparison with continuous for odd n");
%!
%! subplot(222);
%! n=8; k=(n-1)/2; t=[-k:0.1:k]/(k+1/2);
%! plot(t,1+1/n-abs(t),";continuous;",[-k:k]/(k+1/2),triang(n),"g*;discrete;");
%! axis([-1, 1, 0, 1.3]); grid("on");
%! title("note the higher peak for even n");
%!
%! subplot(223);
%! n=7;
%! plot(0:n+1,bartlett(n+2),"g-*;bartlett;",triang(n),"r-+;triang;");
%! axis; grid("off");
%! title("n odd, triang(n)==bartlett(n+2)");
%!
%! subplot(224);
%! n=8;
%! plot(0:n+1,bartlett(n+2),"g-*;bartlett;",triang(n),"r-+;triang;");
%! axis; grid("off");
%! title("n even, triang(n)!=bartlett(n+2)");