]> Creatis software - CreaPhase.git/blob - octave_packages/signal-1.1.3/downsample.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / signal-1.1.3 / downsample.m
1 ## Author: Paul Kienzle <pkienzle@users.sf.net> (2007)
2 ## This program is granted to the public domain.
3
4 ## -*- texinfo -*-
5 ## @deftypefn {Function File} {@var{y} =} downsample (@var{x}, @var{n})
6 ## @deftypefnx {Function File} {@var{y} =} downsample (@var{x}, @var{n}, @var{offset})
7 ## Downsample the signal, selecting every nth element.  If @var{x}
8 ## is a matrix, downsample every column.
9 ##
10 ## For most signals you will want to use @code{decimate} instead since
11 ## it prefilters the high frequency components of the signal and
12 ## avoids aliasing effects.
13 ##
14 ## If @var{offset} is defined, select every nth element starting at
15 ## sample @var{offset}.
16 ## @seealso{decimate, interp, resample, upfirdn, upsample}
17 ## @end deftypefn
18
19 function y = downsample (x, n, phase = 0)
20
21   if nargin<2 || nargin>3, print_usage; end
22
23   if phase > n - 1
24     warning("This is incompatible with Matlab (phase = 0:n-1). See ...
25     octave-forge signal package release notes for details.")
26   end
27
28   if isvector(x)
29     y = x(phase + 1:n:end);
30   else
31     y = x(phase + 1:n:end,:);
32   end
33 endfunction
34
35 %!assert(downsample([1,2,3,4,5],2),[1,3,5]);
36 %!assert(downsample([1;2;3;4;5],2),[1;3;5]);
37 %!assert(downsample([1,2;3,4;5,6;7,8;9,10],2),[1,2;5,6;9,10]);
38 %!assert(downsample([1,2,3,4,5],2,1),[2,4]);
39 %!assert(downsample([1,2;3,4;5,6;7,8;9,10],2,1),[3,4;7,8]);