help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: buffer (signal)?


From: dbateman
Subject: Re: buffer (signal)?
Date: Thu, 10 Apr 2008 15:15:23 -0700 (PDT)

I should read more carefully what I write, the previous version was wrong. Ok
I've rewritten it and now actually tested it a little and the attached
appears to be a complete implementation of buffer. There might be some
corner cases where this function won't work however. For example if the
overlap is greater then the number of rows, though I have tested it. This
will need additional test code before inclusion in octave-forge.

D.

function [y, z, opt] = buffer (x, n, p, opt)

  if (nargin < 2 || nargin > 4)
    print_usage ();
  endif
  if (nargin < 3)
    p = 0;
  endif
  if (nargin <  4)
    if (p < 0)
      opt = 0;
    else
      opt = zeros (1, p);
    endif
  endif

  x = x (:);
  l = length (x);
  if (p < 0 && opt != 0)
    ## Special case
    if (isscalar (opt) && opt == floor (opt) && opt > 0 && opt <= -p)
      m = ceil ((l + opt) / n);
      y = zeros (n, m);
      y (1 + opt : l + opt) = x;
      y (end + p + 1 : end, :) = [];
      lopt = opt;
    else
      error ("buffer: expecting fourth argument to be and integer between 0
and -p");
    endif
  else
    m = ceil (l / (n - p));
    y = zeros (n - p, m);
    y (1 : l) = x;
    lopt = 0;
    if (p < 0)
      y (end + p + 1 : end, :) = [];
    elseif (p > 0)
      if (ischar (opt))
        if (strcmp (opt, "nodelay"))
          y = [y ; zeros(p, m)]; 
          y (end - p + 1 : end, 1 : end - 1) = y (1 : p, 2 : end);
          if (sub2ind([n-p, m], p, m) >= l)
            y (:, end) = [];
          endif
        else
          error ("buffer: unexpected string argument");
        endif
      elseif (isvector (opt))
        if (length (opt) == p)
          y = [zeros(p, m); y]; 
          y (1 : p) = opt;
          y (1 : p, 2 : end) = y (end - p + 1 : end, 1 : end - 1);
          lopt = p;
        else
          error ("buffer: opt vector must be of length p");
        endif
      else
        error ("buffer: unrecognized fourth argument");
      endif
    endif
  endif
  if (nargout > 1)
    [i, j] = ind2sub (size(y), l + lopt + p * (size (y, 2) - 1));
    if (any ([i, j] != size (y)))
       z = y (1 : i, end);
       y (:, end) = [];
    else
       z = zeros (0, 1);
    endif
  endif
endfunction

-- 
View this message in context: 
http://www.nabble.com/buffer-%28signal%29--tp16615296p16619818.html
Sent from the Octave - General mailing list archive at Nabble.com.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]