Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
dft error

Subject: dft error

From: John Deas

Date: 11 Sep, 2007 14:19:52

Message: 1 of 3

Hi, I need to write my own version of a 2d dft algorithm. I
wanted to compare the results with Matlab's fft2d
algorithm, but I can not find why they give different
bidimensional spectrums.
Here is my code, with some graphical outputs to underline
the differences.

% --- beginning code

% user input variables
N=10;
x=zeros(N,N);

% 2D function to be analyzed
for i=1:N
    for j=1:N
        x(i,j)=((i-N/2)^2+(j-N/2)^2)^(1/2);
    end
end

c=N/10/(2*(2*log(2))^1/2);

for i=1:N
    for j=1:N
        x(i,j)=exp((-(x(i,j)-N/4)^2)/(2*c^2));
    end
end

mesh(x);
title('Function to be analyzed');
pause;

% actual dft computation
fftx=zeros(N,N);

for k=1:N
    for l=1:N
        for n=1:N
            for m=1:N
                fftx(k,l)=fftx(k,l)+x(n,m)*exp(-j*2*pi*((k-
1)*(n-1)/N+(l-1)*(m-1)/N));
            end
        end
    end
end

mesh(abs(fftx));
title('User produced 2D dft');
pause;

mesh(abs(fft(x)));
title('Matlab fft2d result');
pause;

% comparison with Matlab result
Err=norm(fftx-fft(x));

% ---- end code

Any help would be appreciated as I am stuck on this for
some times without much results...

Thanks,

J.D.

Subject: dft error

From: John Deas

Date: 11 Sep, 2007 14:22:21

Message: 2 of 3

"John Deas" <john.deas.smash.spam@gmail.com> wrote in
message <fc6868$m0h$1@fred.mathworks.com>...
> Hi, I need to write my own version of a 2d dft algorithm.
I
> wanted to compare the results with Matlab's fft2d
> algorithm, but I can not find why they give different
> bidimensional spectrums.
> Here is my code, with some graphical outputs to underline
> the differences.
>
> % --- beginning code
>
> % user input variables
> N=10;
> x=zeros(N,N);
>
> % 2D function to be analyzed
> for i=1:N
> for j=1:N
> x(i,j)=((i-N/2)^2+(j-N/2)^2)^(1/2);
> end
> end
>
> c=N/10/(2*(2*log(2))^1/2);
>
> for i=1:N
> for j=1:N
> x(i,j)=exp((-(x(i,j)-N/4)^2)/(2*c^2));
> end
> end
>
> mesh(x);
> title('Function to be analyzed');
> pause;
>
> % actual dft computation
> fftx=zeros(N,N);
>
> for k=1:N
> for l=1:N
> for n=1:N
> for m=1:N
> fftx(k,l)=fftx(k,l)+x(n,m)*exp(-
j*2*pi*((k-
> 1)*(n-1)/N+(l-1)*(m-1)/N));
> end
> end
> end
> end
>
> mesh(abs(fftx));
> title('User produced 2D dft');
> pause;
>
> mesh(abs(fft(x)));
> title('Matlab fft2d result');
> pause;
>
> % comparison with Matlab result
> Err=norm(fftx-fft(x));
>
> % ---- end code
>
> Any help would be appreciated as I am stuck on this for
> some times without much results...
>
> Thanks,
>
> J.D.
>

sorry for the flood I did not thought the Mathwork
community site gave access to the same group.

Subject: dft error

From: Greg Heath

Date: 12 Sep, 2007 14:18:08

Message: 3 of 3

On Sep 11, 10:19 am, "John Deas" <john.deas.smash.s...@gmail.com>
wrote:
> Hi, I need to write my own version of a 2d dft algorithm. I
> wanted to compare the results with Matlab's fft2d
> algorithm, but I can not find why they give different
> bidimensional spectrums.
> Here is my code, with some graphical outputs to underline
> the differences.
>
> % --- beginning code
>
> % user input variables
> N=10;
> x=zeros(N,N);
>
> % 2D function to be analyzed
> for i=1:N
> for j=1:N
> x(i,j)=((i-N/2)^2+(j-N/2)^2)^(1/2);
> end
> end
>
> c=N/10/(2*(2*log(2))^1/2);
>
> for i=1:N
> for j=1:N
> x(i,j)=exp((-(x(i,j)-N/4)^2)/(2*c^2));
> end
> end
>
> mesh(x);
> title('Function to be analyzed');
> pause;
>
> % actual dft computation
> fftx=zeros(N,N);
>
> for k=1:N
> for l=1:N
> for n=1:N
> for m=1:N
> fftx(k,l)=fftx(k,l)+x(n,m)*exp(-j*2*pi*((k-
> 1)*(n-1)/N+(l-1)*(m-1)/N));
> end
> end
> end
> end
>
> mesh(abs(fftx));
> title('User produced 2D dft');
> pause;
>
> mesh(abs(fft(x)));
> title('Matlab fft2d result');
> pause;
>
> % comparison with Matlab result
> Err=norm(fftx-fft(x));
>
> % ---- end code
>
> Any help would be appreciated as I am stuck on this for
> some times without much results...
>
> Thanks,
>
> J.D.

Perhaps using matrix notation would help. See my post
in the thread "inverse fft without ifft".

Hope this helps.

Greg

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us