#!/usr/bin/env ruby $: << '~/src/eclipse/autopilot/common/ruby/' require 'gnuplot' require 'gnuplot_support' if ARGV.size < 2 then puts "#{__FILE__} target0 target1 [skip = 0] [denominator = 1]" exit end data0 = [] open(ARGV[0]).each{|line| splitted = line.split(/\s+/)[1..-1] if '0' == splitted[0] then next end data0 << splitted[1..-1].collect{|item| item.to_i} } data1 = [] if ARGV[0] == ARGV[1] then data1 = data0 else open(ARGV[1]).each{|line| splitted = line.split(/\s+/)[1..-1] if '0' == splitted[0] then next end data1 << splitted[1..-1].collect{|item| item.to_i} } end if ARGV[2] then data0 = data0[(ARGV[2].to_i)..-1] if data1 != data0 then data1 = data1[(ARGV[2].to_i)..-1] end end if ARGV[3] then step = ARGV[3].to_i omitted = [] data0.each_with_index{|v, i| if i % step == 0 then omitted << v end } data0 = omitted if data1 != data0 then omitted = [] data1.each_with_index{|v, i| if i % step == 0 then omitted << v end } data1 = omitted end end p data0.size p data1.size TARGETS = [[6, 0], [6, 1], [6, 2], [6, 3], [6, 4], [6, 5]] # グラフを書く TARGETS.each{|target| Gnuplot::open{|gp| # データの生成 data_x = data0.collect{|item| item[target[0]]} data_y = data1.collect{|item| item[target[1]]} if data_x.size < data_y.size then data_y = data_y[0...(data_x.size)] elsif data_x.size > data_y.size then data_x = data_x[0...(data_y.size)] end items = [ Gnuplot::DataSet::new([data_x, data_y]){|ds| ds.with = "dots" ds.notitle } ] # 近似曲線の計算 sum_x = 0 sum_x2 = 0 sum_y = 0 sum_xy = 0 n = data_x.size (0...n).each{|i| sum_x += data_x[i] sum_x2 += data_x[i] ** 2 sum_y += data_y[i] sum_xy += data_x[i] * data_y[i] } a = (n * sum_xy - sum_x * sum_y).to_f / (n * sum_x2 - (sum_x ** 2)) b = (sum_x2 * sum_y - sum_x * sum_xy).to_f / (n * sum_x2 - (sum_x ** 2)) puts "a = #{a}, b = #{b}" # 近似曲線の定義 gp << "a=#{a}\n" gp << "b=#{b}\n" gp << "f(x)=a*x+b\n" # 近似曲線の追加 items << Gnuplot::DataSet::new("f(x)"){|ds| ds.with = "lines lw 2 lt 1" ds.title = "a x + b" } gp << "set label 'a = %f, ',a,'b = %f',b at screen 0.93, 0.15 right\n" # データの書き出し Plotter::plot_eps("ch#{target[0]}_ch#{target[1]}.eps", gp){|plot| plot.xlabel "Channel #{target[0]}" plot.ylabel "Channel #{target[1]}" plot.set('pointsize', '3') plot.data = items } } }