Class: DSP::Dct::Two

Inherits:
Object
  • Object
show all
Extended by:
Prepared
Defined in:
lib/dsprb/dct.rb,
sig/dsprb.rbs

Overview

X = sum_i x cos(pi k (i + 1/2) / N), unnormalized DCT-II.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Prepared

extended, prepared, prepared_count

Constructor Details

#initialize(length, count) ⇒ Two

Returns a new instance of Two.

Parameters:

  • (::Integer)
  • (::Integer)


13
14
15
16
17
18
19
20
21
22
23
# File 'lib/dsprb/dct.rb', line 13

def initialize(length, count)
  @length = Check.positive!(Integer(length), "length")
  @count = Check.positive!(Integer(count), "count")

  @basis = Array
    .new(@count) do |coefficient|
      Array.new(@length) { |index| Math.cos(Math::PI * coefficient * (index + 0.5) / @length) }.freeze
    end
    .freeze
  freeze
end

Instance Attribute Details

#count::Integer (readonly)

Returns the value of attribute count.

Returns:

  • (::Integer)


9
10
11
# File 'lib/dsprb/dct.rb', line 9

def count
  @count
end

#length::Integer (readonly)

Returns the value of attribute length.

Returns:

  • (::Integer)


9
10
11
# File 'lib/dsprb/dct.rb', line 9

def length
  @length
end

Class Method Details

.for(length, count) ⇒ Two

Parameters:

  • (::Integer)
  • (::Integer)

Returns:



11
# File 'lib/dsprb/dct.rb', line 11

def self.for(length, count) = prepared([length, count]) { new(length, count) }

Instance Method Details

#call(values) ⇒ ::Array[::Float]

Parameters:

  • (::Array[::Numeric])

Returns:

  • (::Array[::Float])


25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dsprb/dct.rb', line 25

def call(values)
  Array.new(@count) do |coefficient|
    row = @basis[coefficient]
    total = 0.0
    index = 0
    while index < @length
      total += values[index] * row[index]
      index += 1
    end

    total
  end
end