Skip to content

Commit ed3ec56

Browse files
authored
Make fft.py conform to PEP8 (#685)
1 parent 5bf615a commit ed3ec56

File tree

2 files changed

+43
-37
lines changed

2 files changed

+43
-37
lines changed

contents/cooley_tukey/code/python/fft.py

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,62 @@
22
from cmath import exp, pi
33
from math import log2
44

5+
56
def dft(X):
67
N = len(X)
7-
temp = [0]*N
8+
temp = [0] * N
89
for i in range(N):
910
for k in range(N):
10-
temp[i] += X[k] * exp(-2.0j*pi*i*k/N)
11+
temp[i] += X[k] * exp(-2.0j * pi * i * k / N)
1112
return temp
1213

14+
1315
def cooley_tukey(X):
14-
N = len(X)
15-
if N <= 1:
16-
return X
17-
even = cooley_tukey(X[0::2])
18-
odd = cooley_tukey(X[1::2])
19-
20-
temp = [i for i in range(N)]
21-
for k in range(N//2):
22-
temp[k] = even[k] + exp(-2j*pi*k/N) * odd[k]
23-
temp[k+N//2] = even[k] - exp(-2j*pi*k/N)*odd[k]
24-
return temp
25-
26-
def bitReverse(X):
27-
N = len(X)
28-
temp = [i for i in range(N)]
29-
for k in range(N):
30-
b = sum(1<<(int(log2(N))-1-i) for i in range(int(log2(N))) if k>>i&1)
31-
temp[k] = X[b]
32-
temp[b] = X[k]
33-
return temp
16+
N = len(X)
17+
if N <= 1:
18+
return X
19+
even = cooley_tukey(X[0::2])
20+
odd = cooley_tukey(X[1::2])
21+
22+
temp = [i for i in range(N)]
23+
for k in range(N // 2):
24+
temp[k] = even[k] + exp(-2.0j * pi * k / N) * odd[k]
25+
temp[k + N // 2] = even[k] - exp(-2.0j * pi * k / N) * odd[k]
26+
return temp
27+
28+
29+
def bit_reverse(X):
30+
N = len(X)
31+
temp = [i for i in range(N)]
32+
for k in range(N):
33+
b = sum(1 << int(log2(N)) - 1 -
34+
i for i in range(int(log2(N))) if k >> i & 1)
35+
temp[k] = X[b]
36+
temp[b] = X[k]
37+
return temp
38+
3439

3540
def iterative_cooley_tukey(X):
36-
N = len(X)
41+
N = len(X)
42+
43+
X = bit_reverse(X)
3744

38-
X = bitReverse(X)
45+
for i in range(1, int(log2(N)) + 1):
46+
stride = 2 ** i
47+
w = exp(-2.0j * pi / stride)
48+
for j in range(0, N, stride):
49+
v = 1
50+
for k in range(stride // 2):
51+
X[k + j + stride // 2] = X[k + j] - v * X[k + j + stride // 2]
52+
X[k + j] -= X[k + j + stride // 2] - X[k + j]
53+
v *= w
54+
return X
3955

40-
for i in range(1, int(log2(N)) + 1):
41-
stride = 2**i
42-
w = exp(-2j*pi/stride)
43-
for j in range(0, N, stride):
44-
v = 1
45-
for k in range(stride//2):
46-
X[k + j + stride//2] = X[k + j] - v*X[k + j + stride//2];
47-
X[k + j] -= (X[k + j + stride//2] - X[k + j]);
48-
v *= w;
49-
return X
5056

5157
X = []
5258

5359
for i in range(64):
54-
X.append(random())
60+
X.append(random())
5561

5662
Y = cooley_tukey(X)
5763
Z = iterative_cooley_tukey(X)

contents/cooley_tukey/cooley_tukey.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ For some reason, though, putting code to this transformation really helped me fi
8080
{% sample lang="hs" %}
8181
[import:7-13, lang:"haskell"](code/haskell/fft.hs)
8282
{% sample lang="py" %}
83-
[import:5-11, lang:"python"](code/python/fft.py)
83+
[import:6-12, lang:"python"](code/python/fft.py)
8484
{% sample lang="scratch" %}
8585
[import:4-13, lang:"julia"](code/julia/fft.jl)
8686
{% sample lang="asm-x64" %}
@@ -131,7 +131,7 @@ In the end, the code looks like:
131131
{% sample lang="hs" %}
132132
[import:15-28, lang:"haskell"](code/haskell/fft.hs)
133133
{% sample lang="py" %}
134-
[import:13-24, lang:"python"](code/python/fft.py)
134+
[import:15-26, lang:"python"](code/python/fft.py)
135135
{% sample lang="scratch" %}
136136
[import:16-32, lang:"julia"](code/julia/fft.jl)
137137
{% sample lang="asm-x64" %}

0 commit comments

Comments
 (0)