]> Creatis software - CreaPhase.git/blob - octave_packages/signal-1.1.3/upsample.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / signal-1.1.3 / upsample.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} =} upsample (@var{x}, @var{n})
6 ## @deftypefnx {Function File} {@var{y} =} upsample (@var{x}, @var{n}, @var{offset})
7 ## Upsample the signal, inserting n-1 zeros between every element.
8 ##
9 ## If @var{x} is a matrix, upsample every column.
10 ##
11 ## If @var{offset} is specified, control the position of the inserted sample in
12 ## the block of n zeros.
13 ## @seealso{decimate, downsample, interp, resample, upfirdn}
14 ## @end deftypefn
15
16 function y = upsample (x, n, phase = 0)
17   if nargin<2 || nargin>3, print_usage; end
18
19   if phase > n - 1
20     warning("This is incompatible with Matlab (phase = 0:n-1). See ...
21     octave-forge signal package release notes for details." )
22   end
23
24   [nr,nc] = size(x);
25   if any([nr,nc]==1),
26     y = zeros(n*nr*nc,1);
27     y(phase + 1:n:end) = x;
28     if nr==1, y = y.'; end
29   else
30     y = zeros(n*nr,nc);
31     y(phase + 1:n:end,:) = x;
32   end
33 end
34
35 %!assert(upsample([1,3,5],2),[1,0,3,0,5,0]);
36 %!assert(upsample([1;3;5],2),[1;0;3;0;5;0]);
37 %!assert(upsample([1,2;5,6;9,10],2),[1,2;0,0;5,6;0,0;9,10;0,0]);
38 %!assert(upsample([2,4],2,1),[0,2,0,4]);
39 %!assert(upsample([3,4;7,8],2,1),[0,0;3,4;0,0;7,8]);