File: sawtooth.m

package info (click to toggle)
octave-signal 1.4.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,280 kB
  • sloc: cpp: 2,395; ansic: 672; python: 426; makefile: 173; xml: 24
file content (60 lines) | stat: -rw-r--r-- 2,058 bytes parent folder | download | duplicates (5)
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
## Copyright (C) 2007 Juan Aguado
##
## 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; see the file COPYING. If not, see
## <https://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn  {Function File} {@var{y} =} sawtooth (@var{t})
## @deftypefnx {Function File} {@var{y} =} sawtooth (@var{t}, @var{width})
## Generates a sawtooth wave of period @code{2 * pi} with limits @code{+1/-1}
##  for the elements of @var{t}.
##
## @var{width} is a real number between @code{0} and @code{1} which specifies
## the point between @code{0} and @code{2 * pi} where the maximum is. The
## function increases linearly from @code{-1} to @code{1} in  @code{[0, 2 *
## pi * @var{width}]} interval, and decreases linearly from @code{1} to
## @code{-1} in the interval @code{[2 * pi * @var{width}, 2 * pi]}.
##
## If @var{width} is 0.5, the function generates a standard triangular wave.
##
## If @var{width} is not specified, it takes a value of 1, which is a standard
## sawtooth function.
## @end deftypefn

function y = sawtooth (t,width)

  if (nargin < 1 || nargin > 2)
    print_usage ();
  endif

  if (nargin == 1)
    width = 1;
  else
    if (width < 0 || width > 1 || ! isreal (width))
      error ("width must be a real number between 0 and 1.");
    endif
  endif

  t = mod (t / (2 * pi), 1);
  y = zeros (size (t));

  if (width != 0)
    y (t < width) = 2 * t (t < width) / width - 1;
  endif

  if (width != 1)
    y( t >= width) = -2 * (t (t >= width) - width) / (1 - width) + 1;
  endif

endfunction