-
Notifications
You must be signed in to change notification settings - Fork 88
/
dft.py
119 lines (95 loc) · 2.93 KB
/
dft.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
''' mbinary
#########################################################################
# File : fft.py
# Author: mbinary
# Mail: zhuheqin1@gmail.com
# Blog: https://mbinary.xyz
# Github: https://github.com/mbinary
# Created Time: 2019-06-11 12:48
# Description:
#########################################################################
'''
import numpy as np
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 *
def _fft(a, invert=False):
'''recursion version'''
N = len(a)
if N == 1:
return [a[0]]
elif N & (N - 1) == 0: # O(nlogn), 2^k
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])
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)
def fft(a):
'''fourier[a]'''
n = len(a)
if n == 0:
raise Exception("[Error]: Invalid length: 0")
return _fft(a)
def ifft(a):
'''invert fourier[a]'''
n = len(a)
if n == 0:
raise Exception("[Error]: Invalid length: 0")
return _fft(a, True) / n
def fft2(arr):
return np.apply_along_axis(fft, 0,
np.apply_along_axis(fft, 1, np.asarray(arr)))
def ifft2(arr):
return np.apply_along_axis(ifft, 0,
np.apply_along_axis(ifft, 1, np.asarray(arr)))
def test(n=128):
print('\nsequence length:', n)
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__':
for i in range(1, 4):
test(i * 16)