1## Copyright (C) 2009-2016   Lukas F. Reichlin
2##
3## This file is part of LTI Syncope.
4##
5## LTI Syncope is free software: you can redistribute it and/or modify
6## it under the terms of the GNU General Public License as published by
7## the Free Software Foundation, either version 3 of the License, or
8## (at your option) any later version.
9##
10## LTI Syncope is distributed in the hope that it will be useful,
11## but WITHOUT ANY WARRANTY; without even the implied warranty of
12## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13## GNU General Public License for more details.
14##
15## You should have received a copy of the GNU General Public License
16## along with LTI Syncope.  If not, see <http://www.gnu.org/licenses/>.
17
18## -*- texinfo -*-
19## @deftypefn{Function File} {[@var{u}, @var{t}] =} gensig (@var{sigtype}, @var{tau})
20## @deftypefnx{Function File} {[@var{u}, @var{t}] =} gensig (@var{sigtype}, @var{tau}, @var{tfinal})
21## @deftypefnx{Function File} {[@var{u}, @var{t}] =} gensig (@var{sigtype}, @var{tau}, @var{tfinal}, @var{tsam})
22## Generate periodic signal.  Useful in combination with lsim.
23##
24## @strong{Inputs}
25## @table @var
26## @item sigtype = "sin"
27## Sine wave.
28## @item sigtype = "cos"
29## Cosine wave.
30## @item sigtype = "square"
31## Square wave.
32## @item sigtype = "pulse"
33## Periodic pulse.
34## @item tau
35## Duration of one period in seconds.
36## @item tfinal
37## Optional duration of the signal in seconds.  Default duration is 5 periods.
38## @item tsam
39## Optional sampling time in seconds.  Default spacing is tau/64.
40## @end table
41##
42## @strong{Outputs}
43## @table @var
44## @item u
45## Vector of signal values.
46## @item t
47## Time vector of the signal.
48## @end table
49##
50## @seealso{lsim}
51## @end deftypefn
52
53## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
54## Created: August 2009
55## Version: 0.4
56
57function [u, t] = gensig (sigtype, tau, tfinal, tsam)
58
59  if (nargin < 2 || nargin > 4)
60    print_usage ();
61  endif
62
63  if (! ischar (sigtype))
64    error ("gensig: first argument must be a string");
65  endif
66
67  if (! issample (tau))
68    error ("gensig: second argument is not a valid period");
69  endif
70
71  if (nargin < 3)
72    tfinal = 5 * tau;
73  elseif (! issample (tfinal))
74    error ("gensig: third argument is not a valid final time");
75  endif
76
77  if (nargin < 4)
78    tsam = tau / 64;
79  elseif (! issample (tsam))
80    error ("gensig: fourth argument is not a valid sampling time");
81  endif
82
83  t = reshape (0 : tsam : tfinal, [], 1);
84
85  switch (lower (sigtype(1:2)))
86    case "si"
87      u = sin (2*pi/tau * t);
88    case "co"
89      u = cos (2*pi/tau * t);
90    case "sq"
91      u = double (rem (t, tau) >= tau/2);
92    case "pu"
93      u = double (rem (t, tau) < (1 - 1000*eps) * tsam);
94    otherwise
95      error ("gensig: '%s' is an invalid signal type", sigtype);
96  endswitch
97
98endfunction
99