0

In Repa (Haskell) is there any way to perform row operations directly?

I want to do an fft of each row of a 2D array but this does not seem to be possible. As I do not want to write my own fft algorithm, it would be nice if I can apply fft (from Repa.FFTW) to each row directly resulting in a matrix of the same size as the original.

I tried to write my own dft function but the performance was very bad. The code for this is below.

dftOnMatrix :: Array D DIM2 Complex -> Array D DIM2 Complex
dftOnMatrix arr =
  let (Z :. rs :. cs) = extent arr
      dftFunc :: (DIM2 -> Complex) -> Int -> Int -> Int -> Complex -> Complex
      dftFunc f i j k sum = dftFuncCore k sum
        where
          val2 = fromIntegral j
          temp = 2*pi/ fromIntegral cs
          dftFuncCore k sum = 
            if k == cs
            then sum
            else let val1 = fromIntegral k
                     val = val1*val2*temp
                     cosval = ( - (cos val))
                     sinval = ( - (sin val))
                 in --cosval `seq` (sinval `seq` (dftFuncCore (k+1) (sum + ((f (Z :. i :. k))* (cos val, sin val)))))
                   dftFuncCore (k+1) (sum + ((f (Z :. i :. k))* (cos val, sin val)))
  in R.traverse arr id (\f (Z :. i :. j) -> (1/fromIntegral cs, 0) * dftFunc f i j 0 0)

I have tried to analyze this using ghc-core but did not find anything that might affect performance. I might be completely wrong though as my knowledge of core is very limited.

Thanks,

jpv
  • 101
  • You could look into the [implementation](https://hackage.haskell.org/package/repa-fftw-3.2.3.2/docs/src/Data-Array-Repa-FFTW.html#fft) of repa.fftw to see how to get a pointer to the array's data. Then you could use one of the more general functions of Math.FFT to perform your transform. – Lemming Jun 09 '16 at 11:19
  • Thanks, i will look at it. I was little bit uncomfortable as this code uses unsafePerformIO but I need to learn how to use it correctly. – jpv Jun 10 '16 at 09:09

0 Answers0