function [paddedv, nfront, nback] = padwithzeros(v) % FUNCTION: padwithzeros Part of DPWT Toolbox % % SYNOPSIS: [PADDEDV, NFRONT, NBACK] = padwithzeros(v) % % DESCRIPTION: padwithzeros(V) prepends and postpends zeros onto input vector % V until its length is an integer power of 2. % If the difference between the length of V and % the next highest power of 2 is odd then % ceil((2^p - length(v))/2) zeros are prepended % and floor((2^p - length(v))/2) are postpended. % If the difference is even then half the missing % zeros are added to each end. If V is a column % (row) then paddedv is a column(row). NFRONT and NBACK % return the number of zeros added to the front and back % of V to make PADDEDV, the padded version of V. % % EXAMPLE: v = [1 2 3 4 5]; % paadwithzerosS(v) % ans = % 0 0 1 2 3 4 5 0 % % To force a vector whose length is already an % integer power of 2 to be padded to a length that % is the next power of 2, append a 0 to the vector % and then call padwithzeros, e.g. if v is a column % (row) vector of length 8 that you wish padded to % length 16 use PADWITHZEROS([v;0]) or if v is a row % use PadWithZeros([v,0]). % % AUTHOR: Neil Getz % DATE: 5-14-92 % % COPYRIGHT 1992, Neil Getz % if (nargin~=1), help padwithzeros; return; end; [m,n] = size(v); if((m~=1)&(n~=1)), error('Arg must be a vector'); end; lenv = (m>n)*m + (n>=m)*n; p = ceil(log(lenv)/log(2)); nfront = ceil((2^p - lenv)/2); nback = 2^p - lenv - nfront; if(m>n), paddedv = [zeros(nfront,1); v; zeros(nback,1)]; else paddedv = [zeros(1, nfront), v, zeros(1,nback)]; end