How to get the N-Points FFT in R?

Asked

Viewed 54 times

3

Hi, I have a question about FFT, I am seeking a similar method to numpy.fft.rfft. I wonder if anyone knows a way of calculating the R N-Points FFT. The following is code with only normal fft.

https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.fft.rfft.html

Python:

import numpy
s = [2,4,5,6,7,12,9,8,5,0,2,5,34,1,7,26,6,3,9,1,9,36,12, 23,85,34,19,5,2,7,0,6,23,3,85,2,3,6,4,65,3,2,3,17,41,64,63,61,19,5]
numpy.absolute(numpy.fft.fft(s))

output:

array([ 859.        ,  168.68052632,  233.59258118,  119.29998998,
        261.87777496,  291.21753814,  128.36292786,  217.8378272 ,
        134.5839184 ,  176.3348246 ,  137.5572311 ,  187.26724562,
         54.49667655,  108.90964419,   59.57985882,  104.05453129,
        148.33968185,   99.25844937,   95.07400386,  193.03233757,
        208.02646027,  142.81875361,   78.02947137,  121.82303446,
         99.68797071,   55.        ,   99.68797071,  121.82303446,
         78.02947137,  142.81875361,  208.02646027,  193.03233757,
         95.07400386,   99.25844937,  148.33968185,  104.05453129,
         59.57985882,  108.90964419,   54.49667655,  187.26724562,
        137.5572311 ,  176.3348246 ,  134.5839184 ,  217.8378272 ,
        128.36292786,  291.21753814,  261.87777496,  119.29998998,
        233.59258118,  168.68052632])

R:

s = c(2,4,5,6,7,12,9,8,5,0,2,5,34,1,7,26,6,3,9,1,9,36,12, 23,85,34,19,5,2,7,0,6,23,3,85,2,3,6,4,65,3,2,3,17,41,64,63,61,19,5)
abs(fft(s))

output:

859 168.680526321364 233.592581181804 119.29998998253 261.877774963774 291.217538135653 128.362927855927 217.83782719649 134.583918396054 176.334824604301 137.557231102276 187.267245619583 54.4966765477317 108.909644186516 59.579858824858 104.054531291096 148.339681846157 99.2584493737003 95.0740038555544 193.032337566808 208.026460267619 142.818753605427 78.029471367604 121.823034461609 99.6879707119806 55 99.6879707119805 121.823034461609 78.029471367604 142.818753605427 208.026460267619 193.032337566808 95.0740038555545 99.2584493737003 148.339681846157 104.054531291096 59.579858824858 108.909644186516 54.4966765477317 187.267245619583 137.557231102276 176.334824604301 134.583918396054 217.83782719649 128.362927855927 291.217538135653 261.877774963774 119.29998998253 233.592581181804 168.680526321365
  • 1

    I did not understand the doubt. It seems to me that the results of numpy.absolute(numpy.fft.fft(s)) and abs(fft(s)) are identical (except for some rounding errors). Both have length equal to 50 and the sum of the elements does not differ until the 12a. decimal place.

  • @Marcusnunes any(abs(numpy - R) > .Machine$double.eps^0.5) gives [1] FALSE. (numpy and R are the vectors above.)

  • 1

    I circled the command here max(abs(numpy - R)) and the result was 4.858045e-09. That is, the maximum difference for one of the positions (in this case, the 15th) is in the ninth decimal place. I wouldn’t worry about it. The difference is derisory. Besides, so much R how much python are famous for rounding errors (for example, turn (2-1.8) == .2 in the R and see that he reports FALSE). Another way to test and really make sure that the vectors are equal, use all.equal(numpy, R) and see that the answer is TRUE, indicating that these vectors are approximately equal.

  • I was looking for a function that got the N-Points FFT, I was showing above that the code works to get the FFT normally, but I want a function similar to rfft, in which I pass the N points. But I figured out how to do.

1 answer

3


I was able to create a function in R similar to numpy.fft.rfft. I’ll leave you with a question because that answer might be useful to someone. In Python the following operation generates the corresponding output when n = 8:

import numpy
s = [2,4,5,6,7,12,9,8,5,0,2,5,34,1,7,26,6,3,9,1,9,36,12, 23,85,34,19,5,2,7,0,6,23,3,85,2,3,6,4,65,3,2,3,17,41,64,63,61,19,5]
numpy.absolute(numpy.fft.rfft(s,8))

Output:

array([ 53.        ,  14.42203   ,   5.38516481,   3.16307613,   7.        ])

The stretch on R is:

rfft <- function(x, n){
    mag <- abs(fft(x[1:n]))
    n <- ceiling((n + 1) / 2)
    mag[1:n]
}

s = c(2,4,5,6,7,12,9,8,5,0,2,5,34,1,7,26,6,3,9,1,9,36,12, 23,85,34,19,5,2,7,0,6,23,3,85,2,3,6,4,65,3,2,3,17,41,64,63,61,19,5)
rfft(s,8)

Output:

53 14.4220300015676 5.3851648071345 3.163076134696 7

Browser other questions tagged

You are not signed in. Login or sign up in order to post.