Class: SPCore::Interpolation

Inherits:
Object
  • Object
show all
Defined in:
lib/spcore/interpolation/interpolation.rb

Overview

Provide interpolation methods, including linear and polynomial.

Class Method Summary collapse

Class Method Details

.cubic_hermite(y0, y1, y2, y3, x) ⇒ Object

4-point, 3rd-order (cubic) Hermite interpolater (x-form).

Given 4 evenly-spaced sample points, interpolate a value anywhere between the middle two points.

implementation source: www.musicdsp.org/archive.php?classid=5#93

Parameters:

  • y0 (Float)

    First y-value

  • y1 (Float)

    Second y-value (first middle point)

  • y2 (Float)

    Third y-value (second middle point)

  • y3 (Float)

    Fourth y-value

  • x (Float)

    Percent distance (along the x-axis) between the middle two y-values (y1 and y2)

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/spcore/interpolation/interpolation.rb', line 39

def self.cubic_hermite y0, y1, y2, y3, x
  raise ArgumentError, "x is not between 0.0 and 1.0" unless x.between?(0.0,1.0)

  ## method 1 (slowest)
  #c0 = y1
  #c1 = 0.5 * (y2 - y0)
  #c2 = y0 - 2.5 * y1 + 2*y2 - 0.5 * y3
  #c3 = 1.5 * (y1 - y2) + 0.5 * (y3 - y0)
  
  # method 2 (basically tied with method 3)
  c0 = y1
  c1 = 0.5 * (y2 - y0)
  c3 = 1.5 * (y1 - y2) + 0.5 * (y3 - y0)
  c2 = y0 - y1 + c1 - c3

  ## method 3 (basically tied with method 2)
  #c0 = y1
  #c1 = 0.5 * (y2 - y0)
  #y0my1 = y0 - y1
  #c3 = (y1 - y2) + 0.5 * (y3 - y0my1 - y2)
  #c2 = y0my1 + c1 - c3
  
  return ((c3 * x + c2) * x + c1) * x + c0
end

.linear(y0, y1, x) ⇒ Object

Linear interpolator Given 2 sample points, interpolates a value anywhere between the two points.

Parameters:

  • y0 (Numeric)

    First (left) y-value

  • y1 (Numeric)

    Second (right) y-value

  • x (Numeric)

    Percent distance (along the x-axis) between the two y-values

Raises:

  • (ArgumentError)


22
23
24
25
# File 'lib/spcore/interpolation/interpolation.rb', line 22

def self.linear y0, y1, x
  raise ArgumentError, "x is not between 0.0 and 1.0" unless x.between?(0.0,1.0)
  return y0 + x * (y1 - y0)
end