Module: Digiproc::Plottable::InstanceMethods

Included in:
FFT
Defined in:
lib/concerns/plottable.rb

Overview

Can be included in classes in which you may want a specific plot for a method output (ie the Discrete Fourier Transform magnitude plot)

Instance Method Summary collapse

Instance Method Details

#plot(method:, filename: "plot_#{self.class}", path: "./", xmax: 1, xmin: 0, xsteps: 4) {|g| ... } ⇒ Object

plot(method: Symbol, filename: String [default = “plot_#Digiproc::Plottable::InstanceMethods.selfself.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

Yields:

  • (g)

Raises:

  • (TypeError)


191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# 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: "#{self}_plot", path: "./plots/", xsteps: 4, label_map: nil) {|g| ... } ⇒ Object

Same as qplot in Digiproc::Plottable::InstanceMethods

Yields:

  • (g)

Raises:

  • (ArgumentError)


217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# 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