Module: DSP::Curve

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

Class Method Summary collapse

Class Method Details

.resample(values, length) ⇒ ::Array[::Float]

Linear interpolation onto a uniform grid of the requested length, used to bring contours of differing duration onto a common time base.

Parameters:

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

Returns:

  • (::Array[::Float])

Raises:

  • (ArgumentError)


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

def resample(values, length)
  raise ArgumentError, "length must be positive, got #{length}" unless length.positive?
  return Array.new(length, 0.0) if values.empty?
  return Array.new(length) { values.first.to_f } if values.length == 1 || length == 1

  last = values.length - 1
  span = length - 1

  Array.new(length) do |index|
    position = index.to_f * last / span
    lower = position.floor
    upper = [lower + 1, last].min
    fraction = position - lower
    (values[lower] * (1.0 - fraction)) + (values[upper] * fraction)
  end
end