Module: DSP::Window

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

Constant Summary collapse

HAMMING =

Generalized cosine windows 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

Class Method Details

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

Parameters:

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

Returns:

  • (::Array[::Float])


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

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)


38
39
40
41
42
# File 'lib/dsprb/window.rb', line 38

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])


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

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

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

Parameters:

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

Returns:

  • (::Array[::Float])


24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dsprb/window.rb', line 24

def cosine_sum(length, coefficients)
  assert_length!(length)
  return [1.0] if length == 1

  span = (length - 1).to_f

  Array.new(length) do |index|
    phase = 2.0 * Math::PI * index / span
    coefficients.each_with_index.sum { |weight, term| (term.even? ? weight : -weight) * Math.cos(term * phase) }
  end
end

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

Parameters:

  • (::Integer)

Returns:

  • (::Array[::Float])


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

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

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

Parameters:

  • (::Integer)

Returns:

  • (::Array[::Float])


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

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

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

Parameters:

  • (::Integer)

Returns:

  • (::Array[::Float])


12
13
14
15
16
# File 'lib/dsprb/window.rb', line 12

def rectangular(length)
  assert_length!(length)

  Array.new(length, 1.0)
end

.resolve(window) ⇒ _WindowFunction

Parameters:

  • (window)

Returns:



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

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