module Digiproc::Plottable::InstanceMethods
Can be included in classes in which you may want a specific plot for a method output (ie the Discrete Fourier Transform magnitude plot)
Public Instance Methods
plot(method:, filename: "plot_ { |g| ... }
click to toggle source
plot(method: Symbol, filename: String [default = "plot_#{self.class}"], path: String [default = "./"], xmax: Integer [default = 1], xmin: Integer [default = 0], xsteps: Integer [default = 4])
Can be used to plot the output of a specific method. By specifying the name of the method when you call plot, if the output of that method is a Numeric Array
, then a plot will be made. An example can be seen in Digiproc::FFT
plot_db
# File lib/concerns/plottable.rb, line 191 def plot(method:, filename: "plot_#{self.class}", path: "./", xmax: 1, xmin: 0, xsteps: 4) data = self.send(method) raise TypeError.new('Data must be an array, not a #{data.class}') if not data.is_a? Array g = Gruff::Line.new('1000x1000') g.theme = Digiproc::Plottable::Styles::MIDNIGHT g.line_width = 1 g.dot_radius = 1 g.minimum_x_value = 0 g.data method, data increment_label = (xmax - xmin) / xsteps.to_f increment_datapoints = (data.length.to_f / xsteps).round labels = {} for i in 0..xsteps do datapoint_location = i * increment_datapoints datapoint_location -= 1 if datapoint_location > (data.length - 1) labels[datapoint_location] = (i * increment_label).round(2) end g.labels = labels g.show_vertical_markers = true yield g g.write(path + filename + '.png') end
qplot(x: nil ,y: nil , data: nil, data_name: "data", xyname: "data",filename: " { |g| ... }
click to toggle source
Same as qplot in Digiproc::Plottable::InstanceMethods
# File lib/concerns/plottable.rb, line 217 def qplot(x: nil ,y: nil , data: nil, data_name: "data", xyname: "data",filename: "#{self}_plot", path: "./plots/", xsteps: 4, label_map: nil) raise ArgumentError.new("Either x and y or data must exist") if data.nil? and (x.nil? or y.nil?) data = data raise TypeError.new("Data must be an array, not a #{data.class}") if data and not data.is_a? Array raise TypeError.new("X and Y must be arrays, not #{x.class}, #{y.class}") if (!!x and !!y) and not (x.is_a?(Array) and y.is_a?(Array)) raise ArgumentError.new("X and Y must be the same size") if (!!x and !!y) and (x.length != y.length) g = Gruff::Line.new('1000x1000') g.theme = Styles::BLUESCALE g.line_width = 2.5 g.dot_radius = 0.1 # g.minimum_x_value = 0 g.data data_name, data if !!data g.dataxy xyname, x, y if !!x and !!y xmax = !!data ? data.length : x.max xmin = !!data ? 0 : x.min increment_label = (xmax - xmin) / xsteps.to_f datalength = !!data ? data.length : x.length increment_datapoints = (datalength.to_f / xsteps).round labels = {} for i in 0..xsteps do datapoint_location = i * increment_datapoints datapoint_location -= 1 if datapoint_location > (datalength - 1) label_val = label_map.nil? ? (i * increment_label).round(2) : label_map.call((i * increment_label)).round(2) labels[datapoint_location] = label_val end g.labels = labels g.show_vertical_markers = false yield g g.write(path + filename + '.png') end