Differentiating in one direction using FFT2

조회 수: 7 (최근 30일)
Daniele Avitabile
Daniele Avitabile 2025년 2월 18일
편집: Matt J 2025년 2월 21일
Starting from a univariateL-periodic function u, sampled at evenly-spaced points in , and stored in the vector u, the following function approximates the first derivative , using FFT
function du = FFTDiff(u,n,L)
% Frequency vector
k = (2*pi/L) * [0:n/2-1, -n/2:-1]';
% Compute FFT
uHat = fft(u);
% Compute the derivative in the frequency domain, and back to physical domain
du = ifft( 1i * k .* uHat, 'symmetric');
end
First of all, if you see any alternative ways on how to write the function above, your feedback is welcome. I am now in the process of writing a function that does the following: starting from a bivariate, periodic function u, sampled at evenly spaced points in , I want to write a function that approximates (hence the partial derivative with respect to x), using FFT2. The main problem I have is that I don't quite know how FFT2 outputs the wavenumbers and . It's a bit tricky to understand from the documentation, and I wonder if you have some ideas on how to do that.
  댓글 수: 3
Paul
Paul 2025년 2월 19일
Hi Daniele,
Can you show how this code works with a simple input? I'm running into an error.
Is the code supposed to work for L even and L odd?
L = 20; % period of function
l = 0:L; % n + 1 equally spaced points in [0,L]
n = numel(l) - 1; % n
u = sin(2*pi/20*l).'; % u
du = FFTDiff(u,n,L);
Arrays have incompatible sizes for this operation.

Error in solution>FFTDiff (line 16)
du = ifft( 1i * k .* uHat, 'symmetric');
figure
plot(l,du,'-o')
function du = FFTDiff(u,n,L)
% Frequency vector
k = (2*pi/L) * [0:n/2-1, -n/2:-1]';
% Compute FFT
uHat = fft(u);
% Compute the derivative in the frequency domain, and back to physical domain
du = ifft( 1i * k .* uHat, 'symmetric');
end
Walter Roberson
Walter Roberson 2025년 2월 19일
k = (2*pi/L) * [0:n/2-1, -n/2:-1]';
should probably be
k = (2*pi/L) * [0:n/2-1, -n/2:-1/2]';

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

채택된 답변

Matt J
Matt J 2025년 2월 18일
편집: Matt J 2025년 2월 18일
fft2(u) is equivalent to fft( fft(u,[],1) ,[],2), if that helps at all.
Truthfully, it doesn't appear that you need to use fft2. Assuming the x-direction is column-oriented and the y-direction is row-oriented, you can just use your original function as is to differentiate along x.
  댓글 수: 2
Daniele Avitabile
Daniele Avitabile 2025년 2월 18일
Not much, but thanks for answering. At the moment I got this code, but I am not sure this is optimal, really
function dudx = FFTDiff2D(u, nx, ny, Lx, Ly)
% Frequency vectors
kx = (2 * pi / Lx) * [0:nx/2-1, -nx/2:-1]'; % Wavenumbers in x direction
ky = (2 * pi / Ly) * [0:ny/2-1, -ny/2:-1]; % Wavenumbers in y direction
% Create a meshgrid for the wavenumbers
[KX, KY] = meshgrid(kx, ky);
% Compute 2D FFT of the input function
uHat = fft2(u);
% Compute the derivative in the frequency domain
dudx_hat = 1i * KX .* uHat;
% Back to physical domain using inverse FFT
dudx = ifft2(dudx_hat, 'symmetric');
end
Matt J
Matt J 2025년 2월 18일
편집: Matt J 2025년 2월 21일
Like I said, if you are only differentiating along the columns, FFTDiff() should already be sufficient. There is no need for fft2. If you are differentiating along the rows, then you can still use FFTDiff, but just pre-transpose the input.

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

추가 답변 (1개)

Catalytic
Catalytic 2025년 2월 18일
편집: Catalytic 2025년 2월 18일
How about this? It generalizes your original function to let you differentiate along any specified dimension dim for any nD array u -
function du = FFTDiff(u,L,dim)
arguments
u double {mustBeNonempty}
L (1,1) double
dim (1,1) double = find(size(u)>1,1) %default to first non-singleton dimension
end
n=size(u,dim);
% Frequency vector
k = (2*pi/L) * ifftshift( (0:n-1)-ceil((n-1)/2) );
e=ones(1,ndims(u)); e(dim)=n;
k=reshape(k,e);
% Compute FFT
uHat = fft(u,[],dim);
% Compute the derivative in the frequency domain, and back to physical domain
du = ifft( 1i * k .* uHat, [],dim,'symmetric');
end
  댓글 수: 2
Walter Roberson
Walter Roberson 2025년 2월 18일
I think your code might possibly have some trouble of u is empty. In that case, size(u, find(size(u)>1,1)) becomes size(u,[]) which returns [] . But [] cannot be stored in something size (1,1)
If you are successful in storing the [] into dim, then n=size(u,dim) would return empty, and I suspect that would lead to problems.
Catalytic
Catalytic 2025년 2월 18일
Yes, probably. I fixed it.

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

카테고리

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