Changeset 733c2f8 for python/aubio
- Timestamp:
- Nov 6, 2005, 12:30:12 PM (19 years ago)
- Branches:
- feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
- Children:
- cd9b142
- Parents:
- d45520a
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/aubio/gnuplot.py
rd45520a r733c2f8 21 21 """ 22 22 23 23 24 __notesheight = 0.25 24 25 25 from numarray import *26 import Gnuplot, Gnuplot.funcutils27 28 def plotnote(la,title=None) :29 if la[0,:].size() == 3:30 d = plotnote_withends(la, plot_title=title)31 else:32 # scale data if in freq (for REF.txt files)33 if max(la[:,1] > 128 ):34 print "scaling frequency data to midi range"35 la[:,1] /= 6.87536 la[:,1] = log(la[:,1])/0.693137 la[:,1] *= 1238 la[:,1] -= 339 d = plotnote_withoutends(la, plot_title=title)40 return d41 42 def plotnote_multi(lalist,title=None,fileout=None) :43 d=list()44 for i in range(len(lalist)):45 d.append(plotnote(lalist[i], title=title))46 return d47 48 49 def plotnote_withends(la,plot_title=None) :50 d=[]51 x_widths = array(la[:,1]-la[:,0])/2.52 d.append(Gnuplot.Data(53 la[:,0]+x_widths, # x centers54 la[:,2], # y centers55 x_widths, # x errors56 __notesheight*ones(len(la)), # y errors57 title=plot_title,with=('boxxyerrorbars fs 3')))58 return d59 60 61 def plotnote_withoutends(la,plot_title=None) :62 """ bug: fails drawing last note """63 d=[]64 x_widths = array(la[1:,0]-la[:-1,0])/2;65 d.append(Gnuplot.Data(66 la[:-1,0]+x_widths, # x centers67 la[:-1,1], # y centers68 x_widths, # x errors69 __notesheight*ones(len(la)-1), # y errors70 title=plot_title,with=('boxxyerrorbars fs 3')))71 return d72 73 def plotnote_do(d,fileout=None):74 g = Gnuplot.Gnuplot(debug=1, persist=1)75 g.gnuplot('set style fill solid border 1; \76 set size ratio 1/6; \77 set boxwidth 0.9 relative; \78 set mxtics 2.5; \79 set mytics 2.5; \80 set xtics 5; \81 set ytics 1; \82 set grid xtics ytics mxtics mytics')83 84 g.xlabel('Time (s)')85 g.ylabel('Midi pitch')86 # do the plot87 #g.gnuplot('set multiplot')88 #for i in d:89 g.plot(d[0])90 #g.gnuplot('set nomultiplot')91 if fileout != None:92 g.hardcopy(fileout, enhanced=1, color=0)93 26 94 27 def audio_to_array(filename): 95 28 import aubio.aubioclass 29 import numarray 96 30 hopsize = 2048 97 31 filei = aubio.aubioclass.sndfile(filename) … … 109 43 data.append(myvec.get(curpos,i)) 110 44 curpos+=1 111 time = arange(len(data))*framestep45 time = numarray.arange(len(data))*framestep 112 46 return time,data 113 47 114 48 def plot_audio(filenames, fileout=None, start=0, end=None, noaxis=None): 115 g = Gnuplot.Gnuplot(debug=1, persist=1)49 g = gnuplot_init(fileout) 116 50 d = [] 117 51 todraw = len(filenames) … … 120 54 g.gnuplot('set multiplot;') 121 55 while (len(filenames)): 122 d.append(plot_audio_make(filenames.pop(0))) 56 time,data = audio_to_array(filenames.pop(0)) 57 d.append(make_audio_plot(time,data)) 123 58 if not noaxis and todraw==1: 124 59 g.xlabel('Time (s)') … … 128 63 g.gnuplot('set style data lines; \ 129 64 set yrange [-1.:1.]; \ 130 set xrange [0:%f]' % b[-1])65 set xrange [0:%f]' % time[-1]) 131 66 g.plot(d.pop(0)) 132 67 xorig += 1./todraw 133 68 g.gnuplot('unset multiplot;') 134 if fileout != None:135 g.hardcopy(fileout, enhanced=1, color=0)136 69 137 70 def make_audio_plot(time,data,maxpoints=10000): 138 71 """ create gnuplot plot from an audio file """ 72 import numarray 73 import Gnuplot, Gnuplot.funcutils 139 74 length = len(time) 140 75 downsample = length/maxpoints 141 76 if downsample == 0: downsample = 1 142 x = array(time).resize(length)[0:-1:downsample]143 y = array(data).resize(length)[0:-1:downsample]77 x = numarray.array(time).resize(length)[0:-1:downsample] 78 y = numarray.array(data).resize(length)[0:-1:downsample] 144 79 return Gnuplot.Data(x,y,with='lines') 145 80 146 81 147 82 def plot_onsets(filename, onsets, ofunc, samplerate=44100., hopsize=512, outplot=None): 83 import Gnuplot, Gnuplot.funcutils 148 84 import aubio.txtfile 149 85 import os.path 150 86 import numarray 87 import re 151 88 from aubio.onsetcompare import onset_roc 152 89 … … 186 123 ((100*float(orig-missed-merged)/(orig)), 187 124 (100*float(bad+doubled)/(orig))) 188 #print orig, missed, merged, expc, bad, doubled189 #print "GD %2.8f\t" % (100*float(orig-missed-merged)/(orig)),190 #print "FP %2.8f\t" % (100*float(bad+doubled)/(orig)) ,191 #print "GD-merged %2.8f\t" % (100*float(orig-missed)/(orig)) ,192 #print "FP-pruned %2.8f\t" % (100*float(bad)/(orig))193 125 194 126 # audio data … … 197 129 198 130 # prepare the plot 199 g = Gnuplot.Gnuplot(debug=1, persist=1) 200 if outplot: 201 extension = outplot.split('.')[-1] 202 if extension == 'ps': extension = 'postscript' 203 g('set terminal %s' % extension) 204 g('set output \'%s\'' % outplot) 205 206 g('set title \'%s %s\'' % (filename,title)) 131 g = gnuplot_init(outplot) 132 133 g('set title \'%s %s\'' % (re.sub('.*/','',filename),title)) 207 134 208 135 g('set multiplot') … … 237 164 import os.path 238 165 import numarray 166 import Gnuplot 167 import re 239 168 240 169 d = [] … … 246 175 maxpitch = max(maxpitch,max(pitch[i][:])*1.1) 247 176 248 # check if datafile exists truth177 # check if ground truth exists 249 178 datafile = filename.replace('.wav','.txt') 250 179 if not os.path.isfile(datafile): … … 254 183 title = "truth file plotting not implemented yet" 255 184 values = aubio.txtfile.read_datafile(datafile) 256 time, pitch = [], [] 257 for i in range(len(values)): 258 time.append(values[i][0]) 259 pitch.append(values[i][1]) 260 d.append(Gnuplot.Data(time,pitch,with='lines',title='ground truth')) 185 if (len(datafile[0])) > 1: 186 time, pitch = [], [] 187 for i in range(len(values)): 188 time.append(values[i][0]) 189 pitch.append(values[i][1]) 190 d.append(Gnuplot.Data(time,pitch,with='lines', 191 title='ground truth')) 261 192 262 #orig, missed, merged, expc, bad, doubled = \263 # onset_roc(x2,x1,tol)264 #title = "GD %2.3f%% FP %2.3f%%" % \265 # ((100*float(orig-missed-merged)/(orig)),266 # (100*float(bad+doubled)/(orig)))267 #print orig, missed, merged, expc, bad, doubled268 #print "GD %2.8f\t" % (100*float(orig-missed-merged)/(orig)),269 #print "FP %2.8f\t" % (100*float(bad+doubled)/(orig)) ,270 #print "GD-merged %2.8f\t" % (100*float(orig-missed)/(orig)) ,271 #print "FP-pruned %2.8f\t" % (100*float(bad)/(orig))272 273 193 # audio data 274 194 time,data = audio_to_array(filename) 275 195 f = make_audio_plot(time,data) 276 196 277 # prepare the plot 278 g = Gnuplot.Gnuplot(debug=1, persist=1) 279 if outplot: 280 extension = outplot.split('.')[-1] 281 if extension == 'ps': extension = 'postscript' 282 g('set terminal %s' % extension) 283 g('set output \'%s\'' % outplot) 284 285 g('set title \'%s %s\'' % (filename,title)) 286 197 g = gnuplot_init(outplot) 198 g('set title \'%s %s\'' % (re.sub('.*/','',filename),title)) 287 199 g('set multiplot') 288 289 200 # hack to align left axis 290 201 g('set lmargin 15') 291 292 202 # plot waveform and onsets 293 203 g('set size 1,0.3') … … 297 207 g.ylabel('amplitude') 298 208 g.plot(f) 299 300 209 g('unset title') 301 302 210 # plot onset detection function 303 211 g('set size 1,0.7') … … 309 217 g.ylabel('frequency (Hz)') 310 218 g.plot(*d) 311 312 219 g('unset multiplot') 220 221 def gnuplot_init(outplot,debug=0,persist=1): 222 import Gnuplot 223 # prepare the plot 224 g = Gnuplot.Gnuplot(debug=debug, persist=persist) 225 if outplot == 'stdout': 226 g("set terminal png fontfile 'p052023l.pfb'") 227 #g('set output \'%s\'' % outplot) 228 elif outplot: 229 extension = outplot.split('.')[-1] 230 if extension == 'ps': extension = 'postscript' 231 g('set terminal %s' % extension) 232 g('set output \'%s\'' % outplot) 233 return g
Note: See TracChangeset
for help on using the changeset viewer.