algorithm-in-python/math/fft.py

109 lines
2.6 KiB
Python
Raw Normal View History

2019-06-11 10:51:46 +08:00
import numpy as np
2019-06-11 12:48:40 +08:00
def _fft_n2(a, invert):
'''O(n^2)'''
N = len(a)
w = np.arange(N)
i = 2j if invert else -2j
m = w.reshape((N, 1)) * w
W = np.exp(m * i * np.pi / N)
return np.concatenate(np.dot(W, a.reshape((N,
1)))) # important, cannot use *
2019-06-11 10:51:46 +08:00
def _fft(a, invert=False):
2019-06-11 12:48:40 +08:00
'''recursion version'''
2019-06-11 10:51:46 +08:00
N = len(a)
if N == 1:
return [a[0]]
2019-06-11 12:48:40 +08:00
elif N & (N - 1) == 0: # O(nlogn), 2^k
2019-06-11 10:51:46 +08:00
even = _fft(a[::2], invert)
odd = _fft(a[1::2], invert)
i = 2j if invert else -2j
factor = np.exp(i * np.pi * np.arange(N // 2) / N)
prod = factor * odd
return np.concatenate([even + prod, even - prod])
2019-06-11 12:48:40 +08:00
else:
return _fft_n2(a, invert)
def _fft2(a, invert=False):
''' iteration version'''
def rev(x):
ret = 0
for i in range(r):
ret <<= 1
if x & 1:
ret += 1
x >>= 1
return ret
N = len(a)
if N & (N - 1) == 0: # O(nlogn), 2^k
r = int(np.log(N))
c = np.array(a,dtype='complex')
i = 2j if invert else -2j
w = np.exp(i * np.pi / N)
for h in range(r - 1, -1, -1):
p = 2**h
z = w**(N / p / 2)
for k in range(N):
if k % p == k % (2 * p):
c[k], c[k + p] = c[k] + c[k + p], c[k] * z**(k % p)
return np.asarray([c[rev(i)] for i in range(N)])
else: # O(n^2)
return _fft_n2(a, invert)
2019-06-11 10:51:46 +08:00
def fft(a):
'''fourier[a]'''
n = len(a)
2019-06-11 12:48:40 +08:00
if n == 0:
raise Exception("[Error]: Invalid length: 0")
2019-06-11 10:51:46 +08:00
return _fft(a)
def ifft(a):
'''invert fourier[a]'''
n = len(a)
2019-06-11 12:48:40 +08:00
if n == 0:
raise Exception("[Error]: Invalid length: 0")
2019-06-11 10:51:46 +08:00
return _fft(a, True) / n
def fft2(arr):
2019-06-11 12:48:40 +08:00
return np.apply_along_axis(fft, 0,
np.apply_along_axis(fft, 1, np.asarray(arr)))
2019-06-11 10:51:46 +08:00
def ifft2(arr):
2019-06-11 12:48:40 +08:00
return np.apply_along_axis(ifft, 0,
np.apply_along_axis(ifft, 1, np.asarray(arr)))
2019-06-11 10:51:46 +08:00
def test(n=128):
2019-06-11 12:48:40 +08:00
print('\nsequence length:', n)
2019-06-11 10:51:46 +08:00
print('fft')
li = np.random.random(n)
print(np.allclose(fft(li), np.fft.fft(li)))
print('ifft')
li = np.random.random(n)
print(np.allclose(ifft(li), np.fft.ifft(li)))
print('fft2')
li = np.random.random(n * n).reshape((n, n))
print(np.allclose(fft2(li), np.fft.fft2(li)))
print('ifft2')
li = np.random.random(n * n).reshape((n, n))
print(np.allclose(ifft2(li), np.fft.ifft2(li)))
if __name__ == '__main__':
2019-06-11 12:48:40 +08:00
for i in range(1, 4):
test(i * 16)