Module: DSP::Framing

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

Constant Summary collapse

DEFAULT_PREEMPHASIS =

First-order high-pass that flattens the -6 dB/octave tilt of the glottal source.

Returns:

  • (::Float)
0.97

Class Method Summary collapse

Class Method Details

.frame_count(length, window:, hop:) ⇒ ::Integer

Parameters:

  • (::Integer)
  • window: (::Integer)
  • hop: (::Integer)

Returns:

  • (::Integer)


24
25
26
27
28
# File 'lib/dsprb/framing.rb', line 24

def frame_count(length, window:, hop:)
  return 0 if length < window

  ((length - window) / hop) + 1
end

.frames(samples, window:, hop:) ⇒ ::Array[frame]

Parameters:

  • (samples)
  • window: (::Integer)
  • hop: (::Integer)

Returns:

  • (::Array[frame])

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/dsprb/framing.rb', line 10

def frames(samples, window:, hop:)
  raise ArgumentError, "window must be positive, got #{window}" unless window.positive?
  raise ArgumentError, "hop must be positive, got #{hop}" unless hop.positive?

  out = []
  position = 0
  while position + window <= samples.length
    out << samples[position, window]
    position += hop
  end

  out
end

.preemphasis(samples, coefficient: DEFAULT_PREEMPHASIS) ⇒ ::Array[::Float]

Parameters:

  • (samples)
  • coefficient: (::Numeric) (defaults to: DEFAULT_PREEMPHASIS)

Returns:

  • (::Array[::Float])


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/dsprb/framing.rb', line 30

def preemphasis(samples, coefficient: DEFAULT_PREEMPHASIS)
  return [] if samples.empty?

  out = Array.new(samples.length)
  out[0] = samples[0].to_f
  index = 1
  while index < samples.length
    out[index] = samples[index] - (coefficient * samples[index - 1])
    index += 1
  end

  out
end