view scripts/signal/movslice.m @ 26248:8a0778f549e8

movfun.m, movslice.m: Add additional input validation and BIST tests for same. * movfun.m: Rename "basefun" to "basefcn" in documentation. Validate number of inputs is at least 3. Use strcmpi, rather than ismember, in validation for performance. Use anonymous variable @(d), not @(x) when writing anonymous function that needs to check the existing variable 'x' so that the value is not shadowed. Use isindex() to verify indices for performance. Fix search for first non-singleton dimension so it returns a value even for scalar or empty matrix. Validate that WLEN is a 1- or 2-element array of integers >= 0. Use parfor, instead of for, for loop over columns. Eventually this might do something meaningful in Octave. Add BIST tests for input validation that check the expected error message. * movslice.m: Validate that exactly two arguments are provided. Validate that N is a positive integer. Validate that WLEN is a 1- or 2-element array of integers >= 0. Add BIST tests for input validation that check the expected error message.
author Rik <rik@octave.org>
date Sun, 16 Dec 2018 15:21:48 -0800
parents 7adb62e4cc39
children 78c4aadfbfd9
line wrap: on
line source

## Copyright (C) 2018 Juan Pablo Carbajal
##
## This file is part of Octave.
##
## Octave 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.
##
## Octave 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 Octave; see the file COPYING.  If not, see
## <https://www.gnu.org/licenses/>.

## Author: Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
## Created: 2018-12-08

## -*- texinfo -*-
## @deftypefn  {} {@var{slcidx} =} movslice (@var{N}, @var{wlen})
## @deftypefnx {} {[@var{slcidx}, @var{C}, @var{Cpre}, @var{Cpost}, @var{win}] =} movslice (@dots{})
## Generate indices to slice a vector of length @var{N} in to windows
## of length @var{wlen}.
##
## FIXME: Document inputs N, wlen
##
## FIXME: Document outputs slcidx, C, Cpre, Cpost, win.
## @seealso{movfun}
## @end deftypefn

function [slcidx, C, Cpre, Cpost, win] = movslice (N, wlen)

  if (nargin != 2)
    print_usage ();
  endif

  ## Validate N
  if (! (isscalar (N) && isindex (N)))
    error ("movslice: N must be a positive integer");
  endif

  ## Validate window length
  if (! (isnumeric (wlen) && all (wlen >= 0) && fix (wlen) == wlen))
    error ("Octave:invalid-input-arg",
           "movslice: WLEN must be a scalar or 2-element array of integers >= 0");
  endif
  if (isscalar (wlen))
    ## Check for proper window length
    ## FIXME: Matlab accepts even windows
    if (mod (wlen, 2) == 0)
      error ("Octave:invalid-input-arg", "movslice: WLEN must be an odd length");
    elseif (wlen == 1)
      error ("Octave:invalid-input-arg", "movslice: WLEN must be > 1");
    endif
  elseif (numel (wlen) == 2)
    ## FIXME: Any further tests needed to validate form: wlen = [nb, na] ???
  else
    error ("Octave:invalid-input-arg",
           "movfun: WLEN must be a scalar or 2-element array of integers >= 0");
  endif

  ## FIXME: Eventually add support for asymmetric window
  if (isscalar (wlen))
    hwlen = (wlen - 1) / 2;
    wlen = [hwlen, hwlen];
  endif

  Cpre  = 1:wlen(1);              # centers that can't fit the pre-window
  Cnf   = N - wlen(2) + 1;        # first center that can't fit the post-window
  Cpost = Cnf:N;                  # centers that can't fit post-window
  C     = (wlen(1) + 1):(Cnf - 1);
  win   = (-wlen(1):wlen(2)).';
  slcidx = C + win;

endfunction


## FIXME: Need BIST functional tests

## Test input validation
%!error movslice ()
%!error movslice (1)
%!error movslice (1,2,3)
%!error <WLEN must be .* array of integers> movslice (1, {1})
%!error <WLEN must be .* array of integers .= 0> movslice (1, -1)
%!error <WLEN must be .* array of integers> movslice (1, 1.5)
%!error <WLEN must be an odd length> movslice (1, 4)
%!error <WLEN must be . 1> movslice (1, 1)
%!error <WLEN must be a scalar or 2-element array> movslice (1, [1, 2, 3])