Speeding up Fourier-related transform computations in python

asked 2014-02-28 06:16:23 -0600

obelix gravatar image

updated 2014-02-28 06:48:40 -0600

berak gravatar image

I have an image and I need to compute a fourier-related transform over it called Short Time Fourier Transform (for extra mathematical info check:http://en.wikipedia.org/wiki/Short-time_Fourier_transform).

In order to do that I need to :

(1) place a window at the starting pixel of the image (x,y)=(M/2,M/2)

(2) Truncate the image using this window

(3) Compute the FFT of the truncated image, save results.

(4) Incrementally slide the window to the right

(5) Go to step 3, until window reaches the end of the image

However I need to perform the aformentioned calculation in real time... But it is rather slow !!!

Is there anyway to speed up the aformentioned process ??

I also include my code:

height, width = final_frame.shape
        M=2
        for j in range(M/2, height-M/2):
            for i in range(M/2, width-M/2):
                face_win=final_frame[j-M/2:j+M/2, i-M/2:i+M/2]

                #these steps are perfomed in order to speed up the FFT calculation process
                height_win, width_win = face_win.shape
                fftheight=cv2.getOptimalDFTSize(height_win)
                fftwidth=cv2.getOptimalDFTSize(width_win)
                right = fftwidth - width_win
                bottom = fftheight - height_win
                bordertype = cv2.BORDER_CONSTANT 
                nimg = cv2.copyMakeBorder(face_win,0,bottom,0,right,bordertype, value = 0)

                dft = cv2.dft(np.float32(face_win),flags = cv2.DFT_COMPLEX_OUTPUT)
                dft_shift = np.fft.fftshift(dft)
                magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1])
edit retag flag offensive close merge delete