Home > database >  Can't plot results of matlab 2D fast fourier transform
Can't plot results of matlab 2D fast fourier transform

Time:12-13

I conducted a 2D fourier transform on a 4436413x3 matrix using the matlab fft2 command (this one: https://www.mathworks.com/help/matlab/ref/fft2.html). The product is a 4436413x3 matrix containing complex values. When I try to plot the data, I end up with a graph that is all one color or an error message that references the complex values in my data.

I have tried interpolating to create a 2D set of values using the griddata() function, however, when applied to my 3D dataset, this returns a 1D vector equal to the z-column in my original 3D dataset. My 3D dataset consists of x, y, and z points, and in my attempt to interpolate I used vq = griddata(x,y,z,x,y). I had also tried to create a 4436412x3 meshgrid for my query points (using [xq, yq] = meshgrid(4436412, 3), and then using xq and yq as my query points), but this was returning NaN.

When I run Y = fft2(x); imagesc(abs(fftshift(Y))) I get a single purple box.

When I run Y = fft2(x); imagesc(Y) I get the following error message:

Error using image
Complex values are not supported. Specify the color data as numeric or logical values.

Error in imagesc (line 52)
    hh = image(varargin{:}, 'CDataMapping', 'scaled');

CodePudding user response:

To apply the FFT, you need data to be sampled on a regular grid. Your data represents (x,y,z) coordinates of points. Here's how to use griddata to resample these coordinates onto a regular (x,y) grid:

% Generate example data, let's say x and y are in the range [-3,3]:
n = 62500;
x = rand(n, 1) * 6 - 3;
y = rand(n, 1) * 6 - 3;
z = sin(4*x)   cos(2*y); % an example function
data = [x,y,z]; % This is a 4436413x3 array with (x,y,z) coordinates

% Interpolate z values onto a regular (x,y) grid:
[xq, yq] = meshgrid(linspace(min(x), max(x), sqrt(n)), ...
                    linspace(min(y), max(y), sqrt(n)));
zq = griddata(x, y, z, xq, yq);

% griddata doesn't extrapolate, writes NaN instead. Let's fill those
% in with zeros (maybe a different value is more meaningful in your
% application).
zq(isnan(zq)) = 0;

% Now you can apply a 2D Fourier transform:
Z = fft2(zq);
imshow(log(abs(fftshift(Z))), []);
  • Related