Skip to content

Commit

Permalink
Added DFT and re-wrote FFT (algorithm-archivists#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiegillet authored and leios committed Oct 30, 2018
1 parent 2fa064d commit 95a2811
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
40 changes: 25 additions & 15 deletions contents/cooley_tukey/code/haskell/fft.hs
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
import Data.Complex
import Data.Array
import Data.Ratio
import Data.List (partition)
import Data.Map ((!))
import qualified Data.Map as M
import Data.Ratio

dft :: [Complex Double] -> [Complex Double]
dft x = matMult dftMat x
where
n = length x
w = exp $ (-2) * pi * (0 :+ 1) / fromIntegral n
dftMat = [[w ^ (j * k) | j <- [0 .. n - 1]] | k <- [0 .. n - 1]]
matMult m x = map (sum . zipWith (*) x) m

fft :: [Complex Double] -> [Complex Double]
fft x = let n = length x
i = 0 :+ 1
w = M.fromList [(k%n, exp ((-2)*pi*i*(fromIntegral k)/(fromIntegral n)) ) | k<-[0..n-1]]
arr = fft' n w (listArray (0,n-1) x)
in [arr!k | k<-[0..n-1]]
fft x = fft' x
where
fft' 1 _ x = x
fft' n w x = let n2 = div n 2
e = fft' n2 w (listArray (0, n2-1) [x!k | k<-[0,2..n-1]])
o = fft' n2 w (listArray (0, n2-1) [x!k | k<-[1,3..n-1]])
in array (0, n-1) $ concat [[(k, e!k + o!k * w M.!(k%n)),
(k + n2, e!k - o!k * w M.!(k%n))]
| k <- [0..n2-1]]
n = length x
w0 = exp ((-2) * pi * (0 :+ 1) / fromIntegral n)
w = M.fromList [(k % n, w0 ^ k) | k <- [0 .. n - 1]]
fft' [x] = [x]
fft' x =
let (evens, odds) = partition (even . fst) $ zip [0 ..] x
e = fft' $ map snd evens
o = fft' $ map snd odds
x1 = zipWith3 (\e o k -> e + o * w ! (k %n)) e o [0 ..]
x2 = zipWith3 (\e o k -> e - o * w ! (k %n)) e o [0 ..]
in x1 ++ x2

main = do
print $ fft [0,1,2,3]
print $ dft [0, 1, 2, 3]
print $ fft [0, 1, 2, 3]
4 changes: 2 additions & 2 deletions contents/cooley_tukey/cooley_tukey.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ For some reason, though, putting code to this transformation really helped me fi
{% sample lang="cpp" %}
[import:23-33, lang:"c_cpp"](code/c++/fft.cpp)
{% sample lang="hs" %}
[import:4-13, lang:"julia"](code/julia/fft.jl)
[import:7-13, lang:"haskell"](code/haskell/fft.hs)
{% sample lang="py" %}
[import:5-11, lang:"python"](code/python/fft.py)
{% sample lang="scratch" %}
Expand Down Expand Up @@ -125,7 +125,7 @@ In the end, the code looks like:
{% sample lang="cpp" %}
[import:36-66, lang:"c_cpp"](code/c++/fft.cpp)
{% sample lang="hs" %}
[import:6-19, lang:"haskell"](code/haskell/fft.hs)
[import:15-28, lang:"haskell"](code/haskell/fft.hs)
{% sample lang="py" %}
[import:13-24, lang:"python"](code/python/fft.py)
{% sample lang="scratch" %}
Expand Down

0 comments on commit 95a2811

Please sign in to comment.