/usr/share/octave/packages/signal-1.3.2/buffer.m is in octave-signal 1.3.2-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | ## Copyright (C) 2008 David Bateman <adb014@gmail.com>
##
## 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 <http://www.gnu.org/licenses/>.
## -*- texinfo -*-
## @deftypefn {Function File} {@var{y} =} buffer (@var{x}, @var{n}, @var{p}, @var{opt})
## @deftypefnx {Function File} {[@var{y}, @var{z}, @var{opt}] =} buffer (@dots{})
## Buffer a signal into a data frame. The arguments to @code{buffer} are
##
## @table @asis
## @item @var{x}
## The data to be buffered.
##
## @item @var{n}
## The number of rows in the produced data buffer. This is an positive
## integer value and must be supplied.
##
## @item @var{p}
## An integer less than @var{n} that specifies the under- or overlap
## between column in the data frame. The default value of @var{p} is 0.
##
## @item @var{opt}
## In the case of an overlap, @var{opt} can be either a vector of length
## @var{p} or the string 'nodelay'. If @var{opt} is a vector, then the
## first @var{p} entries in @var{y} will be filled with these values. If
## @var{opt} is the string 'nodelay', then the first value of @var{y}
## corresponds to the first value of @var{x}.
##
## In the can of an underlap, @var{opt} must be an integer between 0 and
## @code{-@var{p}}. The represents the initial underlap of the first
## column of @var{y}.
##
## The default value for @var{opt} the vector @code{zeros (1, @var{p})}
## in the case of an overlap, or 0 otherwise.
## @end table
##
## In the case of a single output argument, @var{y} will be padded with
## zeros to fill the missing values in the data frame. With two output
## arguments @var{z} is the remaining data that has not been used in the
## current data frame.
##
## Likewise, the output @var{opt} is the overlap, or underlap that might
## be used for a future call to @code{code} to allow continuous buffering.
## @end deftypefn
function [y, z, opt] = buffer (x, n, p, opt)
if (nargin < 2 || nargin > 4)
print_usage ();
endif
if (!isscalar (n) || n != floor (n))
error ("buffer: n must be an integer");
endif
if (nargin < 3)
p = 0;
elseif (!isscalar (p) || p != floor (p) || p >= n)
error ("buffer: p must be an integer less than n");
endif
if (nargin < 4)
if (p < 0)
opt = 0;
else
opt = zeros (1, p);
endif
endif
if (rows (x) == 1)
isrowvec = true;
else
isrowvec = false;
endif
if (p < 0)
if (isscalar (opt) && opt == floor (opt) && opt >= 0 && opt <= -p)
lopt = opt;
else
error ("buffer: expecting fourth argument to be and integer between 0 and -p");
endif
else
lopt = 0;
endif
x = x (:);
l = length (x);
m = ceil ((l - lopt) / (n - p));
y = zeros (n - p, m);
y (1 : l - lopt) = x (lopt + 1 : end);
if (p < 0)
y (end + p + 1 : end, :) = [];
elseif (p > 0)
if (ischar (opt))
if (strcmp (opt, "nodelay"))
y = [y ; zeros(p, m)];
if (p > n / 2)
is = n - p + 1;
in = n - p;
ie = is + in - 1;
off = 1;
while (in > 0)
y (is : ie, 1 : end - off) = y (1 : in, 1 + off : end);
off++;
is = ie + 1;
ie = ie + in;
if (ie > n)
ie = n;
endif
in = ie - is + 1;
endwhile
[i, j] = ind2sub([n-p, m], l);
if (all ([i, j] == [n-p, m]))
off --;
endif
y (:, end - off + 2 : end) = [];
else
y (end - p + 1 : end, 1 : end - 1) = y (1 : p, 2 : end);
if (sub2ind([n-p, m], p, m) >= l)
y (:, end) = [];
endif
endif
else
error ("buffer: unexpected string argument");
endif
elseif (isvector (opt))
if (length (opt) == p)
lopt = p;
y = [zeros(p, m); y];
in = p;
off = 1;
while (in > 0)
y (1 : in, off) = opt(off:end);
off++;
in = in - n + p;
endwhile
if (p > n / 2)
in = n - p;
ie = p;
is = p - in + 1;
off = 1;
while (ie > 0)
y (is : ie, 1 + off : end) = ...
y (end - in + 1 : end, 1 : end - off);
off++;
ie = is - 1;
is = is - in;
if (is < 1)
is = 1;
endif
in = ie - is + 1;
endwhile
else
y (1 : p, 2 : end) = y (end - p + 1 : end, 1 : end - 1);
endif
else
error ("buffer: opt vector must be of length p");
endif
else
error ("buffer: unrecognized fourth argument");
endif
endif
if (nargout > 1)
if (p >= 0)
[i, j] = ind2sub (size(y), l + lopt + p * (size (y, 2) - 1));
if (any ([i, j] != size (y)))
z = y (1 + p : i, end);
y (:, end) = [];
else
z = zeros (0, 1);
endif
else
[i, j] = ind2sub (size (y) + [-p, 0], l - lopt);
if (i < size (y, 1))
z = y (1: i, end);
y (:, end) = [];
else
z = zeros (0, 1);
endif
endif
if (isrowvec)
z = z.';
endif
if (p < 0)
opt = max(0, size (y, 2) * (n - p) + opt - l);
elseif (p > 0)
opt = y(end-p+1:end)(:);
else
opt = [];
endif
endif
endfunction
%!error (buffer(1:10, 4.1))
%!assert (buffer(1:10, 4), reshape([1:10,0,0],[4,3]))
%!assert (buffer(1:10, 4, 1), reshape([0:3,3:6,6:9,9,10,0,0],[4,4]))
%!assert (buffer(1:10, 4, 2), reshape ([0,0:2,1:4,3:6,5:8,7:10],[4,5]))
%!assert (buffer(1:10, 4, 3), [0,0,0:7;0,0:8;0:9;1:10])
%!error (buffer(1:10, 4, 3.1))
%!error (buffer(1:10, 4, 4))
%!assert (buffer(1:10, 4, -1), reshape([1:4,6:9],[4,2]))
%!assert (buffer(1:10, 4, -2), reshape([1:4,7:10],[4,2]))
%!assert (buffer(1:10, 4, -3), reshape([1:4,8:10,0],[4,2]))
%!assert (buffer(1:10, 4, 1, 11), reshape([11,1:3,3:6,6:9,9,10,0,0],[4,4]))
%!error (buffer(1:10, 4, 1, [10,11]))
%!assert (buffer(1:10, 4, 1, 'nodelay'), reshape([1:4,4:7,7:10],[4,3]))
%!error (buffer(1:10, 4, 1, 'badstring'))
%!assert (buffer(1:10, 4, 2,'nodelay'), reshape ([1:4,3:6,5:8,7:10],[4,4]))
%!assert (buffer(1:10, 4, 3, [11,12,13]),[11,12,13,1:7;12,13,1:8;13,1:9;1:10])
%!assert (buffer(1:10, 4, 3, 'nodelay'),[1:8;2:9;3:10;4:10,0])
%!assert (buffer(1:11,4,-2,1),reshape([2:5,8:11],4,2))
%!test
%! [y, z] = buffer(1:12,4);
%! assert (y, reshape(1:12,4,3));
%! assert (z, zeros (1,0));
%!test
%! [y, z] = buffer(1:11,4);
%! assert (y, reshape(1:8,4,2));
%! assert (z, [9, 10, 11]);
%!test
%! [y, z] = buffer([1:12]',4);
%! assert (y, reshape(1:12,4,3));
%! assert (z, zeros (0,1));
%!test
%! [y, z] = buffer([1:11]',4);
%! assert (y, reshape(1:8,4,2));
%! assert (z, [9; 10; 11]);
%!test
%! [y,z,opt] = buffer(1:15,4,-2,1);
%! assert (y, reshape([2:5,8:11],4,2));
%! assert (z, [14, 15]);
%! assert (opt, 0);
%!test
%! [y,z,opt] = buffer(1:11,4,-2,1);
%! assert (y, reshape([2:5,8:11],4,2));
%! assert (z, zeros (1,0));
%! assert (opt, 2);
%!test
%! [y,z,opt] = buffer([1:15]',4,-2,1);
%! assert (y, reshape([2:5,8:11],4,2));
%! assert (z, [14; 15]);
%! assert (opt, 0);
%!test
%! [y,z,opt] = buffer([1:11]',4,-2,1);
%! assert (y, reshape([2:5,8:11],4,2));
%! assert (z, zeros (0, 1));
%! assert (opt, 2);
%!test
%! [y,z,opt] = buffer([1:11],5,2,[-1,0]);
%! assert (y, reshape ([-1:3,2:6,5:9],[5,3]));
%! assert (z, [10, 11]);
%! assert (opt, [8; 9]);
%!test
%! [y,z,opt] = buffer([1:11]',5,2,[-1,0]);
%! assert (y, reshape ([-1:3,2:6,5:9],[5,3]));
%! assert (z, [10; 11]);
%! assert (opt, [8; 9]);
|