-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdft.py
161 lines (127 loc) · 4.7 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import numpy as np
import matplotlib
class DFT:
@staticmethod
def slow_one_dimension(a):
a = np.asarray(a, dtype=complex)
N = a.shape[0]
res = np.zeros(N, dtype=complex)
for k in range(N):
for n in range(N):
res[k] += a[n] * np.exp(-2j * np.pi * k * n / N)
return res
@staticmethod
def slow_one_dimension_inverse(a):
a = np.asarray(a, dtype=complex)
N = a.shape[0]
res = np.zeros(N, dtype=complex)
for n in range(N):
for k in range(N):
res[n] += a[k] * np.exp(2j * np.pi * k * n / N)
res[n] /= N
return res
@staticmethod
def fast_one_dimension(a):
a = np.asarray(a, dtype=complex)
N = a.shape[0]
if N % 2 != 0:
raise AssertionError("size of a must be a power of 2")
elif N <= 16:
return DFT.slow_one_dimension(a)
else:
even = DFT.fast_one_dimension(a[::2])
odd = DFT.fast_one_dimension(a[1::2])
res = np.zeros(N, dtype=complex)
half_size = N//2
for n in range(N):
res[n] = even[n % half_size] + \
np.exp(-2j * np.pi * n / N) * odd[n % half_size]
return res
@staticmethod
def fast_one_dimension_inverse(a):
a = np.asarray(a, dtype=complex)
N = a.shape[0]
if N % 2 != 0:
raise AssertionError("size of a must be a power of 2")
elif N <= 16:
return DFT.slow_one_dimension_inverse(a)
else:
even = DFT.fast_one_dimension_inverse(a[::2])
odd = DFT.fast_one_dimension_inverse(a[1::2])
res = np.zeros(N, dtype=complex)
half_size = N//2
for n in range(N):
res[n] = half_size * even[n % half_size] + \
np.exp(2j * np.pi * n / N) * half_size * odd[n % half_size]
res[n] /= N
return res
@staticmethod
def slow_two_dimension(a):
a = np.asarray(a, dtype=complex)
N, M = a.shape
res = np.zeros((N, M), dtype=complex)
for k in range(N):
for l in range(M):
for m in range(M):
for n in range(N):
res[k, l] += a[n, m] * \
np.exp(-2j * np.pi * ((l * m / M) + (k * n / N)))
return res
@staticmethod
def slow_two_dimension_inverse(a):
a = np.asarray(a, dtype=complex)
N, M = a.shape
res = np.zeros((N, M), dtype=complex)
for k in range(N):
for l in range(M):
for m in range(M):
for n in range(N):
res[k, l] += a[n, m] * \
np.exp(2j * np.pi * ((l * m / M) + (k * n / N)))
res[k, l] /= M * N
return res
@staticmethod
def fast_two_dimension(a):
a = np.asarray(a, dtype=complex)
N, M = a.shape
res = np.zeros((N, M), dtype=complex)
for col in range(M):
res[:, col] = DFT.fast_one_dimension(a[:, col])
for row in range(N):
res[row, :] = DFT.fast_one_dimension(res[row, :])
return res
@staticmethod
def fast_two_dimension_inverse(a):
a = np.asarray(a, dtype=complex)
N, M = a.shape
res = np.zeros((N, M), dtype=complex)
for row in range(N):
res[row, :] = DFT.fast_one_dimension_inverse(a[row, :])
for col in range(M):
res[:, col] = DFT.fast_one_dimension_inverse(res[:, col])
return res
@staticmethod
def test():
# one dimension
a = np.random.random(1024)
fft = np.fft.fft(a)
# two dimensions
a2 = np.random.rand(32, 32)
fft2 = np.fft.fft2(a2)
tests = (
(DFT.slow_one_dimension, a, fft),
(DFT.slow_one_dimension_inverse, fft, a),
(DFT.fast_one_dimension, a, fft),
(DFT.fast_one_dimension_inverse, fft, a),
(DFT.slow_two_dimension, a2, fft2),
(DFT.slow_two_dimension_inverse, fft2, a2),
(DFT.fast_two_dimension, a2, fft2),
(DFT.fast_two_dimension_inverse, fft2, a2)
)
for method, args, expected in tests:
if not np.allclose(method(args), expected):
print(args)
print(method(args))
print(expected)
raise AssertionError(
"{} failed the test".format(method.__name__))