Skip to content

Instantly share code, notes, and snippets.

@birula
Created March 28, 2014 14:42
Show Gist options
  • Save birula/9834428 to your computer and use it in GitHub Desktop.
Save birula/9834428 to your computer and use it in GitHub Desktop.
Ruby Wraper to Gnuplot
require 'open3'
class GNUPlotter < Struct.new(:data, :params)
def plot
image, s = Open3.capture2("gnuplot", stdin_data: gnuplot_commands, binmode: true)
system "open #{params[:image_name]}"
end
private
def gnuplot_commands
commands = %{
set terminal png font "/Library/Fonts/Arial.ttf" 14
set title "#{params[:title]}"
set xlabel "#{params[:x_axis_title]}"
set ylabel "#{params[:y_axis_title]}"
set output "#{params[:image_name]}"
set key off
plot "-" with points
}
data.each do |x, y|
commands << "#{x} #{y}\n"
end
commands << "e\n"
end
end
sound_data = File.read("guitar_first_string.dat").split("\n")[2..-1].map { |row| row.split.map(&:to_f) }.
map { |r| r.first(2) }
plot_params = {
image_name: "plot.png",
title: "Guitar first string sound",
x_axis_title: "Time, s",
y_axis_title: ".wav signal"
}
plotter = GNUPlotter.new(sound_data, plot_params)
plotter.plot
@birula
Copy link
Author

birula commented Mar 28, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment