Class: NMatrix

Inherits:
Object
  • Object
show all
Defined in:
lib/nmatrix/fftw.rb

Defined Under Namespace

Modules: FFTW

Instance Method Summary collapse

Instance Method Details

#fftNMatrix

Compute 1D FFT of the matrix using FFTW default parameters.

Examples:

Compute 1D FFT of an NMatrix.

nm = NMatrix.new([10],
  [
    Complex(9.32,0), Complex(44,0), Complex(125,0), Complex(34,0),
    Complex(31,0),   Complex(44,0), Complex(12,0),  Complex(1,0),
    Complex(53.23,0),Complex(-23.23,0)
  ], dtype: :complex128)
nm.fft

Returns:

  • (NMatrix)

    NMatrix of dtype :complex128 containing computed values.



45
46
47
48
49
50
51
# File 'lib/nmatrix/fftw.rb', line 45

def fft
  input = self.dtype == :complex128 ? self : self.cast(dtype: :complex128)
  plan  = NMatrix::FFTW::Plan.new([self.size])
  plan.set_input input
  plan.execute
  plan.output
end

#fft2NMatrix

Compute 2D FFT of a 2D matrix using FFTW default parameters.

Returns:

  • (NMatrix)

    NMatrix of dtype :complex128 containing computed values.

Raises:

  • (ShapeError)


55
56
57
58
59
60
61
62
# File 'lib/nmatrix/fftw.rb', line 55

def fft2
  raise ShapeError, "Shape must be 2 (is #{self.shape})" if self.shape.size != 2
  input = self.dtype == :complex128 ? self : self.cast(dtype: :complex128)
  plan  = NMatrix::FFTW::Plan.new(self.shape, dim: 2)
  plan.set_input input
  plan.execute
  plan.output
end