annotate scripts/general/nextpow2.m @ 2816:ad4bf2a82b4f

[project @ 1997-03-15 21:08:25 by jwe]
author jwe
date Sat, 15 Mar 1997 21:08:32 +0000
parents 1dca28c213f0
children 6923abb04e16
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
1 ## Copyright (C) 1995, 1996 Kurt Hornik
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
2 ##
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
3 ## This program is free software; you can redistribute it and/or modify
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
4 ## it under the terms of the GNU General Public License as published by
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
5 ## the Free Software Foundation; either version 2, or (at your option)
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
6 ## any later version.
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
7 ##
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
8 ## This program is distributed in the hope that it will be useful, but
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
9 ## WITHOUT ANY WARRANTY; without even the implied warranty of
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
11 ## General Public License for more details.
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
12 ##
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
13 ## You should have received a copy of the GNU General Public License
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
14 ## along with this file. If not, write to the Free Software Foundation,
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
15 ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
16
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
17 ## usage: nextpow2 (x)
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
18 ##
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
19 ## If x is a scalar, returns the first integer n such that
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
20 ## 2^n >= abs (x).
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
21 ##
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
22 ## If x is a vector, return nextpow2 (length (x)).
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
23
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
24 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at>
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
25 ## Created: 7 October 1994
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
26 ## Adapted-By: jwe
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
27
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
28 function n = nextpow2 (x)
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
29
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
30 if (nargin != 1)
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
31 usage ("nextpow2 (x)");
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
32 endif
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
33
2816
ad4bf2a82b4f [project @ 1997-03-15 21:08:25 by jwe]
jwe
parents: 2539
diff changeset
34 if (! (is_scalar (x) || is_vector (x)))
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
35 error ("nextpow2: x must be a scalar or a vector");
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
36 endif
2816
ad4bf2a82b4f [project @ 1997-03-15 21:08:25 by jwe]
jwe
parents: 2539
diff changeset
37
ad4bf2a82b4f [project @ 1997-03-15 21:08:25 by jwe]
jwe
parents: 2539
diff changeset
38 t = length (x);
ad4bf2a82b4f [project @ 1997-03-15 21:08:25 by jwe]
jwe
parents: 2539
diff changeset
39 if (t > 1)
ad4bf2a82b4f [project @ 1997-03-15 21:08:25 by jwe]
jwe
parents: 2539
diff changeset
40 x = t;
ad4bf2a82b4f [project @ 1997-03-15 21:08:25 by jwe]
jwe
parents: 2539
diff changeset
41 endif
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
42
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
43 [f, n] = log2 (abs (x));
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
44 if (f == 0.5)
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
45 n = n - 1;
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
46 endif
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
47
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
48 endfunction