0

Here is my code for the 2D Discrete Fourier transform. I know, it is a bit brute force-ish, but I did not have much programming experience before taking Mathematical Physics this semester.

I am wondering why my program is so slow, and if there are any things that jump out that would make it more robust [it takes so long, and I am hoping that someone more experienced than I can see something wrong].

function [trans] = d2ftrans(Matrix)
[o,p] = size(Matrix);
F = 0;
for v = 0:1:p-1
    for u = 0:1:o-1
        for Xi = 0:1:o-1
            for Yi = 0:1:p-1
                S = Xi+1;
                T = Yi+1;
                c = Matrix(S,T) * exp(-1i*2*pi*(u*Xi/o + v*Yi/p));
                F = F+c;
            end
        end
        Si = v+1;
        Ti = u+1;
        G(Ti,Si) = F;
        F = 0;
    end
end
trans = G;
knedlsepp
  • 6,065
  • 3
  • 20
  • 41
  • 2
    A few things wrong here: 1.) at least one `end` is missing. 2.) you return something called `trans` that is never assigned to. 3.) function names can't start with a digit. – knedlsepp Mar 17 '15 at 17:42
  • Sorry, I forgot the last line of my script, and accidentally deleted part of my initial function. It is working though, just very slowly... – Aron Goldberg Mar 17 '15 at 19:35
  • 1
    Please use more descriptive variable names. I personally hate single character variables with a passion, with the exception of `i, j, k` being used as loop variables, but only when their use makes sense. – Setsu Mar 17 '15 at 19:45
  • The naive approach of computing the discrete 2D Fourier transform involves a double summation for every entry. This will result in four for-loops. The MATLAB builtin implementation of `fft2` is based on a [different algorithm](http://en.wikipedia.org/wiki/Fast_Fourier_transform#Algorithms), (Cooley-Tukey [listed here](http://de.mathworks.com/help/matlab/ref/fft.html#f83-998418)) which needs less computations. You could maybe improve the speed of your implementation a bit by vectorizing your code as described by *potAito*, but in the end this won't help you much for large matrices. – knedlsepp Mar 17 '15 at 19:50

1 Answers1

0

I'm sorry for being lazy, but I'll just give you a tip on what I think is going on here. See, many beginners start using loops for everything, even when doing some element operations on a vector. Example:

% Assume we want to square every element inside a random vector.
% First we create a vector
vector = rand(10,1);

% then we loop over each element, square it and store it where it was. 
for i=1:vector.length()
  vector(i) = vector(i)^2;
end

Now here's the simpler, better and way faster solution:

vector = rand(10,1);
% Pointwise operations in matlab are indicated with a dot (.)
vector = vector.^2;

So you'll always want to think about how you can do an operation in one step instead of writing a for loop. You'll have to check your problem for yourself and see where you can apply that. Matlab has the following elementwise operations:

vec1 .* vec2  % Multiply elements of vec1 and vec2 together
vec1 ./ vec2  % element-wise division
vec1 + vec2   % Addition is element-wise by default
vec1 - vec2   % same
vec.^2        % square each element of vec.
              % vec^2 is NOT the same thing!
Potaito
  • 1,181
  • 2
  • 10
  • 32