Module: DSP::Window

Extended by:
Prepared
Defined in:
lib/dsprb/window.rb,
sig/dsprb.rbs

Constant Summary collapse

HAMMING =

w = sum_k (-1)^k a_k cos(2 pi k i / (N - 1)).

Returns:

  • (::Array[::Float])
[0.54, 0.46].freeze
HANN =

Returns:

  • (::Array[::Float])
[0.5, 0.5].freeze
BLACKMAN =

Returns:

  • (::Array[::Float])
[0.42, 0.5, 0.08].freeze
REGISTRY =

Returns:

{
  rectangular: method(:rectangular),
  hamming: method(:hamming),
  hann: method(:hann),
  blackman: method(:blackman)
}.freeze
DEFAULT =

Returns:

  • (::Symbol)
:hamming

Class Method Summary collapse

Methods included from Prepared

extended, prepared, prepared_count

Class Method Details

.apply(frame, window) ⇒ ::Array[::Float]

Parameters:

  • (frame)
  • (::Array[::Float])

Returns:

  • (::Array[::Float])


51
# File 'lib/dsprb/window.rb', line 51

def apply(frame, window) = Array.new(frame.length) { |index| frame[index] * window[index] }

.assert_length!(length) ⇒ void

This method returns an undefined value.

Parameters:

  • (Object)

Raises:

  • (ArgumentError)


53
54
55
56
57
# File 'lib/dsprb/window.rb', line 53

def assert_length!(length)
  return if length.is_a?(Integer) && length.positive?

  raise ArgumentError, "window length must be a positive Integer, got #{length.inspect}"
end

.blackman(length) ⇒ ::Array[::Float]

Parameters:

  • (::Integer)

Returns:

  • (::Array[::Float])


24
# File 'lib/dsprb/window.rb', line 24

def blackman(length) = cosine_sum(length, BLACKMAN)

.build_cosine_sum(length, coefficients) ⇒ ::Array[::Float]

Parameters:

  • (::Integer)
  • (::Array[::Float])

Returns:

  • (::Array[::Float])


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dsprb/window.rb', line 32

def build_cosine_sum(length, coefficients)
  return [1.0] if length == 1

  span = (length - 1).to_f
  terms = coefficients.length

  Array.new(length) do |index|
    phase = 2.0 * Math::PI * index / span
    total = 0.0
    term = 0
    while term < terms
      total += (term.even? ? coefficients[term] : -coefficients[term]) * Math.cos(term * phase)
      term += 1
    end

    total
  end
end

.cosine_sum(length, coefficients) ⇒ ::Array[::Float]

Parameters:

  • (::Integer)
  • (::Array[::Float])

Returns:

  • (::Array[::Float])


26
27
28
29
30
# File 'lib/dsprb/window.rb', line 26

def cosine_sum(length, coefficients)
  assert_length!(length)

  Window.prepared([:cosine_sum, length, coefficients]) { build_cosine_sum(length, coefficients).freeze }
end

.hamming(length) ⇒ ::Array[::Float]

Parameters:

  • (::Integer)

Returns:

  • (::Array[::Float])


20
# File 'lib/dsprb/window.rb', line 20

def hamming(length) = cosine_sum(length, HAMMING)

.hann(length) ⇒ ::Array[::Float]

Parameters:

  • (::Integer)

Returns:

  • (::Array[::Float])


22
# File 'lib/dsprb/window.rb', line 22

def hann(length) = cosine_sum(length, HANN)

.rectangular(length) ⇒ ::Array[::Float]

Parameters:

  • (::Integer)

Returns:

  • (::Array[::Float])


14
15
16
17
18
# File 'lib/dsprb/window.rb', line 14

def rectangular(length)
  assert_length!(length)

  Window.prepared([:rectangular, length]) { Array.new(length, 1.0).freeze }
end

.resolve(window) ⇒ _WindowFunction

Parameters:

  • (window)

Returns:



68
# File 'lib/dsprb/window.rb', line 68

def resolve(window) = Resolver.call(window, REGISTRY, "window")