]> Creatis software - CreaPhase.git/blob - octave_packages/signal-1.1.3/wkeep.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / signal-1.1.3 / wkeep.m
1 ## Copyright (C) 2008 Sylvain Pelissier <sylvain.pelissier@gmail.com>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {[@var{y}] =} wkeep(@var{x,l,opt})
18 ## Extract the elements of x of size l from the center, the right or the left.
19 ## @end deftypefn
20
21 function y = wkeep(x,l,opt = 'c')
22
23   if (nargin < 2|| nargin > 3); print_usage; end
24   if(isvector(x))
25
26     if(l > length(x))
27       error('l must be or equal the size of x');
28     end
29
30     if(opt=='c')
31       s = (length(x)-l)./2;
32       y = x(1+floor(s):end-ceil(s));
33
34     elseif(opt=='l')
35       y=x(1:l);
36
37     elseif(opt=='r')
38       y = x(end-l+1:end);
39
40     else
41       error('opt must be equal to c, l or r');
42     end
43   else
44     if(length(l) == 2)
45       s1 = (length(x)-l(1))./2;
46       s2 = (length(x)-l(2))./2;
47     else
48       error('For a matrix l must be a 1x2 vector');
49     end
50
51     if(nargin==2)
52       y = x(1+floor(s1):end-ceil(s1),1+floor(s2):end-ceil(s2));
53     else
54       if(length(opt) == 2)
55         firstr=opt(1);
56         firstc=opt(2);
57       else
58         error('For a matrix l must be a 1x2 vector');
59       end
60
61       y=x(firstr:firstr+l(1)-1,firstc:firstc+l(2)-1);
62     end
63
64   end
65 end