Created
March 28, 2014 14:42
-
-
Save birula/9834428 to your computer and use it in GitHub Desktop.
Ruby Wraper to Gnuplot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Reference: http://makaroni4.com/ruby/hacking/2014/03/26/how-to-tune-guitar-with-ruby/