필터 지우기
필터 지우기

FFT of 3D array in MATLAB

조회 수: 9 (최근 30일)
Janee
Janee 2024년 8월 6일
댓글: Janee 2024년 8월 7일
I am trying to understand how the FFT of different directions in MATLAB works to reproduce in C/C++ instead.
So far I have the following simple example in MATLAB:
clearvars; clc; close all;
%3D FFT test
Nx = 8;
Ny = 4;
Nz= 6;
Lx =16;
Ly = 6;
dx = Lx/Nx;
dy = Ly/Ny;
%-----------
xi_x = (2*pi)/Lx;
yi_y = (2*pi)/Ly;
xi = ((0:Nx-1)/Nx)*(2*pi);
yi = ((0:Ny-1)/Ny)*(2*pi);
x = xi/xi_x;
y = yi/yi_y;
zlow = 0; %a
zupp =6; %b
Lz = (zupp-zlow);
eta_zgl = 2/Lz;
[D,zgl] = cheb(Nz);
zgl = (1/2)*(((zupp-zlow)*zgl) + (zupp+zlow));
[X,Z,Y] = meshgrid(x,zgl,y); %this gives 3d grid with z-by-x-by-y size (i.e. ZXY)
%ICs
A = 2*pi / Lx;
B = 2*pi / Ly;
u = (Z-zlow) .* (Z-zupp) .* sin(A*X).* sin(B*Y);
uh1 =(fft(u,[],3));%ZXY
uh2 =(fft(u,[],1));%ZXY
uh3 =(fft(u,[],2));%ZXY
So, in C/C++ I have a 3D tensor with (Nz+1) rows and Nx coumns and Ny matrices and taking the 1D FFT along each "row" of u returns the same results as the following in MATLAB:
uh3 =(fft(u,[],2));%ZXY
While taking the 1D FFT of u along each column of u in C/C++ returns the same result as the following in MATLAB:
uh2 =(fft(u,[],1));%ZXY
Then my question is what does this 1D FFT represent? and how should I represent it in C/C++?
uh1 =(fft(u,[],3));%ZXY
The cheb(N) function is:
function [ D, x ] = cheb ( N )
if ( N == 0 )
D = 0.0;
x = 1.0;
return
end
x = cos ( pi * ( 0 : N ) / N )';
c = [ 2.0; ones(N-1,1); 2.0 ] .* (-1.0).^(0:N)';
X = repmat ( x, 1, N + 1 );
dX = X - X';
% Set the off diagonal entries.
D =( c * (1.0 ./ c )' ) ./ ( dX + ( eye ( N + 1 ) ) );
% Diagonal entries.
D = D - diag ( sum ( D' ) );
return
end

채택된 답변

Matt J
Matt J 2024년 8월 6일
Why reinvent the wheel. Why not just use the open source FFTW C/C++ library (which Matlab is based on).
In any case, the mathematical interpretation of Matlab's 1D FFT is in the documentation,
  댓글 수: 9
Matt J
Matt J 2024년 8월 6일
편집: Matt J 2024년 8월 7일
No, it is the fft along each vector u(i,j,:).
Janee
Janee 2024년 8월 7일
This line "fft along each u(i,j,:)" actually solves my issue and makes more sense!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Fourier Analysis and Filtering에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by