taking partial derivative via fourier transform using matlab

1.1k Views Asked by At

For a task for university, I need to calculate in matlab a partial derivative using the 2D Fourier transform. Using equation (13) on pg 3 in http://www2.ph.ed.ac.uk/~wjh/teaching/Fourier/documents/properties.pdf , we have the formula $$\mathbb{F}_2 ( \frac{df(u,v)}{du} )= i 2 \pi u f(u, v)$$ So when implement it in matlab (so then we use the discrete version of the fourier transform), It went a bit wrong. Based on http://www.mathworks.com/matlabcentral/answers/41712-derivative-using-fft-properties I have following code where we use the function $f(x,y) = \sin(x2 \pi)$ as test function, so the derivative is $\cos(x2 \pi)2 \pi$

    N = 64;
    dkx = 1/N;
    dky = 1/N;
    kx = -1/2: dkx : 1/2-dkx; % x-wavenumber
    ky = -1/2 : dky : 1/2-dky; % y-wavenumber

    [X,Y] = meshgrid(kx,ky);
    data_spacedomain = sin(X*2*pi);
    % Compute 2D FFT
    data_wavenumberdomain = fft2(data_spacedomain);

    % Compute grid of wavenumbers
    [KX, KY] = meshgrid(ifftshift(kx),ifftshift(ky));

    % Compute 2D derivative
    data_wavenumberdomain_differentiated =  1i*2*pi*KX.*data_wavenumberdomain; 
    % Convert back to space domain
    data_spacedomain_differentiated = ifft2(data_wavenumberdomain_differentiated );

    figure(1)
    surf( X,Y,real(data_spacedomain_differentiated ))
    figure(2)
    surf( X,Y, cos(X*2*pi)*2*pi)

Then the derivative ( equal to $\cos(x2 \pi)2 \pi$) is enter image description here but the derivative I get from my program, is enter image description here Has someone an idea where in my code it went wrong?

1

There are 1 best solutions below

1
On

you need to multiply with $N$, so you need to plot real(data_spacedomain_differentiated*N ) , If you do so , it is exactly the same !!