Skip to content

Commit 0e6126f

Browse files
Gathrosleios
authored andcommitted
Adding DFT to python and fixing code examples for FFT. (#382)
* Adding dft to python * updating cooley_tukey.md * adding code examples for cooley tukey
1 parent 8585885 commit 0e6126f

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

contents/cooley_tukey/code/python/fft.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
from cmath import exp, pi
33
from math import log2
44

5+
def dft(X):
6+
N = len(X)
7+
temp = [0]*N
8+
for i in range(N):
9+
for k in range(N):
10+
temp[i] += X[k] * exp(-2.0j*pi*i*k/N)
11+
return temp
12+
513
def cooley_tukey(X):
614
N = len(X)
715
if N <= 1:
@@ -47,5 +55,7 @@ def iterative_cooley_tukey(X):
4755

4856
Y = cooley_tukey(X)
4957
Z = iterative_cooley_tukey(X)
58+
T = dft(X)
5059

5160
print(all(abs([Y[i] - Z[i] for i in range(64)][j]) < 1 for j in range(64)))
61+
print(all(abs([Y[i] - T[i] for i in range(64)][j]) < 1 for j in range(64)))

contents/cooley_tukey/cooley_tukey.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ For some reason, though, putting code to this transformation really helped me fi
7474
{% sample lang="c" %}
7575
[import:8-19, lang:"c_cpp"](code/c/fft.c)
7676
{% sample lang="cpp" %}
77-
[import:2-11, lang:"julia"](code/julia/fft.jl)
77+
[import:23-33, lang:"c_cpp"](code/c++/fft.cpp)
7878
{% sample lang="hs" %}
7979
[import:2-11, lang:"julia"](code/julia/fft.jl)
8080
{% sample lang="py" %}
81-
[import:2-11, lang:"julia"](code/julia/fft.jl)
81+
[import:5-11, lang:"python"](code/python/fft.py)
8282
{% sample lang="scratch" %}
8383
[import:2-11, lang:"julia"](code/julia/fft.jl)
8484
{% endmethod %}
@@ -119,11 +119,11 @@ In the end, the code looks like:
119119
{% sample lang="c" %}
120120
[import:20-39, lang:"c_cpp"](code/c/fft.c)
121121
{% sample lang="cpp" %}
122-
[import:35-66, lang:"c_cpp"](code/c++/fft.cpp)
122+
[import:36-66, lang:"c_cpp"](code/c++/fft.cpp)
123123
{% sample lang="hs" %}
124124
[import:6-19, lang:"haskell"](code/haskell/fft.hs)
125125
{% sample lang="py" %}
126-
[import:5-16, lang:"python"](code/python/fft.py)
126+
[import:13-24, lang:"python"](code/python/fft.py)
127127
{% sample lang="scratch" %}
128128
[import:14-31, lang:"julia"](code/julia/fft.jl)
129129
{% endmethod %}

0 commit comments

Comments
 (0)