]> Creatis software - CreaPhase.git/blob - octave_packages/control-2.3.52/issample.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / control-2.3.52 / issample.m
1 ## Copyright (C) 1996, 2000, 2002, 2003, 2004, 2005, 2007
2 ##               Auburn University.  All rights reserved.
3 ##
4 ## This program is free software: you can redistribute it and/or modify
5 ## it under the terms of the GNU General Public License as published by
6 ## the Free Software Foundation, either version 3 of the License, or
7 ## (at your option) any later version.
8 ##
9 ## This program is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details.
13 ##
14 ## You should have received a copy of the GNU General Public License
15 ## along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 ## -*- texinfo -*-
18 ## @deftypefn {Function File} {@var{bool} =} issample (@var{ts})
19 ## @deftypefnx {Function File} {@var{bool} =} issample (@var{ts}, @var{flg})
20 ## Return true if @var{ts} is a valid sampling time.
21 ##
22 ## @strong{Inputs}
23 ## @table @var
24 ## @item ts
25 ## Alleged sampling time to be tested.
26 ## @item flg = 1
27 ## Accept real scalars @var{ts} > 0.  Default Value.
28 ## @item flg = 0
29 ## Accept real scalars @var{ts} >= 0.
30 ## @item flg = -1
31 ## Accept real scalars @var{ts} > 0 and @var{ts} == -1.
32 ## @item flg = -10
33 ## Accept real scalars @var{ts} >= 0 and @var{ts} == -1.
34 ## @item flg = -2
35 ## Accept real scalars @var{ts} >= 0, @var{ts} == -1 and @var{ts} == -2.
36 ## @end table
37 ##
38 ## @strong{Outputs}
39 ## @table @var
40 ## @item bool
41 ## True if conditions are met and false otherwise.
42 ## @end table
43 ##
44 ## @end deftypefn
45
46 ## Author: A. S. Hodel <a.s.hodel@eng.auburn.edu>
47 ## Created: July 1995
48
49 ## Adapted-By: Lukas Reichlin <lukas.reichlin@gmail.com>
50 ## Date: September 2009
51 ## Version: 0.3
52
53 function bool = issample (tsam, flg = 1)
54
55   if (nargin < 1 || nargin > 2)
56     print_usage (); 
57   endif
58
59   switch (flg)
60     case 1    # discrete
61       bool = is_real_scalar (tsam) && (tsam > 0);
62     case 0    # continuous or discrete
63       bool = is_real_scalar (tsam) && (tsam >= 0);
64     case -1   # discrete, tsam unspecified
65       bool = is_real_scalar (tsam) && (tsam > 0 || tsam == -1);
66     case -10  # continuous or discrete, tsam unspecified
67       bool = is_real_scalar (tsam) && (tsam >= 0 || tsam == -1);
68     case -2   # accept static gains
69       bool = is_real_scalar (tsam) && (tsam >= 0 || tsam == -1 || tsam == -2);
70     otherwise
71       print_usage ();
72   endswitch
73
74 endfunction
75
76
77 ## flg == 1
78 %!assert (issample (1))
79 %!assert (issample (pi))
80 %!assert (issample (0), false)
81 %!assert (issample (-1), false)
82 %!assert (issample (-1, 1), false)
83 %!assert (issample ("a"), false)
84 %!assert (issample (eye (2)), false)
85 %!assert (issample (2+2i), false)
86
87 ## flg == 0
88 %!assert (issample (1, 0))
89 %!assert (issample (0, 0))
90 %!assert (issample (-1, 0), false)
91 %!assert (issample (pi, 0))
92 %!assert (issample ("b", 0), false)
93 %!assert (issample (rand (3,2), 0), false)
94 %!assert (issample (2+2i, 0), false)
95 %!assert (issample (0+2i, 0), false)
96
97 ## flg == -1
98 %!assert (issample (-1, -1))
99 %!assert (issample (0, -1), false)
100 %!assert (issample (1, -1))
101 %!assert (issample (pi, -1))
102 %!assert (issample (-pi, -1), false)
103 %!assert (issample ("b", -1), false)
104 %!assert (issample (rand (3,2), -1), false)
105 %!assert (issample (-2+2i, -1), false)
106
107 ## errors
108 %!error (issample (-1, "ab"))
109 %!error (issample ())
110 %!error (issample (-1, -1, -1))
111 %!error (issample (1, pi))
112 %!error (issample (5, rand (2,3)))
113 %!error (issample (0, 1+2i))