]> Creatis software - CreaPhase.git/blob - octave_packages/signal-1.1.3/triang.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / signal-1.1.3 / triang.m
1 ## Copyright (C) 2000-2002 Paul Kienzle <pkienzle@users.sf.net>
2 ##
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
6 ## version.
7 ##
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
11 ## details.
12 ##
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/>.
15
16 ## usage:  w = triang (L)
17 ##
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.
22
23 function w = triang(L)
24   if (nargin != 1)
25     print_usage;
26   elseif (!isscalar(L) || L != fix (L) || L < 1)
27     error("triang: L has to be an integer > 0");
28   endif
29   w = 1 - abs ([-(L-1):2:(L-1)]' / (L+rem(L,2)));
30 endfunction
31
32 %!error triang
33 %!error triang(1,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);
39 %!test
40 %! x = bartlett(5);
41 %! assert (triang(3), x(2:4));
42
43 %!demo
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;");
48 %!
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;");
53 %!
54 %! subplot(223); axis; grid("off");
55 %! title("n odd, triang(n)==bartlett(n+2)");
56 %! n=7;
57 %! plot(0:n+1,bartlett(n+2),"g-*;bartlett;",triang(n),"r-+;triang;");
58 %!
59 %! subplot(224); axis; grid("off");
60 %! title("n even, triang(n)!=bartlett(n+2)");
61 %! n=8;
62 %! plot(0:n+1,bartlett(n+2),"g-*;bartlett;",triang(n),"r-+;triang;");
63 %!
64 %! subplot(111); title("");