Module: Muze::Core::DCT

Defined in:
lib/muze/core/dct.rb

Overview

DCT utilities.

Constant Summary collapse

BASIS_CACHE =
Muze::Core::BoundedCache.new(max_size: 64)

Class Method Summary collapse

Class Method Details

.dct(x, type: 2, n: nil, axis: 0, norm: :ortho) ⇒ Numo::SFloat

Parameters:

  • x (Numo::NArray)
  • type (Integer) (defaults to: 2)
  • n (Integer, nil) (defaults to: nil)
  • axis (Integer) (defaults to: 0)
  • norm (Symbol, nil) (defaults to: :ortho)

Returns:

  • (Numo::SFloat)

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/muze/core/dct.rb', line 16

def dct(x, type: 2, n: nil, axis: 0, norm: :ortho)
  raise Muze::ParameterError, "only DCT type 2 is supported" unless type == 2
  raise Muze::ParameterError, "axis must be 0 or 1" unless [0, 1].include?(axis)

  matrix = Numo::SFloat.cast(x)
  matrix = matrix.expand_dims(1) if matrix.ndim == 1
  matrix = matrix.transpose if axis == 1

  rows, cols = matrix.shape
  target_length = n || rows
  working = adjust_rows(matrix, target_length)
  result = basis_matrix(rows: target_length, cols: target_length, norm:).dot(working).cast_to(Numo::SFloat)

  axis == 1 ? result.transpose : result
end