1 ## Copyright (C) 2009, 2010, 2011 Lukas F. Reichlin
3 ## This file is part of LTI Syncope.
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.
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.
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/>.
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.
26 ## @item sigtype = "sin"
28 ## @item sigtype = "cos"
30 ## @item sigtype = "square"
32 ## @item sigtype = "pulse"
35 ## Duration of one period in seconds.
37 ## Optional duration of the signal in seconds. Default duration is 5 periods.
39 ## Optional sampling time in seconds. Default spacing is tau/64.
45 ## Vector of signal values.
47 ## Time vector of the signal.
53 ## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
54 ## Created: August 2009
57 function [u, t] = gensig (sigtype, tau, tfinal, tsam)
59 if (nargin < 2 || nargin > 4)
63 if (! ischar (sigtype))
64 error ("gensig: first argument must be a string");
68 error ("gensig: second argument is not a valid period");
73 elseif (! issample (tfinal))
74 error ("gensig: third argument is not a valid final time");
79 elseif (! issample (tsam))
80 error ("gensig: fourth argument is not a valid sampling time");
83 t = reshape (0 : tsam : tfinal, [], 1);
85 switch (lower (sigtype(1:2)))
87 u = sin (2*pi/tau * t);
89 u = cos (2*pi/tau * t);
91 u = double (rem (t, tau) >= tau/2);
93 u = double (rem (t, tau) < (1 - 1000*eps) * tsam);
95 error ("gensig: '%s' is an invalid signal type", sigtype);