2

I am plotting a phase map and using a circular colormap like hsv. The issue I am having is at the pi/-pi angle interfaces, the interpolation (shading interp command in MATLAB) gives me 0, which leads to strange lines in the resulting figure (see below, the below figure is the 4 tangent arctan function). Is there a way I can get rid of these artifacts? I like the smoothness of interpolated shading as opposed to flat shading, but flat shading avoids these artifacts.

enter image description here

Here is the code that generates the above image:

[x_grid,y_grid] = meshgrid(-31:32,-31:32);
phase = atan2(y_grid,x_grid);
surf(x_grid,y_grid,phase);
view(0,90);
shading interp
colorbar
axis([-31 32 -31 32])
colormap hsv

1 Answers1

2

The problem is that using an N-by-N set of grid points is always going to have a discontinuity at the pi/-pi interface which it will try to interpolate across. You can instead create a 2-by-N strip of coordinates that wrap around the origin and are disconnected at the pi/-pi interface. The following illustrates how the discontinuity looks for the 2 approaches:

% N-by-N grid:
[x_grid, y_grid] = meshgrid(-31:32, -31:32);
phase = atan2(y_grid, x_grid);
subplot(1, 2, 1);
surf(x_grid, y_grid, phase);
title('N-by-N grid');

% 2-by-N strip:
X = [-31.*ones(1, 33) -30:31 32.*ones(1, 64) 31:-1:-30 -31.*ones(1, 32); ...
     zeros(1, 253)];
Y = [0:32 32.*ones(1, 62) 32:-1:-31 -31.*ones(1, 62) -31:0; ...
     zeros(1, 253)];
phase = atan2(Y([1 1], :), X([1 1], :));
phase(:, end) = -pi;
subplot(1, 2, 2);
surf(X, Y, phase);
title('2-by-N strip');

enter image description here

And here's how the final 2-D view would look (with a higher-resolution colormap):

X = [-31.*ones(1, 33) -30:31 32.*ones(1, 64) 31:-1:-30 -31.*ones(1, 32); ...
     zeros(1, 253)];
Y = [0:32 32.*ones(1, 62) 32:-1:-31 -31.*ones(1, 62) -31:0; ...
     zeros(1, 253)];
phase = atan2(Y([1 1], :), X([1 1], :));
phase(:, end) = -pi;
surf(X, Y, phase);
view(0, 90);
shading interp;
colorbar;
axis([-31 32 -31 32]);
colormap(hsv(256));

enter image description here

gnovice
  • 125,304
  • 15
  • 256
  • 359