Matlab syntax for 2-d fourier transform

In summary: I told you the recipe: you need to do a series of 1D transforms. As you already know how to do one, it shouldn't be hard to extend the code to loop over the other dimension. (Note that you will probably have to use a loop here. I can't see how you could do the full 2D FT only with vector operations.
  • #1
Ben Wilson
90
16
I have a function f(x,y) which i have defined in this way:

a vector x and a vector y

meshgrid[x,y]

z= f(meshgrid[x,y]).

how do i do a 2-d Fourier transform of f(x,y)?

the transform must be done without using operations like fft, and must be done using summations written in the code.
 
Physics news on Phys.org
  • #2
Ben Wilson said:
I have a function f(x,y) which i have defined in this way:

a vector x and a vector y

meshgrid[x,y]

z= f(meshgrid[x,y]).

how do i do a 2-d Fourier transform of f(x,y)?

the transform must be done without using operations like fft, and must be done using summations written in the code.
To further illustrate what i mean, my transforms for 1-D functions (f(x)) have been written like this (neglecting some constants):

create a k vector and an x vector.

f = f(x)

for i = 1:length(k)
F(i) = 0
for j = 1:length(x)
F(i) = F(i) + f(j)*exp[-1i*x(j)*k(i)]
end
end
 
  • #3
A 2D Fourier transform is simply a series of 1D Fourier transforms. For each value of x, you Fourier transform f(x,y) along y to get f1(x,y), then for each value of y you Fourier transform f1(x,y) along x to get f2(x,y). The resulting f2 contains the 2D FT of f(x,y).

By the way, you should not use for loops in Matlab for things you can do using vector operations.
 
  • #4
DrClaude said:
A 2D Fourier transform is simply a series of 1D Fourier transforms. For each value of x, you Fourier transform f(x,y) along y to get f1(x,y), then for each value of y you Fourier transform f1(x,y) along x to get f2(x,y). The resulting f2 contains the 2D FT of f(x,y).

By the way, you should not use for loops in Matlab for things you can do using vector operations.

Why should I not? and how can I write the loop in vector operations? p.s thanks for you help it feels much clearer now.
 
  • #5
Ben Wilson said:
Why should I not? and how can I write the loop in vector operations?
For loops have an important overhead that is not present when using vector operations (the latter are optimized in Matlab). This is less a problem nowadays that Matlab does compilation on the fly (instead of being only an interpreter as it used to be), but it is still faster to use vector operations and your code looks more like Matlab code than C code.

An example from your code above: instead of
Matlab:
for j = 1:length(x)
F(i) = F(i) + f(j)*exp[-1i*x(j)*k(i)]
end
use
Matlab:
F(i) = sum(f.*exp(-1i*x*k(i)))
or
Matlab:
F(i) = dot(f,exp(-1i*x*k(i)))
 
  • #6
DrClaude said:
A 2D Fourier transform is simply a series of 1D Fourier transforms. For each value of x, you Fourier transform f(x,y) along y to get f1(x,y), then for each value of y you Fourier transform f1(x,y) along x to get f2(x,y). The resulting f2 contains the 2D FT of f(x,y).

By the way, you should not use for loops in Matlab for things you can do using vector operations.
sadly
DrClaude said:
For loops have an important overhead that is not present when using vector operations (the latter are optimized in Matlab). This is less a problem nowadays that Matlab does compilation on the fly (instead of being only an interpreter as it used to be), but it is still faster to use vector operations and your code looks more like Matlab code than C code.

An example from your code above: instead of
Matlab:
for j = 1:length(x)
F(i) = F(i) + f(j)*exp[-1i*x(j)*k(i)]
end
use
Matlab:
F(i) = sum(f.*exp(-1i*x*k(i)))
or
Matlab:
F(i) = dot(f,exp(-1i*x*k(i)))

F and f here are still functions of one variable, my initial problem is how to write the above statements (in vector or loop form) for a function of 2 variables?
 
  • #7
Ben Wilson said:
F and f here are still functions of one variable, my initial problem is how to write the above statements (in vector or loop form) for a function of 2 variables?
I told you the recipe: you need to do a series of 1D transforms. As you already know how to do one, it shouldn't be hard to extend the code to loop over the other dimension. (Note that you will probably have to use a loop here. I can't see how you could do the full 2D FT only with vector operations. If you are more comfortable with loops, it may be better to start writing the code using for loops, and then try to eliminate some later.)
 
  • #8
DrClaude said:
I told you the recipe: you need to do a series of 1D transforms. As you already know how to do one, it shouldn't be hard to extend the code to loop over the other dimension. (Note that you will probably have to use a loop here. I can't see how you could do the full 2D FT only with vector operations. If you are more comfortable with loops, it may be better to start writing the code using for loops, and then try to eliminate some later.)

In matlab, i have in 1 dimension done what you have said by using a vector to define a function as a new vector. then in my loops i do things like f(i) and x(i) and k(j).

in MATLAB one can't simply write f(x,y). how do i write f(x,y) in matlab? i.e i want to do the sums by writing what should in my head look like f( x(i), y(j) ) etc.

my actual function in my work f(x,y) can not be separated into f1(x) and f2(y) or any other parametric form.
 
  • #9
Ben Wilson said:
In matlab, i have in 1 dimension done what you have said by using a vector to define a function as a new vector. then in my loops i do things like f(i) and x(i) and k(j).

in MATLAB one can't simply write f(x,y). how do i write f(x,y) in matlab? i.e i want to do the sums by writing what should in my head look like f( x(i), y(j) ) etc.

my actual function in my work f(x,y) can not be separated into f1(x) and f2(y) or any other parametric form.
i think its the whole loop vs vector issue that's the problem which is why i want someone with MATLAB expertise to show me how my pattern of thinking is wrong for matlab
 
  • #10
Ben Wilson said:
in MATLAB one can't simply write f(x,y). how do i write f(x,y) in matlab? i.e i want to do the sums by writing what should in my head look like f( x(i), y(j) ) etc.
Just like you write a function f(x) as an array f(i), a function f(x,y) is a 2D array f(i,j).
 

Related to Matlab syntax for 2-d fourier transform

1. What is the syntax for performing a 2-d Fourier transform in Matlab?

The syntax for performing a 2-d Fourier transform in Matlab is fft2(A), where A is the input matrix.

2. Can the 2-d Fourier transform be applied to color images in Matlab?

Yes, the 2-d Fourier transform can be applied to color images in Matlab. However, the input image must first be converted to grayscale using the rgb2gray() function.

3. How can I visualize the 2-d Fourier transform of an image in Matlab?

You can visualize the 2-d Fourier transform of an image in Matlab by using the fftshift() function to center the low-frequency components, and then using abs() and log() to convert the complex numbers to a grayscale image.

4. What is the difference between the 2-d Fourier transform and the 2-d discrete Fourier transform in Matlab?

The 2-d Fourier transform in Matlab is a continuous transform, while the 2-d discrete Fourier transform is a discrete version that is used on sampled data. The 2-d discrete Fourier transform is also more efficient for computing on digital computers.

5. Can I perform a 2-d Fourier transform on a non-square image in Matlab?

Yes, Matlab allows you to perform a 2-d Fourier transform on non-square images. However, the resulting transform will also be non-square, with dimensions equal to the dimensions of the input image.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • Differential Equations
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Calculus and Beyond Homework Help
Replies
5
Views
391
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
Back
Top