-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdft.py
executable file
·50 lines (45 loc) · 2.07 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
#!/usr/bin/env python3
import math
import random
import threading
import time
def dft(inreal, inimage, outreal, outimage):
n = len(inreal)
for i in range(0, n):
sumreal = 0
sumimage = 0
for t in range(0, n):
angle = float(2 * math.pi * t * i / n);
sumreal += float(float(inreal[t]) * float(math.cos(angle)) + float(inimage[t]) * float(math.sin(angle)))
sumimage += float(float(-inreal[t]) * float(math.sin(angle)) + float(inimage[t]) * float(math.cos(angle)))
outreal[i] = float(sumreal)
outimage[i] = float(sumimage)
return outreal, outimage
inreal = [random.randint(1,8) for i in range(0,128)]
inimage = [0 for i in range(0,128)]
outreal = [0 for i in range(0,128)]
outimage = [0 for i in range(0,128)]
start_time = time.time()
outreal,outimage = dft(inreal,inimage,outreal,outimage)
print ("non-threaded dft took {} seconds".format(time.time() - start_time))
def dft_threading(inreal, inimage, outreal, outimage, start_index, end_index):
n = len(inreal)
for i in range(start_index, end_index):
sumreal = 0
sumimage = 0
for t in range(start_index, end_index):
angle = float(2 * math.pi * t * i / n);
sumreal += float(float(inreal[t]) * float(math.cos(angle)) + float(inimage[t]) * float(math.sin(angle)))
sumimage += float(float(-inreal[t]) * float(math.sin(angle)) + float(inimage[t]) * float(math.cos(angle)))
outreal[i] = float(sumreal)
outimage[i] = float(sumimage)
return outreal, outimage
threads = []
start_time = time.time()
for i in range(0,4):
t = threading.Thread(name="thread%d" % i, target=dft_threading, args=(inreal, inimage, outreal, outimage, int((len(inreal) / 4) * i), int((len(inreal) / 4) * i + (len(inreal) / 4)) if i == 0 else int((len(inreal) / 4) * i + (len(inreal) / 4)) - 1 ))
t.start()
threads.append(t)
for t in threads:
t.join()
print ("threaded dft took {} seconds".format(time.time() - start_time))