Skip to content

Calling fft from C

4 messages · Gabor Grothendieck, Gunnar Hellmund, Matt Shotwell

#
Hi

I have made a R function 'convolve2' for convolution of two real
valued vectors based on Rs 'convolve' with option type="open" - see
below.
(exp.length and irf.length are variables set in another part of the program)

I wish to implement the function convolve2 in C and use it in a
function used from R with .Call - e.g. I need to call fft in C.
All I can find in the source code is do_fft in Internals.h - but how
do I use do_fft? Or should I call another C routine (and how)?

How do I solve the problem in the most appropriate way?

convolve2=function2(x,y)
{
x<- c(rep.int(0,exp.length-1),x)
n <- length(y<-c(y, rep.int(0, irf.length -1)))
x <- fft(fft(x) * Conj(fft(y)), inverse=TRUE)
return(Re(x)/n)
}
#
The Writing R Extensions manual specifically uses convolve as an
example of calling C from R.
On Sun, May 30, 2010 at 2:08 PM, Gunnar Hellmund <ghellmund at gmail.com> wrote:
#
Thanks for the reply, but ...

No, I am not interested in the convolution function defined in the
R-extensions manual.
I want to use a fast fourier transform version implemented in C
returning values to another C function.
So the question is: How do I call the fft supplied by R in C? (just
like it is possible to use other R functions in C using #include
<Rmath.h> etc)

Best,
Gunnar

On Sun, May 30, 2010 at 8:12 PM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:

  
    
#
The fft in R has the following C call chain

file                      purpose
src/main/names.c          links R function "fft" to do_fft
src/main/fourier.c        do_fft calls fft_factor, fft_work
src/appl/fft.c            home of fft_factor, fft_work

If you want to use the fft at a low level, you should use fft_factor and
fft_work. You can read about these functions in the fft.c file. In order
to use these functions in an R package, you must use the definitions in
the R/R_ext/Applic.h header. 

However, I believe the time limiting factor in an fft call is the work
performed by the fft_factor and fft_work functions. Hence, you are not
likely to save much time by calling them directly.

BTW, I think this sort of question is better suited for the R-devel
mailing list.

Matt Shotwell
Graduate Student
Division of Biostatistics and Bioinformatics 
Medical University of South Carolina
On Sun, 2010-05-30 at 21:35 +0200, Gunnar Hellmund wrote: