]> Creatis software - CreaPhase.git/blob - octave_packages/signal-1.1.3/sawtooth.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / signal-1.1.3 / sawtooth.m
1 ## Copyright (C) 2007 Juan Aguado
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 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {[@var{y}] =} sawtooth(@var{t})
18 ## @deftypefnx {Function File} {[@var{y}] =} sawtooth(@var{t},@var{width})
19 ## Generates a sawtooth wave of period @code{2 * pi} with limits @code{+1/-1}
20 ##  for the elements of @var{t}.
21 ##
22 ## @var{width} is a real number between @code{0} and @code{1} which specifies
23 ## the point between @code{0} and @code{2 * pi} where the maximum is. The
24 ## function increases linearly from @code{-1} to @code{1} in  @code{[0, 2 * 
25 ## pi * @var{width}]} interval, and decreases linearly from @code{1} to 
26 ## @code{-1} in the interval @code{[2 * pi * @var{width}, 2 * pi]}.
27 ##
28 ## If @var{width} is 0.5, the function generates a standard triangular wave.
29 ##
30 ## If @var{width} is not specified, it takes a value of 1, which is a standard
31 ## sawtooth function.
32 ## @end deftypefn
33
34 function y = sawtooth (t,width)
35
36   if (nargin < 1 || nargin > 2)
37     print_usage ();
38   endif
39
40   if (nargin == 1)
41     width = 1;
42   else
43     if (width < 0 || width > 1 || ! isreal (width))
44       error ("width must be a real number between 0 and 1.");
45     endif
46   endif
47
48   t = mod (t / (2 * pi), 1);
49   y = zeros (size (t));
50
51   if (width != 0)
52     y (t < width) = 2 * t (t < width) / width - 1;
53   endif
54
55   if (width != 1)
56     y( t >= width) = -2 * (t (t >= width) - width) / (1 - width) + 1;
57   endif
58
59 endfunction