0

I've write a function that calculates the DFT of an image, My prupose is to show the amplitude spectrum without using the fftshift command. DFT_img.m looks like this:

function f = DFT_img(a);
  [n m]=size(a);
  for i =1:n
    k=1;
    for j =1:n
      l=1;
      F(i,j)=(1/n*n)*a(i,j)*exp(-i*2*pi*(k*i+l*j)/n);
      l=l+1;
    end
    k=k+1;
  end
  f=F;

And when i write in Command Window the commands

A = imread("lena.tiff");
ans = DFT_img(A);
spectrum = log(abs(ans));
mesh(spectrum)

i am not getting the same result that

fftshift matlab function

does !!! Am i having a mistake in the function, or where is the problem with that ?

Nico
  • 3,471
  • 2
  • 29
  • 40
ah92
  • 307
  • 5
  • 20

2 Answers2

1

Your code is not a 2D DFT, at all.

The easiest way to write a 2D DFT is to perform a 1D DFT on each of the columns, and then perform a 1D DFT on each of the rows of the results.

In pseudocode:

temp = zeros(size(a));
f = zeros(size(a));

for i = (1:m)
    temp(:,i) = dft(a(:,i));
end

for j = (1:n)
    f(j,:) = dft(temp(j,:));
end
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • i dont understand!! What is "dft" in this case – ah92 May 25 '13 at 09:53
  • It is showing me an Error !!! Undefined function 'dft' for input arguments of type 'double'. Error in DFT_img2 (line 6) temp(:,i) = dft(a(:,i)); – ah92 May 25 '13 at 09:56
  • @user2287999: Yes, it's **pseudocode**! You will need to implement a function called `dft`. – Oliver Charlesworth May 25 '13 at 09:57
  • My task is to use this formula F(i,j)=(1/n*n)*a(i,j)*exp(-i*2*pi*(k*i+l*j)/n); to perform DFT in the image that i created in this way: % draw a square in the middle of image % m - height of image % n - width of image % k - dimension of square function f = Kat(m,n,k) A = zeros(m,n); A(m/2-k:m/2+k,n/2-k:n/2+k)=1; f=A; end – ah92 May 25 '13 at 10:05
  • @user2287999: That formula is incorrect. It should be `F(k,l) = ...`. – Oliver Charlesworth May 25 '13 at 10:06
  • when i wrote in that way, the mesh command it is showing me this error Error using mesh (line 76) Z must be a matrix, not a scalar or vector. 'code' A = Kat(512,512,32); pergj=DFT_img(A); spectrum = log(abs(pergj)); mesh(spectrum) 'code' this is my Command Windows looks like !!! – ah92 May 25 '13 at 10:12
  • @user2287999: I think you will need to spend some time researching what a 2D DFT actually does. Until you understand that, it's somewhat hard for me to explain to you how to write the code ... ;) – Oliver Charlesworth May 25 '13 at 10:14
0

Your code does not look like DFT, and has some issues with index numbers: note that the DFT is defined as a sum of complex exponentials from 0:N-1, not from 1:N like in your code. Long story short: try this function (paste in a kmv_dft2.m file):

function F =  kmv_dft2(f)

[M, N] = size(f);
F = zeros(M,N);

for k = 0:M-1
    for l = 0:N-1
        F(k+1,l+1) = dft_atomic_sum(f,k,l,M,N);
    end
end

%% computing a core of DFT2
function F_kl = dft_atomic_sum(f,k,l,M,N)

F_kl = 0;

for m=0:M-1
    for n=0:N-1
        F_kl = F_kl + f(m+1,n+1)*exp(-i*2*pi*( k*m/M   + l*n/N) );        
    end
end

Now check this and compare with MATLAB FFT implementation

%%% input 2D signal
f = magic(5) 

%% Fast Fourier Transform
F_fft = fftshift(fft2(f))

%% Slow Discrete Fouriers Transform
F =  fftshift(kmv_dft2(f))

You may want to consult MATLAB help about fftshift function and what does it exactly do. This fftshift function is a source of constant confusion among novices, and I even wrote a short explanation for my own students about this.

virens
  • 28
  • 3