Mercurial > hg > octave-avbm
changeset 2539:1dca28c213f0
[project @ 1996-11-19 23:54:48 by jwe]
author | jwe |
---|---|
date | Tue, 19 Nov 1996 23:55:06 +0000 |
parents | 6f71af650490 |
children | c3d634d49ce4 |
files | scripts/general/common_size.m scripts/general/diff.m scripts/general/nextpow2.m scripts/general/shift.m scripts/signal/detrend.m |
diffstat | 5 files changed, 299 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
new file mode 100644 --- /dev/null +++ b/scripts/general/common_size.m @@ -0,0 +1,70 @@ +## Copyright (C) 1995, 1996 Kurt Hornik +## +## 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 2, 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 file. If not, write to the Free Software Foundation, +## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +## usage: [errorcode, y_1, ...] = common_size (x_1, ...) +## +## Determine if all input arguments are either scalar or of common +## size. In this case, errorcode is zero, and y_i is a matrix of the +## common size with all entries equal to x_i if this is a scalar or +## x_i otherwise. +## +## If the inputs cannot be brought to a common size, errorcode is 1, and +## y_i is x_i. +## +## For example, +## +## [errorcode, a, b] = common_size ([1 2; 3 4], 5) +## +## returns errorcode = 0, a = [1 2, 3 4], b = [5 5; 5 5]. +## +## This is useful for implementing functions where arguments can either +## be scalars or of common size. + +## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> +## Created: 15 October 1994 +## Adapted-By: jwe + +function [errorcode, ...] = common_size (...) + + if (nargin < 2) + error ("common_size: only makes sense if nargin >= 2"); + endif + + va_start (); + for k = 1 : nargin + s(k, :) = size (va_arg ()); + endfor + + m = max (s); + if (any (any ((s != 1)') & any ((s != ones (nargin, 1) * m)'))) + errorcode = 1; + va_start (); + for k = 1 : nargin + vr_val (va_arg ()); + endfor + else + errorcode = 0; + va_start (); + for k = 1 : nargin + if (prod (s(k, :)) == 1) + vr_val (va_arg () * ones (m)); + else + vr_val (va_arg ()); + endif + endfor + endif + +endfunction
new file mode 100644 --- /dev/null +++ b/scripts/general/diff.m @@ -0,0 +1,68 @@ +## Copyright (C) 1995, 1996 Kurt Hornik +## +## 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 2, 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 file. If not, write to the Free Software Foundation, +## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +## usage: diff (x [, k]) +## +## If x is a vector of length n, diff (x) is the vector of first +## differences x(2) - x(1), ..., x(n) - x(n-1). +## +## If x is a matrix, diff (x) is the matrix of column differences. +## diff (x, k), where k is a nonnegative integer, returns the k-th +## differences. + +## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> +## Created: 2 February 1995 +## Adapted-By: jwe + +function x = diff (x, k) + + if (nargin == 1) + k = 1; + elseif (nargin == 2) + if (! (is_scalar (k) && k == round (k) && k >= 0)) + error ("diff: k must be a nonnegative integer"); + elseif (k == 0) + return; + endif + else + usage ("diff (x [, k]"); + endif + + if (isstr (x)) + error ("diff: symbolic differentiation not (yet) supported"); + elseif (is_vector (x)) + n = length (x); + if (n <= k) + x = []; + else + for i = 1 : k + x = x (2 : (n - i + 1)) - x (1 : (n - i)); + endfor + endif + elseif (is_matrix (x)) + n = rows (x); + if (n <= k) + x = []; + else + for i = 1 : k + x = x (2 : (n - i + 1), :) - x (1: (n - i), :); + endfor + endif + else + x = []; + endif + +endfunction
new file mode 100644 --- /dev/null +++ b/scripts/general/nextpow2.m @@ -0,0 +1,45 @@ +## Copyright (C) 1995, 1996 Kurt Hornik +## +## 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 2, 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 file. If not, write to the Free Software Foundation, +## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +## usage: nextpow2 (x) +## +## If x is a scalar, returns the first integer n such that +## 2^n >= abs (x). +## +## If x is a vector, return nextpow2 (length (x)). + +## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> +## Created: 7 October 1994 +## Adapted-By: jwe + +function n = nextpow2 (x) + + if (nargin != 1) + usage ("nextpow2 (x)"); + endif + + if (is_vector (x)) + x = length (x); + elseif (! is_scalar (x)) + error ("nextpow2: x must be a scalar or a vector"); + endif + + [f, n] = log2 (abs (x)); + if (f == 0.5) + n = n - 1; + endif + +endfunction
new file mode 100644 --- /dev/null +++ b/scripts/general/shift.m @@ -0,0 +1,60 @@ +## Copyright (C) 1995, 1996 Kurt Hornik +## +## 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 2, 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 file. If not, write to the Free Software Foundation, +## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +## usage: y = shift (x, b) +## +## If x is a vector, perform a circular shift of length b of the +## elements of x. +## +## If x is a matrix, do the same for each column of x. + +## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at> +## Created: 14 September 1994 +## Adapted-By: jwe + +function y = shift (x, b) + + if (nargin != 2) + error ("usage: shift (X, b)"); + endif + + [nr nc] = size (x); + + if (nr == 0 || nc == 0) + error ("shift: x must not be empty"); + elseif (nr == 1) + x = x.'; + nr = nc; + nc = 0; + endif + + if (! (is_scalar (b) && b == round (b))) + error ("shift: b must be an integer"); + endif + + if (b >= 0) + b = rem (b, nr); + y = [x (nr - b + 1 : nr, :); x (1 : nr - b, :)]; + elseif (b < 0) + b = rem (abs (b), nr); + y = [x (b + 1 : nr, :); x (1 : b, :)]; + endif + + if (nc == 0) + y = reshape (y, 1, nr); + endif + +endfunction
new file mode 100644 --- /dev/null +++ b/scripts/signal/detrend.m @@ -0,0 +1,56 @@ +## Copyright (C) 1995, 1996 Kurt Hornik +## +## 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 2, 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 file. If not, write to the Free Software Foundation, +## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +## usage: detrend (x [, p]) +## +## If x is a vector, detrend (x, p) removes the best fit of a +## polynomial of order p from the data x. +## +## If x is a matrix, detrend (x, p) does the same for each column. +## +## If p is not specified, p = 1 is used, i.e., a linear trend is +## removed. + +## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> +## Created: 11 October 1994 +## Adapted-By: jwe + +function y = detrend (x, p) + + if (nargin == 1) + p = 1; + elseif (nargin == 2) + if (! (is_scalar (p) && p == round (p) && p >= 0)) + error ("detrend: p must be a nonnegative integer"); + endif + else + usage ("detrend (x [, p])"); + endif + + [m, n] = size (x); + if (m == 1) + x = x'; + endif + + r = rows (x); + b = ((1 : r)' * ones (1, p + 1)) .^ (ones (r, 1) * (0 : p)); + y = x - b * (b \ x); + + if (m == 1) + y = y'; + endif + +endfunction