github.com/noriah/catnip@v1.8.5/fft/fftw.go (about)

     1  //go:build cgo && (withfftw || fftw)
     2  
     3  package fft
     4  
     5  // This only included bindings are those that are needed by catnip.
     6  // This includes the use of `fftw_plan_dft_r2c_2d`.
     7  // It is the only fftw plan we need, and the only one we have chosen to
     8  // implement here.
     9  
    10  // #cgo pkg-config: fftw3
    11  // #include <fftw3.h>
    12  import "C"
    13  
    14  import (
    15  	"runtime"
    16  	"unsafe"
    17  )
    18  
    19  // FFTW is true if Catnip is built with cgo.
    20  const FFTW = true
    21  
    22  // Plan holds an FFTW C plan
    23  type Plan struct {
    24  	input  []float64
    25  	output []complex128
    26  	cPlan  C.fftw_plan
    27  }
    28  
    29  // Init sets up the plan so we dont run checks during execute
    30  func (p *Plan) init() {
    31  	if p.cPlan == nil {
    32  		p.cPlan = C.fftw_plan_dft_r2c_1d(
    33  			C.int(len(p.input)),
    34  			(*C.double)(unsafe.Pointer(&p.input[0])),
    35  			(*C.fftw_complex)(unsafe.Pointer(&p.output[0])),
    36  			C.FFTW_MEASURE,
    37  		)
    38  
    39  		runtime.SetFinalizer(p, (*Plan).destroy)
    40  	}
    41  }
    42  
    43  // Execute runs the plan
    44  func (p *Plan) Execute() {
    45  	C.fftw_execute(p.cPlan)
    46  }
    47  
    48  // destroy releases resources
    49  func (p *Plan) destroy() {
    50  	C.fftw_destroy_plan(p.cPlan)
    51  }