ND Fourier Transform in Python

In summary: I don't know how to plot this...plt.imshow(magnitude_spectrum)plt.colorbar()...but it might work better as...plt.imshow(magnitude_spectrum[0])plt.colorbar()...since the first image has all the low-frequency data, and the 2nd and 3rd are just noise.In summary, the conversation discusses the process of getting a series of 2D images and putting them through a 3D Fourier Transform using Python. The speaker is new to this process and has only a basic knowledge of Python. They have provided some code for doing a FFT for a list of point sources, but are having trouble
  • #1
EnSlavingBlair
36
6
Hi,

My aim is to get a series of images in 2D space that run over different timestamps and put them through a 3D Fourier Transform. So my 3D FT has 2 spatial axes and one temporal axis. However I have never done anything like this before, and I have a very basic knowledge of Python.

So far, I can do the FFT for a list (or 1D array) of point sources as follows:

##
# Import libs
import matplotlib.pyplot as plt
import numpy as np

# create point sources
tmp = range(100)
source = [0 for x in tmp]
source.insert(50,1)
source.insert(5,1)
source.insert(60,0.5)

# make t useable later
t = np.array(source)

# equations for later
f = np.fft.fft(t)
g = np.sqrt(np.abs(f)**2)

# set up plot
fig = plt.figure()

# add sources to plot
ax1 = fig.add_subplot(211)
plt.plot(source)
ax1.set_title('Source')
ax1.xaxis.set_visible(False)

# add FT of sources to plot
ax2 = fig.add_subplot(212)
plt.plot(f)
ax2.set_title('Fourier Transform')
ax2.xaxis.set_visible(False)

# show plot
plt.show()
##

Now I would like to turn my 1D image into a 2D image, and I just can't work out how to do this. I've tried by doing something like this for my point sources:

##
# Array of 3 source lists
# Create list of lists - seems dodgy
tmp_array = range(3)
array_list = []

# create source lists to go in array
tmp = range(10)
source1 = [0 for x in tmp]
source1.insert(5,1)
source2 = [0 for x in tmp]
source2.insert(4,1)
source3 = [0 for x in tmp]
source3.insert(2,1)

# put lists in array
array_list.insert(1,source1)
array_list.insert(2,source2)
array_list.insert(3,source3)
##

Though calling it "source" instead of "array_list" would fit better with previous code.
However it is not working and I cannot figure out why.

I was also wandering if I need to bother with getting the 2D FT working before trying the 3D, or if I can just jump forward? Not that I have any idea how to do that yet.

Thank you for your help
 
Technology news on Phys.org
  • #2
I would suggest creating arrays as numpy arrays first, not as Python lists:

For the 1d array:

t = np.zeros(103)
t[5] = 1
t[51] = 1
t[60] = 0.5

(if that was your intention)

For the 2d array:

array_list = np.zeros((3, 11))
array_list[0,5] = 1
# etc...

For a 2d fft of with real-valued input, use rfft2 or rfftn.

Note that for large FFT sizes, try to avoid a size with large prime factors, or pad out to the next largest power of 2 if that is unavoidable (using the optional size parameter). Fftpack seems to handle small prime factors like 3 or 5 OK, though.
 
  • #3
One more comment:

f = np.fft.fft(t)
g = np.sqrt(np.abs(f)**2)
The sqrt and square here are unnecessary: np.abs(f) is already sqrt(f.real**2 + f.imag**2).
 
  • #4
Thank you :)

I've gone for a slightly different approach now than above, and hitting different problems.

# A lot of this is taken from http://matplotlib.org/users/image_tutorial.html
# as well as http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_imgproc/py_transforms/py_fourier_transform/py_fourier_transform.html

##
img=mpimg.imread('stinkbug.png')
img_red = img[:,:,0]
img_green = img[:,:,1]
img_blue = img[:,:,2]
# I assume this is taking the data in each of the red, green and blue columns?

array_list = []
array_list.append(img_red)
array_list.append(img_green)
array_list.append(img_blue)

t = np.array(array_list)
f = np.fft.fftn(t)
fshift = np.fft.fftshift(f)
magnitude_spectrum = np.log(np.abs(fshift))

fig = plt.figure()

ax1 = fig.add_subplot(211)
imgplot=plt.imshow(img)
ax1.set_title('FT this Bug')
imgplot.set_cmap('gray')
plt.colorbar()

ax2 = fig.add_subplot(212)
plt.imshow(magnitude_spectrum)
plt.colorbar()

plt.show()
##

I get the following error

###
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "image_test2", line 51, in <module>
plt.imshow(magnitude_spectrum)
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2892, in imshow
imlim=imlim, resample=resample, url=url, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 7300, in imshow
im.set_data(X)
File "/usr/lib/pymodules/python2.7/matplotlib/image.py", line 429, in set_data
raise TypeError("Invalid dimensions for image data")
TypeError: Invalid dimensions for image data
##

Which I believe is because I'm trying to plot 3 images in 1 2D image, where I should be making a very thin rectangular cube. Because the images are basically the same, I expect that all the information will be in the first part and only left-over stuff in the others as I am sampling a limited sized image. I just don't know how to do that!
 
  • #5
Try plotting the red, green, and blue sections separately.

A few other comments on the code

nSlavingBlair said:
Thank you :)
img=mpimg.imread('stinkbug.png')
img_red = img[:,:,0]
img_green = img[:,:,1]
img_blue = img[:,:,2]
# I assume this is taking the data in each of the red, green and blue columns?

array_list = []
array_list.append(img_red)
array_list.append(img_green)
array_list.append(img_blue)

t = np.array(array_list)

This could more easily be achieved using rollaxis(img, 2, 0):

img_rgba = img.rollaxis(img, 2, 0)
img_rgb = img_rgba[:3]

f = np.fft.fftn(t)
fshift = np.fft.fftshift(f)
magnitude_spectrum = np.log(np.abs(fshift))

Again, for real-valued input, the fft will have a second set of redundant conjugate negative-frequency values (the shift just moves them to to the beginning of the array, IIRC). I suggest using rfftn.
 

1. What is the ND Fourier Transform in Python?

The ND Fourier Transform in Python is a mathematical tool used for analyzing the frequency components of a signal. It is a type of Fourier transform that can handle N-dimensional data, meaning it can be applied to signals with multiple variables or dimensions.

2. How is the ND Fourier Transform calculated in Python?

The ND Fourier Transform in Python can be calculated using the numpy.fft.fftn() function. This function takes in an N-dimensional array as input and returns the Fourier transform of that array as output. It uses the Fast Fourier Transform algorithm to efficiently calculate the transform.

3. What is the difference between a 1D and an ND Fourier Transform in Python?

The main difference between a 1D and an ND Fourier Transform in Python is the dimensionality of the input data. A 1D Fourier Transform is used for analyzing signals with only one variable, while an ND Fourier Transform can handle signals with multiple variables. Additionally, the 1D Fourier Transform produces a single spectrum, while the ND Fourier Transform produces a spectrum for each dimension of the input data.

4. What are some applications of the ND Fourier Transform in Python?

The ND Fourier Transform in Python has many applications in fields such as signal processing, image processing, and data analysis. It is commonly used for filtering noise from signals, analyzing the frequency content of images, and extracting features from multidimensional data.

5. Are there any limitations to using the ND Fourier Transform in Python?

One limitation of the ND Fourier Transform in Python is that it assumes the input data is periodic. This means that the signal repeats itself infinitely in time or space, which may not always be the case in real-world data. Additionally, the ND Fourier Transform may not be suitable for analyzing highly complex signals or data with irregular patterns.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
  • Advanced Physics Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
2
Views
2K
  • Advanced Physics Homework Help
Replies
7
Views
1K
Replies
4
Views
1K
  • Advanced Physics Homework Help
Replies
6
Views
1K
  • Topology and Analysis
Replies
1
Views
2K
Replies
2
Views
4K
Back
Top