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)).
[0.54, 0.46].freeze
- HANN =
[0.5, 0.5].freeze
- BLACKMAN =
[0.42, 0.5, 0.08].freeze
- REGISTRY =
{ rectangular: method(:rectangular), hamming: method(:hamming), hann: method(:hann), blackman: method(:blackman) }.freeze
- DEFAULT =
:hamming
Class Method Summary collapse
- .apply(frame, window) ⇒ ::Array[::Float]
- .assert_length!(length) ⇒ void
- .blackman(length) ⇒ ::Array[::Float]
- .cosine_sum(length, coefficients) ⇒ ::Array[::Float]
- .hamming(length) ⇒ ::Array[::Float]
- .hann(length) ⇒ ::Array[::Float]
- .rectangular(length) ⇒ ::Array[::Float]
- .resolve(window) ⇒ _WindowFunction
Class Method Details
.apply(frame, window) ⇒ ::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.
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]
22 |
# File 'lib/dsprb/window.rb', line 22 def blackman(length) = cosine_sum(length, BLACKMAN) |
.cosine_sum(length, coefficients) ⇒ ::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]
18 |
# File 'lib/dsprb/window.rb', line 18 def hamming(length) = cosine_sum(length, HAMMING) |
.hann(length) ⇒ ::Array[::Float]
20 |
# File 'lib/dsprb/window.rb', line 20 def hann(length) = cosine_sum(length, HANN) |
.rectangular(length) ⇒ ::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
53 |
# File 'lib/dsprb/window.rb', line 53 def resolve(window) = Resolver.call(window, REGISTRY, "window") |