X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?p=CreaPhase.git;a=blobdiff_plain;f=octave_packages%2Fmiscellaneous-1.1.0%2Fclip.m;fp=octave_packages%2Fmiscellaneous-1.1.0%2Fclip.m;h=16b76185895c14e1c9093a0617ae22d592434d8e;hp=0000000000000000000000000000000000000000;hb=c880e8788dfc484bf23ce13fa2787f2c6bca4863;hpb=1705066eceaaea976f010f669ce8e972f3734b05 diff --git a/octave_packages/miscellaneous-1.1.0/clip.m b/octave_packages/miscellaneous-1.1.0/clip.m new file mode 100644 index 0000000..16b7618 --- /dev/null +++ b/octave_packages/miscellaneous-1.1.0/clip.m @@ -0,0 +1,66 @@ +## Copyright (C) 1999 Paul Kienzle +## +## 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 3 of the License, 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 program; if not, see . + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{x} =} clip (@var{x}) +## @deftypefnx {Function File} {@var{x} =} clip (@var{x}, @var{hi}) +## @deftypefnx {Function File} {@var{x} =} clip (@var{x}, [@var{lo}, @var{hi}]) +## Clip @var{x} values outside the range.to the value at the boundary of the +## range. +## +## Range boundaries, @var{lo} and @var{hi}, default to 0 and 1 respectively. +## +## @var{x} = clip (@var{x}) +## Clip to range [0, 1] +## +## @var{x} = clip (@var{x}, @var{hi}) +## Clip to range [0, @var{hi}] +## +## @var{x} = clip (@var{x}, [@var{lo}, @var{hi}]) +## Clip to range [@var{lo}, @var{hi}] +## @end deftypefn + +## TODO: more clip modes, such as three level clip(X, [lo, mid, hi]), which +## TODO: sends everything above hi to hi, below lo to lo and between to +## TODO: mid; or infinite peak clipping, which sends everything above mid +## TODO: to hi and below mid to lo. + +function x = clip (x, range = [0, 1]) + + if (nargin < 1 || nargin > 2) + print_usage; + else + if (numel (range) == 2) + ## do nothing, it's good + elseif (numel (range) == 1) + range = [0, range]; + else + print_usage; + endif + endif + + x (x > range (2)) = range (2); + x (x < range (1)) = range (1); + +endfunction + +%!error clip +%!error clip(1,2,3) +%!assert (clip(pi), 1) +%!assert (clip(-pi), 0) +%!assert (clip([-1.5, 0, 1.5], [-1, 1]), [-1, 0, 1]); +%!assert (clip([-1.5, 0, 1.5]', [-1, 1]'), [-1, 0, 1]'); +%!assert (clip([-1.5, 1; 0, 1.5], [-1, 1]), [-1, 1; 0, 1]); +%!assert (isempty(clip([],1)));