Class: STFTSpectrogram::Plot

Inherits:
Object
  • Object
show all
Defined in:
lib/plot/plot.rb

Overview

Creates plots using gnuplot

Constant Summary collapse

TEMP_SPECTR_IMG_FILE =
'spec'.freeze
TEMP_SPECTR_DAT_FILE =
'spec.dat'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePlot



12
13
14
15
16
# File 'lib/plot/plot.rb', line 12

def initialize
  @imgfile = Tempfile.new([TEMP_SPECTR_IMG_FILE, '.png'])
  @imgfile.close
  set_gnuplot_defaults
end

Instance Attribute Details

#imgfileObject (readonly)

Returns the value of attribute imgfile.



10
11
12
# File 'lib/plot/plot.rb', line 10

def imgfile
  @imgfile
end

Instance Method Details

#build_term_str(w, h, fnt_size) ⇒ Object



47
48
49
# File 'lib/plot/plot.rb', line 47

def build_term_str(w, h, fnt_size)
  'png font arial ' + fnt_size.to_s + ' size ' + w.to_s + ',' + h.to_s
end

#plot(x, y, w = 1920, h = 1080, fnt_size = 12) ⇒ Object

Plots data contained int x and y parameters



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/plot/plot.rb', line 68

def plot(x, y, w = 1920, h = 1080, fnt_size = 12)
  outfile = @imgfile.path
  termstr = build_term_str(w, h, fnt_size)
  Numo.gnuplot do
    set term: termstr
    set output: outfile
    set autoscale: 'xfix'
    set autoscale: 'yfix'
    set autoscale: 'cbfix'
    plot x, y, w: 'lines', t: '', lc_rgb: 'red'
    unset :output
  end
end

#plot_matrix(data, w = 1920, h = 1080, fnt_size = 12) ⇒ Object

Plots data in a matrix format using a temporary file Data are plotted to 2D with their magnitude being differentiated by color gradient



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/plot/plot.rb', line 54

def plot_matrix(data, w = 1920, h = 1080, fnt_size = 12)
  tmpfile = write_temp_data(data)
  outfile = @imgfile.path
  termstr = build_term_str(w, h, fnt_size)
  Numo.gnuplot do
    set term: termstr
    set output: outfile
    plot "'" + tmpfile.path + "'", :matrix, w: 'image', t: ''
    unset :output
  end
  tmpfile.unlink
end

#write_temp_data(data) ⇒ Object



40
41
42
43
44
45
# File 'lib/plot/plot.rb', line 40

def write_temp_data(data)
  datafile = Tempfile.new(TEMP_SPECTR_DAT_FILE)
  datafile.write(data.map { |row| row.join(' ') }.join("\n"))
  datafile.close
  datafile
end

#xtics=(str) ⇒ Object

Sets x axis tics



31
32
33
# File 'lib/plot/plot.rb', line 31

def xtics=(str)
  Numo.gnuplot { set xtics: str }
end

#ytics=(str) ⇒ Object

Sets y axis tics



36
37
38
# File 'lib/plot/plot.rb', line 36

def ytics=(str)
  Numo.gnuplot { set ytics: str }
end