-
Notifications
You must be signed in to change notification settings - Fork 0
/
dft.py
43 lines (36 loc) · 1.14 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
import math
import numpy as np
def dft2(matrix):
image = np.zeros(matrix.shape, dtype=complex)
for col in range(image.shape[1]):
image[:, col] = dft(matrix[:, col])
for row in range(image.shape[0]):
image[row, :] = dft(image[row, :])
return image
def idft2(matrix):
image = np.zeros(matrix.shape, dtype=complex)
for col in range(image.shape[1]):
image[:, col] = idft(matrix[:, col])
for row in range(image.shape[0]):
image[row, :] = idft(image[row, :])
return image
def dft(vector, N=None):
if N is None:
N = len(vector)
fourier = []
for k in range(N):
fourier.append(complex(0))
for n, x in enumerate(vector):
theta = math.tau * k * n / N
fourier[k] += x * complex(math.cos(theta), math.sin(theta))
return fourier
def idft(vector, N=None):
if N is None:
N = len(vector)
fourier = []
for k in range(N):
fourier.append(complex(0))
for n, x in enumerate(vector):
theta = - math.tau * k * n / N
fourier[k] += x * complex(math.cos(theta), math.sin(theta))
return fourier