Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I plot FFT in Numpy

This seems like a very easy question, yet I couldn't find any documentation for this.

I have an image in Numpy, and i want to imshow the FFT.

In Matlab I can just do

F = fft(myimg)
imshow(F)

I can't do the same in Numpy because F is complex valued. Trying to do imshow(real(F)) gives me an all black image - I'm guessing because in [0,1] instead of 0..255. Multiplying by 255 also doesn't fix the issue.

Any ideas on how to get my plot?

Update:

Okay, natan pointed out how I incorrectly simplified this problem. Allow me to backtrack a bit. I have a video matrix, of dimensions (200, 30, 30, 3). 200 frames, 30x30 pixels, 3 color channels. For each color channel of each pixel, I want to compute fft of that pixel across time in the series. That should give me a new matrix which is (200,30,30,3). For each pixel, across each color channel, a 200-dim temporal fourier transform of each pixel. Then I should be able to look at e.g. the image created by the values of the first coefficient of the fourier transform in each pixel.

Note that the matlab fft operates on the first nonsingleton dimension, so F = fft(video) is doing what I'm after.

like image 930
Kurt Spindler Avatar asked Mar 21 '13 07:03

Kurt Spindler


1 Answers

Here's an example for a 2D image using scipy :

from scipy import fftpack
import numpy as np
import pylab as py

# Take the fourier transform of the image.
F1 = fftpack.fft2(myimg)

# Now shift so that low spatial frequencies are in the center.
F2 = fftpack.fftshift( F1 )

# the 2D power spectrum is:
psd2D = np.abs( F2 )**2

# plot the power spectrum
py.figure(1)
py.clf()
py.imshow( psf2D )
py.show()

For a 1D trace you can see and example here...

like image 196
bla Avatar answered Oct 02 '22 09:10

bla