MATLAB: Problem with MATLAB unwrap() function

phase unwrapping

Hi,
Step 1: I created a 2D phase function.
x = -1:1/128:1-(1/128);
[X,Y] = meshgrid(x,x);
phi = 3*pi*X.^2 + 3*pi*Y.^2;
Step 2: I wrapped it from -pi tp +pi.
wrapped_phi = phi; % copy phase to another 2D array
for vrt = 1:1:256
for hrz = 1:1:256
x = phi(vrt,hrz);
if (x > pi)
k = round(x/(2*pi));
x = x - k*(2*pi);
wrapped_phi(vrt,hrz) = x;
end
if (x < -pi)
k = round(abs(x)/(2*pi));
x = x + k*(2*pi);
wrapped_phi(vrt,hrz) = x;
end
end
end
Step 3: I used MATLAB unwrap() function.
But I did not get same phase function.
Phase values also are different (please see colorbars).
figure
imagesc(unwrap(wrapped_phi));colorbar; title('Using MATLAB unwrap function')
Why ?
Please see attached jpg images (which are outcomes of the above code snippets).
Any help will be appreciated.

Best Answer

Hello Swapnil, like many Matlab functions, for 2d arrays unwrap works down the columns only unless told otherwise. You need to unwrap in both dimensions, so try
imagesc(unwrap(unwrap(wrapped_phi,[],2),[],1));
which works, although there may be some luck involved.
Related Question