# HG changeset patch # User jwe # Date 848447706 0 # Node ID 1dca28c213f0d9cdf0ddefaec17abf505268610a # Parent 6f71af650490f7e32738384c0b8f4e3b80790d4e [project @ 1996-11-19 23:54:48 by jwe] diff --git a/scripts/general/common_size.m b/scripts/general/common_size.m 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 +## 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 diff --git a/scripts/general/diff.m b/scripts/general/diff.m 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 +## 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 diff --git a/scripts/general/nextpow2.m b/scripts/general/nextpow2.m 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 +## 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 diff --git a/scripts/general/shift.m b/scripts/general/shift.m 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 +## 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 diff --git a/scripts/signal/detrend.m b/scripts/signal/detrend.m 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 +## 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