python - Why does padding an FFT in NumPy make it run much slower? -
I wrote a script using NumPy's fft
function, where I used to input my input array Padding was fast to get the FFT, the closest power of 2
After the outline of the code, I came to know that FFT was taking the longest time, so I fielded around with the parameters Done and found that if I not the pad input array, FFT went on fast several times.
This is a minimal example to illustrate what I'm talking about (I went to the IPython and used the % timeit
magic at the time of execution .)
x = np.arange (-4. * Np.pi, 4. * np.pi, 1000) dat1 = np.sin (x)
Time result:
% timeit np.fft.fft (dat1) 100000 loops, best 3: 12.3 μs per loop% timeit np.fft.fft (dat1, n = 1024) 10000 loops, the best 3: 61.5 μs per loop
Padding the array in the power of 2 is going to a very tough slump. It comes
Even if I create an array with a prime number element (hence theoretically slow FFT)
x2 = np.arange (-4. Np.pi, 4. * np.pi, 1009) dat2 = np.sin (x2)
Even the time it takes to move does not change so fast!
% timeit np.fft.fft (dat2) 100000 loops, best 3: 12.2 μs per loop
I would have thought that padding an array Time will be conducted, and then FFT should be calculated faster. Am I missing anything?
Edit: I should use np.linspace
instead of np.arange
. The results of time using linspace
using [2] are: np import np [3]: x = np.linspace (- 4 * np.pi, 4 * np.pi, 1000) in [4]: x2 = np.linspace (-4 * np.pi, 4 * np.pi, 1024) [5]: dat1 = np [6]: in dat2 = np.sin (x2) [7]:% timeit np.fft.fft (dat1) 10000 loops, the best 3: 55.1 μs per loop in [8]:%. Timeit np.fft.fft (Dat2) 10000 loops, best 3: 49.4 μs per loop [9]:% timeit np.fft.fft (dat1, n = 1024) 10000 loops, best 3: 64.9 μs per loop < / Code>
Padding still causes a downturn. Could this be a local issue? That is, is it working like this because of some bidding in my sample setup?
You Np.arange
when you np.linspace
< / Pre>
[2]: x = np.arange (-4. * Np) .pi, 4. * np.pi, 1000) [3] : X out [3]: Array ([- 12.56637061])
np.arange
takes argument (start, stop, phase) while Np.linspace is
(start, stop, number_and permission). When you count with the data I suspect that you think that you are using, you get the expected behavior: <4p: 4. * np.pi, 1000) in [5]: dat1 = Np.sin (x) [6]:% timeit np.fft.fft (dat1) 1 loops, best 3: 28.1 in μs per loop [7]:% timeit np.fft.fft (dat1, n = 1024 ) 10000 loops, best in 3: 26.7 μs per loop [8]: x = np.linspace (-4. * Np.pi, 4. * Np.pi, 100 9) in [9]: dat2 = np. [10] in sin (x):% timeit np.fft.fft (dat2) 10000 loops, the best 3: 53 μs per loop [11]:% Timeit np.fft.fft (dat2, n = 1024) 10000 Loops, 3: 26.8 μs per loop
Comments
Post a Comment