2

I'm trying to convert R code into scala, and i notice a difference beetween breeze and R when i perform an inverse fourier transform. ( fft in R ; inverseFourierTransform in breeze ).

In Scala Breeze

val vec = new DenseVector(Array(1.0,2.0,3.0,4.0))
val res = inverseFourierTransform(vec)

// res = ( 2.5 ; -0.5 - 0.5i ; -0.5 ; -0.5 + 0.5i )

In R

vec <- c(1,2,3,4)
res <- fft(vec, inverse = TRUE)

# res = ( 10 ; -2 - 2i ; -2 ; -2 + 2i ) 

We can observe a difference by a factor 4 beetween two results

Where did she come from ?

KyBe
  • 842
  • 1
  • 14
  • 33

1 Answers1

2

According to the help file of fft, the inverse should be calculated with:

fft(x, inverse = TRUE)/length(x)

So in your case:

vec <- c(1,2,3,4)
res <- fft(vec, inverse = TRUE)/length(vec)
res
# [1]  2.5+0.0i -0.5-0.5i -0.5+0.0i -0.5+0.5i

Or using pracma package:

pracma::ifft(vec)
# [1]  2.5+0.0i -0.5-0.5i -0.5+0.0i -0.5+0.5i

A note in ifft help page specifies that it is Almost an alias for R's fft(x, inverse=TRUE), but dividing by length(x)