- https://www.mathworks.com/help/matlab/ref/meshgrid.html
- https://www.mathworks.com/help/matlab/ref/griddata.html
How do I perform an fft2 from x-y coördinates and a value?
16 views (last 30 days)
Show older comments
I am trying to get the 2D frequency spectrum from a dataset I am currently working with.
The fft2 function how,ever assumes the data to be in a square matrix form, but this is not my case and I am not always able to transform the data to a square-matrix.
I have a vector containing the x-position, one of the y-position, and the value that corresponds to that location.
An example code that shows the data-format:
xcoordinate = [-2.024, -2.924, 3.042, 0, 2.412, 1.119];
ycoordinate = [3.043, 2.342, 1.119, -2.293, -1.192, 0];
value = [12, 23, 43, 92, 1, 4];
What my current solution is, is creating a 2D-matrix as a grid, rounding down the coordinates and placing the values at those points:
matrix =
[ 0 12 0 0 0 0
23 0 0 0 0 0
0 0 0 0 0 43
0 0 4 0 0 0
0 1 0 0 0 0
0 0 0 92 0 0
];
fft2(matrix);
This is, however, very inaccurate and scales horribly.
Is there a better/easier way to perform a spatial fft2 from a set of x- and y-coördinates and a value at that position? Any idea would be very appreciated!
Thanks in advance.
0 Comments
Answers (1)
Abhimenyu
on 3 Nov 2023
Edited: Abhimenyu
on 3 Nov 2023
Hi Simon,
I understand that you want to perform a spatial fast Fourier transform from a set of x-coordinates, y-coordinates and a value at a particular position using the "fft2" function of MATLAB.
For better performance of the "fft2" function, the data must be interpolated onto a regular grid accurately. The "meshgrid" function of MATLAB can be used to convert coordinates into a grid of points to represent spatial domain and then the "griddata" function of MATLAB can be used to estimate the values at the grid points based on the original data.
Please refer to the example MATLAB code below for more understanding:
% Assuming you have the xcoordinate, ycoordinate, and value vectors
xcoordinate = [-2.024, -2.924, 3.042, 0, 2.412, 1.119];
ycoordinate = [3.043, 2.342, 1.119, -2.293, -1.192, 0];
value = [12, 23, 43, 92, 1, 4];
% Define the grid
xMin = min(xcoordinate);
xMax = max(xcoordinate);
yMin = min(ycoordinate);
yMax = max(ycoordinate);
gridSize = 100; % Adjust the grid size as needed
%using the meshgrid function to create a grid of points
[X, Y] = meshgrid(linspace(xMin, xMax, gridSize), linspace(yMin, yMax, gridSize));
% Interpolate the data using the griddata function
V = griddata(xcoordinate, ycoordinate, value, X, Y, 'nearest');% Experiments can be done using various methods like 'linear', 'cubic' etc.
% Compute the 2D FFT
fft2Data = fft2(V);
% Visualize the frequency spectrum
figure;
imagesc(abs(fftshift(fft2Data)));
colormap jet;
colorbar;
xlabel('Frequency (k_x)');
ylabel('Frequency (k_y)');
title('2D Frequency Spectrum');
The above method will help to obtain better results using the "fft2" function.
To know more about the "meshgrid" and "griddata" functions, please refer to the MATLAB documentation links given below respectively:
I hope this helps to resolve the query.
Thanks,
Abhimenyu
0 Comments
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!