1 ## Copyright (C) 2007 Juan Aguado
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/>.
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}.
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]}.
28 ## If @var{width} is 0.5, the function generates a standard triangular wave.
30 ## If @var{width} is not specified, it takes a value of 1, which is a standard
34 function y = sawtooth (t,width)
36 if (nargin < 1 || nargin > 2)
43 if (width < 0 || width > 1 || ! isreal (width))
44 error ("width must be a real number between 0 and 1.");
48 t = mod (t / (2 * pi), 1);
52 y (t < width) = 2 * t (t < width) / width - 1;
56 y( t >= width) = -2 * (t (t >= width) - width) / (1 - width) + 1;