# Fourier transform processor. FFT's data in the physical domain and
# corrects for the peculiarities of numpy's fft algorithm.
# First we fft the data, then we take the negative sign of every other
# mode in each direction (x and y), then we
# divide the bad boy by (sqrt(res))^2 to get the correct intensity scaling,
# finally we fftshift everything.
from numpy.random import rand
from numpy import exp, sqrt, log, tan, pi, floor, zeros, ones
from scipy.special import gammaln
import numpy as np
import scipy as scipy
[docs]def FFT(f):
shape = f.shape;
res=max(shape);
if shape[0] == shape[1]:
F= np.fft.fft2(f);
F=F/(res);
F[1:res:2,:]=-F[1:res:2,:];
F[:,1:res:2]=-F[:,1:res:2];
else:
F = np.fft.fft(f,axis=0);
F = F.ravel('F'); #create 1-d array, colum-major order (matlab style), not really nice
F[1:res:2] =-F[1:res:2];
F = F.reshape(shape[1],shape[0]).T; #back to original shape
F=F/np.sqrt(res);
return np.fft.fftshift(F);
[docs]def fft(a):
"""
fft(a)
Compute the one-dimensional discrete Fourier transform of a the way Matlab
does. When a is a vector, the Fourier transform of the vector is
returned. When a is a matrix, each column vector of a is
transformed individually, and a new matrix containing the transformed
column vectors of a is returned.
Parameters
----------
a : array_like
1-D or 2-D input array (can be complex)
Returns
-------
result : ndarray
1-D or 2-D array of similar shape and type
containing the discrete fourier transform of a
See Also
--------
ifft
Notes
-----
Using the Numpy function fft on a matrix does not
produce results similar to what Matlab does.
This helper function uses the Numpy functions to produce
a resut that agrees with what Matlab does.
"""
return transformVectors(a, np.fft.fft)
[docs]def ifft(a):
"""
ifft(a)
Compute the one-dimensional inverse discrete Fourier transform the
way Matlab does. When a is a vector, the inverse Fourier transform
of the vector is returned. When a is a matrix, each column vector
of a is transformed individually, and a new matrix containing the
transformed column vectors of a is returned.
Parameters
----------
a : array_like
1-D or 2-D input array (can be complex)
Returns
-------
out : ndarray
1-D or 2-D array of similar shape and type
containing the inverse discrete fourier transform of a
See Also
--------
fft
Notes
-----
Using the Numpy function ifft on a matrix does not
produce results similar to what Matlab does.
This helper function uses the Numpy functions to produce
a resut that agrees with what Matlab does.
"""
return transformVectors(a, np.fft.ifft)