class Curver

Constants

CHANNELS
ChannelCurve
IndexReader
MultiChannelCurve

Attributes

max_value[RW]
polynom_degree[RW]
polynom_precision[RW]
range_size[RW]
acv_file_path[R]

Public Class Methods

compute_polynom(points, range) click to toggle source
# File lib/curver.rb, line 76
def compute_polynom points, range
  values = spline_values(points, range)
  polynom(range, values)
end
multi_channel_curve(ary) click to toggle source
# File lib/curver.rb, line 69
def multi_channel_curve ary
  ir = IndexReader.new(2)
  channels_points = CHANNELS.map { |k| sanitize_points read_array!(ary, ir) }
  channels_curves = channels_points.map{ |points| ChannelCurve.new(points) }
  MultiChannelCurve.new(*channels_curves)
end
new(acv_file_path) click to toggle source
# File lib/curver.rb, line 26
def initialize(acv_file_path)
  raise "No file at this path" unless File.exist?(acv_file_path)
  self.class.set_default_values
  @acv_file_path = acv_file_path
end
polynom(x, y) click to toggle source
# File lib/curver.rb, line 94
def polynom x, y
  x_data = x.map { |xi| (0..polynom_degree).map { |pow| (xi**pow).to_f } }
  mx = Matrix[*x_data]
  my = Matrix.column_vector(y)
  ((mx.t * mx).inv * mx.t * my).transpose.to_a[0].map{|e| truncate(e, polynom_precision) }
end
range(coords) click to toggle source
# File lib/curver.rb, line 110
def range coords
  min_value, max_value = coords.first.first, coords.last.first
  (min_value..max_value).step((max_value - min_value)/range_size).to_a
end
read_array!(array, index_reader) click to toggle source
# File lib/curver.rb, line 86
def read_array! array, index_reader
  ary = array.drop(index_reader.position)
  raise 'Wrong index reader position' if ary.empty?
  points_count = (ary.first * 2)
  index_reader.position += points_count + 1
  ary[1..points_count]
end
read_curves(file_path) click to toggle source
# File lib/curver.rb, line 64
def read_curves file_path
  ary = File.read(file_path, encode: 'binary').unpack('S>*')
  multi_channel_curve(ary)
end
sanitize_points(array) click to toggle source
# File lib/curver.rb, line 81
def sanitize_points array
  ary = (array.length / 2).times.map{|i| [array[2*i+1], array[2*i]]}
  ary.map{ |dot| dot.map{ |v| v / max_value.to_f } }
end
set_default_values() click to toggle source
# File lib/curver.rb, line 57
def set_default_values
  self.polynom_degree    ||= 6
  self.max_value         ||= 255
  self.range_size        ||= 255  
  self.polynom_precision ||= 3    
end
spline_values(coords, range) click to toggle source
# File lib/curver.rb, line 105
def spline_values(coords, range)
  spline = Spliner::Spliner.new coords.map(&:first), coords.map(&:last)
  spline[range]
end
truncate(i, length) click to toggle source
# File lib/curver.rb, line 101
def truncate i, length
  (i * (10 ** length)).to_i.to_f / (10 ** length)
end

Public Instance Methods

curves() click to toggle source
# File lib/curver.rb, line 32
def curves
  @curves ||= self.class.read_curves(acv_file_path)
end
export_image(original_path, export_base_name) click to toggle source
# File lib/curver.rb, line 43
def export_image(original_path, export_base_name)
  self.class.export_image(curves, original_path, export_base_name)
end
polynoms() click to toggle source
# File lib/curver.rb, line 36
def polynoms
  CHANNELS.reduce({}) do |h, channel|
    h[channel] = curves[channel].polynom
    h
  end
end
puts_polynoms() click to toggle source
# File lib/curver.rb, line 47
def puts_polynoms
  puts "Polynoms (x^0 -> x^n) ---"
  puts polynoms
  puts "---"
end