How to get N-Points FFT in R?

3

Hi, I have a question about FFT, I'm looking for a similar method to numpy.fft.rfft . I would like to know if anyone knows an R method that calculates the N-Points FFT . The following is the code with only normal fft.

link

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])

A:

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
    
asked by anonymous 01.05.2018 / 15:45

1 answer

3

I was able to create an R function similar to numpy.fft.rfft . I will leave question because this answer can be useful for someone. In Python the following operation generates its 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 section in 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
    
01.05.2018 / 22:51