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