Module: Muze::Core::Windows
- Defined in:
- lib/muze/core/windows.rb
Overview
Window function generators for short-time analysis.
Constant Summary collapse
- CACHE =
Muze::Core::BoundedCache.new(max_size: 64)
Class Method Summary collapse
- .blackman(n, periodic: false) ⇒ Numo::SFloat
- .blackman_harris(n, periodic: false) ⇒ Numo::SFloat
- .hamming(n, periodic: false) ⇒ Numo::SFloat
- .hann(n, periodic: false) ⇒ Numo::SFloat
- .kaiser(n, beta: 14.0, periodic: false) ⇒ Numo::SFloat
- .ones(n) ⇒ Numo::SFloat
- .resolve(name, n, periodic: false) ⇒ Numo::SFloat
- .tukey(n, alpha: 0.5, periodic: false) ⇒ Numo::SFloat
Class Method Details
.blackman(n, periodic: false) ⇒ Numo::SFloat
33 34 35 36 37 38 39 40 41 |
# File 'lib/muze/core/windows.rb', line 33 def blackman(n, periodic: false) raise Muze::ParameterError, "window length must be positive" if n <= 0 return Numo::SFloat[1.0] if n == 1 build_window(n, periodic:) do |k, denom| phase = (2.0 * Math::PI * k) / denom 0.42 - (0.5 * Math.cos(phase)) + (0.08 * Math.cos(2.0 * phase)) end end |
.blackman_harris(n, periodic: false) ⇒ Numo::SFloat
46 47 48 49 50 51 52 53 54 |
# File 'lib/muze/core/windows.rb', line 46 def blackman_harris(n, periodic: false) raise Muze::ParameterError, "window length must be positive" if n <= 0 return Numo::SFloat[1.0] if n == 1 build_window(n, periodic:) do |k, denom| phase = (2.0 * Math::PI * k) / denom 0.35875 - (0.48829 * Math.cos(phase)) + (0.14128 * Math.cos(2.0 * phase)) - (0.01168 * Math.cos(3.0 * phase)) end end |
.hamming(n, periodic: false) ⇒ Numo::SFloat
23 24 25 26 27 28 |
# File 'lib/muze/core/windows.rb', line 23 def hamming(n, periodic: false) raise Muze::ParameterError, "window length must be positive" if n <= 0 return Numo::SFloat[1.0] if n == 1 build_window(n, periodic:) { |k, denom| 0.54 - (0.46 * Math.cos((2.0 * Math::PI * k) / denom)) } end |
.hann(n, periodic: false) ⇒ Numo::SFloat
13 14 15 16 17 18 |
# File 'lib/muze/core/windows.rb', line 13 def hann(n, periodic: false) raise Muze::ParameterError, "window length must be positive" if n <= 0 return Numo::SFloat[1.0] if n == 1 build_window(n, periodic:) { |k, denom| 0.5 * (1.0 - Math.cos((2.0 * Math::PI * k) / denom)) } end |
.kaiser(n, beta: 14.0, periodic: false) ⇒ Numo::SFloat
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/muze/core/windows.rb', line 60 def kaiser(n, beta: 14.0, periodic: false) raise Muze::ParameterError, "window length must be positive" if n <= 0 return Numo::SFloat[1.0] if n == 1 denominator = bessel_i0(beta) build_window(n, periodic:) do |k, denom| ratio = ((2.0 * k) / denom) - 1.0 bessel_i0(beta * Math.sqrt([1.0 - (ratio * ratio), 0.0].max)) / denominator end end |
.ones(n) ⇒ Numo::SFloat
96 97 98 99 100 |
# File 'lib/muze/core/windows.rb', line 96 def ones(n) raise Muze::ParameterError, "window length must be positive" if n <= 0 Numo::SFloat.ones(n) end |
.resolve(name, n, periodic: false) ⇒ Numo::SFloat
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/muze/core/windows.rb', line 106 def resolve(name, n, periodic: false) resolved = case name when Numo::NArray then Numo::SFloat.cast(name) when Array then Numo::SFloat.cast(name) when Proc then Numo::SFloat.cast(name.call(n)) when Symbol then cached_symbol_window(name, n, periodic:).dup else raise Muze::ParameterError, "Unsupported window: #{name.inspect}" end raise Muze::ParameterError, "window must have length #{n}" unless resolved.size == n resolved end |
.tukey(n, alpha: 0.5, periodic: false) ⇒ Numo::SFloat
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/muze/core/windows.rb', line 75 def tukey(n, alpha: 0.5, periodic: false) raise Muze::ParameterError, "window length must be positive" if n <= 0 raise Muze::ParameterError, "alpha must be between 0 and 1" unless alpha.between?(0.0, 1.0) return ones(n) if alpha.zero? return hann(n, periodic:) if alpha == 1.0 return Numo::SFloat[1.0] if n == 1 build_window(n, periodic:) do |k, denom| position = k.to_f / denom if position < alpha / 2.0 0.5 * (1.0 + Math.cos(Math::PI * ((2.0 * position / alpha) - 1.0))) elsif position <= 1.0 - (alpha / 2.0) 1.0 else 0.5 * (1.0 + Math.cos(Math::PI * ((2.0 * position / alpha) - (2.0 / alpha) + 1.0))) end end end |