Class: Ecu::Interpolator
- Inherits:
-
Object
- Object
- Ecu::Interpolator
- Defined in:
- lib/ecu/labels/interpolator.rb
Class Method Summary collapse
- .find_position(ref, x) ⇒ Object
- .interp(x, y, f) ⇒ Object
- .interp1(x_ref, y_ref, x) ⇒ Object
- .interp2(x_ref, y_ref, z_ref, x, y) ⇒ Object
Class Method Details
.find_position(ref, x) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/ecu/labels/interpolator.rb', line 37 def self.find_position(ref, x) x = x.to_f n = ref.size - 1 return [0, 0, 0] if x < ref.first return [n, n, 0] if x > ref.last ref.each_cons(2).each do |lo, hi| if (lo..hi).cover?(x) f = (x - lo)/(hi - lo) return [ref.index(lo), ref.index(hi), f] end end end |
.interp(x, y, f) ⇒ Object
51 52 53 |
# File 'lib/ecu/labels/interpolator.rb', line 51 def self.interp(x, y, f) x*(1-f) + y*f end |
.interp1(x_ref, y_ref, x) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/ecu/labels/interpolator.rb', line 9 def self.interp1(x_ref, y_ref, x) raise ArgumentError unless x_ref.size == y_ref.size raise ArgumentError unless x_ref.strictly_mon_inc? return y_ref.first if x < x_ref.first return y_ref.last if x > x_ref.last x1, x2, fx = find_position(x_ref, x) interp(y_ref[x1], y_ref[x2], fx) end |
.interp2(x_ref, y_ref, z_ref, x, y) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/ecu/labels/interpolator.rb', line 21 def self.interp2(x_ref, y_ref, z_ref, x, y) raise ArgumentError unless z_ref.size == y_ref.size raise ArgumentError unless z_ref.all? { |row| row.size == x_ref.size } raise ArgumentError unless x_ref.strictly_mon_inc? raise ArgumentError unless y_ref.strictly_mon_inc? x1, x2, fx = find_position(x_ref, x) y1, y2, fy = find_position(y_ref, y) interp(interp(z_ref[y1][x1], z_ref[y2][x1], fy), interp(z_ref[y1][x2], z_ref[y2][x2], fy), fx) end |