


REMOVEOFFSET Remove DC offset using various techniques. Y = REMOVEOFFSET(X) subtracts the mean value from the data in vector X and returns it in vector Y. If X is a matrix, REMOVEOFFSET removes the mean value from each column. Y = REMOVEOFFSET(X, DIM) removes the mean along dimension DIM for the N-D matrix X. DIM can be the empty matrix [], in which case it defaults to 2 for row vectors and 1 for all other arrays. Y = REMOVEOFFSET(X, DIM, 'median') subtracts the median. Y = REMOVEOFFSET(X, DIM, 'mean') subtracts the mean (default). Y = REMOVEOFFSET(X, DIM, 'local') subtracts a local 3x3 average. Y = REMOVEOFFSET(X, DIM, MODE, DC) uses the matrix DC to compute the offset, which is then removed from X to give Y. If MODE is 'mean or 'median', DC must have the same size as X in all dimensions except for DIM. If MODE is 'local', DC must be the same size as X in all dimensions. See also DETREND.


0001 function Y = removeoffset(X, dim, mode, dc) 0002 %REMOVEOFFSET Remove DC offset using various techniques. 0003 % Y = REMOVEOFFSET(X) subtracts the mean value from the data in vector X 0004 % and returns it in vector Y. If X is a matrix, REMOVEOFFSET removes 0005 % the mean value from each column. 0006 % 0007 % Y = REMOVEOFFSET(X, DIM) removes the mean along dimension DIM for the 0008 % N-D matrix X. DIM can be the empty matrix [], in which case it 0009 % defaults to 2 for row vectors and 1 for all other arrays. 0010 % 0011 % Y = REMOVEOFFSET(X, DIM, 'median') subtracts the median. 0012 % Y = REMOVEOFFSET(X, DIM, 'mean') subtracts the mean (default). 0013 % Y = REMOVEOFFSET(X, DIM, 'local') subtracts a local 3x3 average. 0014 % 0015 % Y = REMOVEOFFSET(X, DIM, MODE, DC) uses the matrix DC to compute the 0016 % offset, which is then removed from X to give Y. If MODE is 'mean or 0017 % 'median', DC must have the same size as X in all dimensions except for 0018 % DIM. If MODE is 'local', DC must be the same size as X in all 0019 % dimensions. 0020 % 0021 % See also DETREND. 0022 0023 %%%%%%%%%%%%%%%%%%%%%%%%%%%% Parse Inputs %%%%%%%%%%%%%%%%%%%%%%%%%%%% 0024 if (nargin < 4), dc = X; end; 0025 if ((nargin < 3) || (isempty(mode))), mode = 'mean'; end 0026 if ((nargin < 2) || (isempty(dim))) 0027 if (isvectord(X)>1), dim = 2; else dim = 1; end; 0028 end 0029 if (ischar(dim)), error('Second argument must be numeric.'); end; 0030 0031 %%%%%%%%%%%%%%%%%%%%%%%%%%% Compute Offset %%%%%%%%%%%%%%%%%%%%%%%%%%% 0032 % need this to match the computed offsets dimensions to X 0033 sz = size(X); rep = ones(size(sz)); rep(dim) = sz(dim); 0034 0035 switch (mode), 0036 case 'mean', offset = repmat(mean(dc, dim), rep); 0037 case 'median', offset = repmat(median(dc, dim), rep); 0038 case 'local', offset = conv2(dc, ones(3)/9, 'same'); 0039 otherwise, error('Invalid mode.'); 0040 end 0041 0042 %%%%%%%%%%%%%%%%%%%%%%%%%%%% Remove Offset %%%%%%%%%%%%%%%%%%%%%%%%%%% 0043 Y = X - offset;