module Numeric.Transform.Fourier.PFA (fft_pfa) where
import Data.List (transpose)
import Data.Array
import Data.Complex
fft_pfa :: (Ix a, Integral a, RealFloat b) => Array a (Complex b)
-> a
-> a
-> (Array a (Complex b) -> Array a (Complex b))
-> Array a (Complex b)
fft_pfa a l m fft = array (0,n1) $ zip ks (elems x')
where x = listArray ((0,0),(l1,m1)) [ a!i | i <- xs ]
f = listArray ((0,0),(l1,m1)) (flatten_rows $ map fft $ rows x)
x' = listArray ((0,0),(l1,m1)) (flatten_cols $ map fft $ cols f)
(xs,ks) = pfa_index_map l m
n = l * m
pfa_index_map :: (Integral a) => a -> a -> ([a],[a])
pfa_index_map l m = (ns,ks)
where ns = [ (m * n1 + l * n2) `mod` n | n1 <- [0..(l1)], n2 <- [0..(m1)] ]
ks = [ (c * m * k1 + d * l * k2) `mod` n | k1 <- [0..(l1)], k2 <- [0..(m1)] ]
c = find_inverse m l
d = find_inverse l m
n = l * m
find_inverse :: (Integral a) => a -> a -> a
find_inverse a0 n0 = find_inverse' a0 n0 1
where find_inverse' a n a' | (a*a') `mod` n == 1 = a'
| otherwise = find_inverse' a n (a'+1)
rows :: (Ix a, Integral a, RealFloat b) => Array (a,a) (Complex b) -> [Array a (Complex b)]
rows x = [ listArray (0,m) [ x!(i,j) | j <- [0..m] ] | i <- [0..l] ]
where ((_,_),(l,m)) = bounds x
cols :: (Ix a, Integral a, RealFloat b) => Array (a,a) (Complex b) -> [Array a (Complex b)]
cols x = [ listArray (0,l) [ x!(i,j) | i <- [0..l] ] | j <- [0..m] ]
where ((_,_),(l,m)) = bounds x
flatten_rows :: (Ix a, Integral a, RealFloat b) => [Array a (Complex b)] -> [(Complex b)]
flatten_rows a = foldr (++) [] $ map elems a
flatten_cols :: (Ix a, Integral a, RealFloat b) => [Array a (Complex b)] -> [(Complex b)]
flatten_cols a = foldr (++) [] $ transpose $ map elems a