function q = unwrap(p,cutoff) %UNWRAP Unwrap the phase. % UNWRAP(P) unwraps radian phases P by changing absolute % jumps greater than pi to their 2*pi complement. P can be a % scalar or a vector. % % UNWRAP(P,TOL) uses a jump tolerance of TOL rather % than the default TOL = pi. % % See also ANGLE, ARG, ABS. % Davide Rocchesso: 1998/02/9 % Convert row vector in a column vector. rflag = 0; sizp = size(p); if (sizp(1) == 1) & (sizp(2) > 0), rflag = 1; p = p.'; end; % Initialize parameters. nshifts = 0; perm = 1:length(sizp); if nargin == 1 cutoff = pi; end; % Unwrap phase angles. [m, n] = size(p); pmin = min(p); pmin = pmin(ones(m, 1), :); % To force REM to behave. p = rem(p - pmin, 2 .* pi) + pmin; % Phases modulo 2*pi. if m > 1 b = [p(1, :); diff(p)]; % Differentiate phases. else b = p; end c = -(b > cutoff); d = (b < -cutoff); % Locations of jumps. e = (c + d) .* 2 .* pi; % Array of 2*pi jumps. f = cumsum(e); % Integrate to get corrections. q = p + f; % Phases + corrections. if rflag, q = q.'; end